Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --keep_going to make Bazel support more robust #288

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TODOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Select entire name, find references => no results
- Crashes if maven is not installed
- Homebrew users don't have src.zip, detect java version and download the appropriate src.zip
- Temporary files created for Bazel command output are not cleaned up.

## Optimizations
- Compilation is very slow in the presence of lots of errors
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/org/javacs/InferConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private Path bazelOutputBase(Path bazelWorkspaceRoot) {
String[] command = {
"bazel", "info", "output_base",
};
var output = fork(bazelWorkspaceRoot, command);
var output = fork(bazelWorkspaceRoot, command, false);
if (output == NOT_FOUND) {
return NOT_FOUND;
}
Expand All @@ -319,20 +319,21 @@ private void bazelDryRunBuild(Path bazelWorkspaceRoot, Set<String> targets) {
var command = new ArrayList<String>();
command.add("bazel");
command.add("build");
command.add("--keep_going");
command.add("--nobuild");
command.addAll(targets);
String[] c = new String[command.size()];
c = command.toArray(c);
var output = fork(bazelWorkspaceRoot, c);
var output = fork(bazelWorkspaceRoot, c, true);
if (output == NOT_FOUND) {
return;
}
return;
}

private Set<String> bazelQuery(Path bazelWorkspaceRoot, String filterKind) {
String[] command = {"bazel", "query", "kind(" + filterKind + ",//...)"};
var output = fork(bazelWorkspaceRoot, command);
String[] command = {"bazel", "query", "--keep_going", "kind(" + filterKind + ",//...)"};
var output = fork(bazelWorkspaceRoot, command, true);
if (output == NOT_FOUND) {
return Set.of();
}
Expand Down Expand Up @@ -366,13 +367,14 @@ private Set<String> bazelAQuery(
String[] command = {
"bazel",
"aquery",
"--keep_going",
"--output=proto",
"--include_aspects", // required for java_proto_library, see
// https://stackoverflow.com/questions/63430530/bazel-aquery-returns-no-action-information-for-java-proto-library
"--allow_analysis_failures",
"mnemonic(" + filterMnemonic + ", " + kindUnion + ")"
};
var output = fork(bazelWorkspaceRoot, command);
var output = fork(bazelWorkspaceRoot, command, true);
if (output == NOT_FOUND) {
return Set.of();
}
Expand Down Expand Up @@ -474,7 +476,7 @@ private static String buildPath(List<PathFragment> fragments, int id) {
throw new RuntimeException();
}

private static Path fork(Path workspaceRoot, String[] command) {
private static Path fork(Path workspaceRoot, String[] command, boolean allowNonZeroExit) {
try {
LOG.info("Running " + String.join(" ", command) + " ...");
var output = Files.createTempFile("java-language-server-bazel-output", ".proto");
Expand All @@ -489,7 +491,9 @@ private static Path fork(Path workspaceRoot, String[] command) {
var result = process.waitFor();
if (result != 0) {
LOG.severe("`" + String.join(" ", command) + "` returned " + result);
return NOT_FOUND;
if (!allowNonZeroExit) {
return NOT_FOUND;
}
}
return output;
} catch (InterruptedException | IOException e) {
Expand Down
1 change: 1 addition & 0 deletions src/test/examples/bazel-project-broken/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.2.4
1 change: 1 addition & 0 deletions src/test/examples/bazel-project-broken/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
32 changes: 32 additions & 0 deletions src/test/examples/bazel-project-broken/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "4.2"
RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca"

http_archive(
name = "rules_jvm_external",
sha256 = RULES_JVM_EXTERNAL_SHA,
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()

load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")

rules_jvm_external_setup()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"com.google.guava:guava:18.0",
],
repositories = [
"https://jcenter.bintray.com/",
"https://repo1.maven.org/maven2",
],
fetch_sources = True,
)
1 change: 1 addition & 0 deletions src/test/examples/bazel-project-broken/broken/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fail("intentionally broken")
9 changes: 9 additions & 0 deletions src/test/examples/bazel-project-broken/hello/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
java_binary(
name = "main",
srcs = glob(["src/**/*.java"]),
main_class = "Hello",
visibility = ["//visibility:public"],
deps = [
"@maven//:com_google_guava_guava",
],
)
5 changes: 5 additions & 0 deletions src/test/examples/bazel-project-broken/hello/src/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
1 change: 1 addition & 0 deletions src/test/examples/bazel-project/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.2.4
1 change: 1 addition & 0 deletions src/test/examples/bazel-protos-project/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.2.4
6 changes: 6 additions & 0 deletions src/test/java/org/javacs/InferBazelConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public void bazelClassPathWithProtos() {
assertThat(bazel.classPath(), hasItem(hasToString(endsWith("libperson_proto-speed.jar"))));
}

@Test
public void bazelClassPathBrokenProject() {
var bazel = new InferConfig(Paths.get("src/test/examples/bazel-project-broken"));
assertThat(bazel.classPath(), contains(hasToString(endsWith("guava-18.0.jar"))));
}

@Test
public void bazelDocPath() {
var bazel = new InferConfig(Paths.get("src/test/examples/bazel-project"));
Expand Down