Skip to content

Commit 6fb8a78

Browse files
authored
Merge pull request #448 from sourcegraph/olafurpg/bazel-flake
Fix brittle code related to launching a Bazel process
2 parents fd79d2a + 5db3962 commit 6fb8a78

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

examples/bazel-example/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-bazel-example

lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/BazelBuildTool.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
public class BazelBuildTool {
1717

18-
public static int runAndReturnExitCode(String[] args) throws IOException {
18+
public static int runAndReturnExitCode(String[] args) throws IOException, InterruptedException {
1919
Optional<BazelOptions> maybeOptions = BazelOptions.parse(args);
2020
if (!maybeOptions.isPresent()) {
2121
return 1;
@@ -56,7 +56,8 @@ public void error(Throwable e) {
5656
return 0;
5757
}
5858

59-
public static List<MavenPackage> mavenPackages(BazelOptions options) throws IOException {
59+
public static List<MavenPackage> mavenPackages(BazelOptions options)
60+
throws IOException, InterruptedException {
6061
ArrayList<MavenPackage> result = new ArrayList<>();
6162
if (!options.isQueryMavenImports) {
6263
return result;
@@ -109,22 +110,19 @@ public static List<MavenPackage> mavenPackages(BazelOptions options) throws IOEx
109110
}
110111

111112
public static Bazelbuild.QueryResult runBazelQuery(BazelOptions options, String query)
112-
throws IOException {
113+
throws IOException, InterruptedException {
113114
List<String> command = Arrays.asList(options.bazelBinary, "query", query, "--output=proto");
114115
System.out.println("running: " + String.join(" ", command));
115116
Process bazelQuery = new ProcessBuilder(command).directory(options.sourceroot.toFile()).start();
116117
byte[] bytes = InputStreamBytes.readAll(bazelQuery.getInputStream());
117-
if (bazelQuery.isAlive()) {
118-
throw new RuntimeException(new String(InputStreamBytes.readAll(bazelQuery.getErrorStream())));
119-
}
120-
int exitValue = bazelQuery.exitValue();
118+
int exitValue = bazelQuery.waitFor();
121119
if (exitValue != 0) {
122120
throw new RuntimeException("bazel command failed\n" + new String(bytes));
123121
}
124122
return Bazelbuild.QueryResult.parseFrom(bytes);
125123
}
126124

127-
public static void main(String[] args) throws IOException {
125+
public static void main(String[] args) throws IOException, InterruptedException {
128126
System.exit(runAndReturnExitCode(args));
129127
}
130128
}

0 commit comments

Comments
 (0)