From 5db396245350d7035737ce40093abbd7292380b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Tue, 10 May 2022 11:12:49 +0200 Subject: [PATCH] Fix brittle code related to launching a Bazel process I saw flaky test failures caused by a false assumption in the code that the `bazel query` process has completed execution after reading stdout. We now wait for the process to complete instead of crashing the program. --- examples/bazel-example/.gitignore | 1 + .../lsif_semanticdb/BazelBuildTool.java | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) create mode 100644 examples/bazel-example/.gitignore diff --git a/examples/bazel-example/.gitignore b/examples/bazel-example/.gitignore new file mode 100644 index 00000000..ac41fd29 --- /dev/null +++ b/examples/bazel-example/.gitignore @@ -0,0 +1 @@ +bazel-bazel-example diff --git a/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/BazelBuildTool.java b/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/BazelBuildTool.java index a545013c..25891ee6 100644 --- a/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/BazelBuildTool.java +++ b/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/BazelBuildTool.java @@ -15,7 +15,7 @@ public class BazelBuildTool { - public static int runAndReturnExitCode(String[] args) throws IOException { + public static int runAndReturnExitCode(String[] args) throws IOException, InterruptedException { Optional maybeOptions = BazelOptions.parse(args); if (!maybeOptions.isPresent()) { return 1; @@ -56,7 +56,8 @@ public void error(Throwable e) { return 0; } - public static List mavenPackages(BazelOptions options) throws IOException { + public static List mavenPackages(BazelOptions options) + throws IOException, InterruptedException { ArrayList result = new ArrayList<>(); if (!options.isQueryMavenImports) { return result; @@ -109,22 +110,19 @@ public static List mavenPackages(BazelOptions options) throws IOEx } public static Bazelbuild.QueryResult runBazelQuery(BazelOptions options, String query) - throws IOException { + throws IOException, InterruptedException { List command = Arrays.asList(options.bazelBinary, "query", query, "--output=proto"); System.out.println("running: " + String.join(" ", command)); Process bazelQuery = new ProcessBuilder(command).directory(options.sourceroot.toFile()).start(); byte[] bytes = InputStreamBytes.readAll(bazelQuery.getInputStream()); - if (bazelQuery.isAlive()) { - throw new RuntimeException(new String(InputStreamBytes.readAll(bazelQuery.getErrorStream()))); - } - int exitValue = bazelQuery.exitValue(); + int exitValue = bazelQuery.waitFor(); if (exitValue != 0) { throw new RuntimeException("bazel command failed\n" + new String(bytes)); } return Bazelbuild.QueryResult.parseFrom(bytes); } - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, InterruptedException { System.exit(runAndReturnExitCode(args)); } }