44
55import com .google .common .collect .Iterables ;
66
7+ import java .io .*;
8+ import java .nio .charset .StandardCharsets ;
79import java .io .IOException ;
810import java .nio .file .Files ;
911import java .nio .file .Path ;
@@ -45,13 +47,11 @@ public List<BazelTarget> queryAllTargets() throws IOException {
4547 @ Override
4648 public Set <String > queryForImpactedTargets (Set <String > impactedTargets ) throws IOException {
4749 Set <String > impactedTestTargets = new HashSet <>();
48- for (List <String > partition : Iterables .partition (impactedTargets , 100 )) {
49- String targetQuery = partition .stream ().collect (Collectors .joining (" + " ));
50- List <Build .Target > targets = performBazelQuery (String .format ("rdeps(//..., %s)" , targetQuery ));
51- for (Build .Target target : targets ) {
52- if (target .hasRule ()) {
53- impactedTestTargets .add (target .getRule ().getName ());
54- }
50+ String targetQuery = impactedTargets .stream ().collect (Collectors .joining (" + " ));
51+ List <Build .Target > targets = performBazelQuery (String .format ("rdeps(//..., %s)" , targetQuery ));
52+ for (Build .Target target : targets ) {
53+ if (target .hasRule ()) {
54+ impactedTestTargets .add (target .getRule ().getName ());
5555 }
5656 }
5757 return impactedTestTargets ;
@@ -60,13 +60,11 @@ public Set<String> queryForImpactedTargets(Set<String> impactedTargets) throws I
6060 @ Override
6161 public Set <String > queryForTestTargets (Set <String > targets ) throws IOException {
6262 Set <String > impactedTestTargets = new HashSet <>();
63- for (List <String > partition : Iterables .partition (targets , 100 )) {
64- String targetQuery = partition .stream ().collect (Collectors .joining (" + " ));
65- List <Build .Target > testTargets = performBazelQuery (String .format ("kind(test, %s)" , targetQuery ));
66- for (Build .Target target : testTargets ) {
67- if (target .hasRule ()) {
68- impactedTestTargets .add (target .getRule ().getName ());
69- }
63+ String targetQuery = targets .stream ().collect (Collectors .joining (" + " ));
64+ List <Build .Target > testTargets = performBazelQuery (String .format ("kind(test, %s)" , targetQuery ));
65+ for (Build .Target target : testTargets ) {
66+ if (target .hasRule ()) {
67+ impactedTestTargets .add (target .getRule ().getName ());
7068 }
7169 }
7270 return impactedTestTargets ;
@@ -101,8 +99,10 @@ public Set<BazelSourceFileTarget> convertFilepathsToSourceTargets(Set<Path> file
10199 }
102100
103101 private List <Build .Target > performBazelQuery (String query ) throws IOException {
102+ Path tempFile = Files .createTempFile (null , ".txt" );
103+ Files .write (tempFile , query .getBytes (StandardCharsets .UTF_8 ));
104+
104105 List <String > cmd = new ArrayList <String >();
105-
106106 cmd .add ((bazelPath .toString ()));
107107 cmd .addAll (this .startupOptions );
108108 cmd .add ("query" );
@@ -112,7 +112,8 @@ private List<Build.Target> performBazelQuery(String query) throws IOException {
112112 cmd .add ("--show_progress=false" );
113113 cmd .add ("--show_loading_progress=false" );
114114 cmd .addAll (this .commandOptions );
115- cmd .add (query );
115+ cmd .add ("--query_file" );
116+ cmd .add (tempFile .toString ());
116117
117118 ProcessBuilder pb = new ProcessBuilder (cmd ).directory (workingDirectory .toFile ());
118119 Process process = pb .start ();
@@ -122,6 +123,9 @@ private List<Build.Target> performBazelQuery(String query) throws IOException {
122123 if (target == null ) break ; // EOF
123124 targets .add (target );
124125 }
126+
127+ Files .delete (tempFile );
128+
125129 return targets ;
126130 }
127131}
0 commit comments