Skip to content

Commit 51a8034

Browse files
committed
bazel: keep inferring classpath from broken workspaces
1 parent 38840e8 commit 51a8034

12 files changed

Lines changed: 86 additions & 7 deletions

File tree

TODOS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- Select entire name, find references => no results
2121
- Crashes if maven is not installed
2222
- Homebrew users don't have src.zip, detect java version and download the appropriate src.zip
23+
- Temporary files created for Bazel command output are not cleaned up.
2324

2425
## Optimizations
2526
- Compilation is very slow in the presence of lots of errors

src/main/java/org/javacs/InferConfig.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ private Path bazelOutputBase(Path bazelWorkspaceRoot) {
337337
String[] command = {
338338
"bazel", "info", "output_base",
339339
};
340-
var output = fork(bazelWorkspaceRoot, command);
340+
var output = fork(bazelWorkspaceRoot, command, false);
341341
if (output == NOT_FOUND) {
342342
return NOT_FOUND;
343343
}
@@ -354,20 +354,21 @@ private void bazelDryRunBuild(Path bazelWorkspaceRoot, Set<String> targets) {
354354
var command = new ArrayList<String>();
355355
command.add("bazel");
356356
command.add("build");
357+
command.add("--keep_going");
357358
command.add("--nobuild");
358359
command.addAll(targets);
359360
String[] c = new String[command.size()];
360361
c = command.toArray(c);
361-
var output = fork(bazelWorkspaceRoot, c);
362+
var output = fork(bazelWorkspaceRoot, c, true);
362363
if (output == NOT_FOUND) {
363364
return;
364365
}
365366
return;
366367
}
367368

368369
private Set<String> bazelQuery(Path bazelWorkspaceRoot, String filterKind) {
369-
String[] command = {"bazel", "query", "kind(" + filterKind + ",//...)"};
370-
var output = fork(bazelWorkspaceRoot, command);
370+
String[] command = {"bazel", "query", "--keep_going", "kind(" + filterKind + ",//...)"};
371+
var output = fork(bazelWorkspaceRoot, command, true);
371372
if (output == NOT_FOUND) {
372373
return Set.of();
373374
}
@@ -401,13 +402,14 @@ private Set<String> bazelAQuery(
401402
String[] command = {
402403
"bazel",
403404
"aquery",
405+
"--keep_going",
404406
"--output=proto",
405407
"--include_aspects", // required for java_proto_library, see
406408
// https://stackoverflow.com/questions/63430530/bazel-aquery-returns-no-action-information-for-java-proto-library
407409
"--allow_analysis_failures",
408410
"mnemonic(" + filterMnemonic + ", " + kindUnion + ")"
409411
};
410-
var output = fork(bazelWorkspaceRoot, command);
412+
var output = fork(bazelWorkspaceRoot, command, true);
411413
if (output == NOT_FOUND) {
412414
return Set.of();
413415
}
@@ -509,7 +511,7 @@ private static String buildPath(List<PathFragment> fragments, int id) {
509511
throw new RuntimeException();
510512
}
511513

512-
private static Path fork(Path workspaceRoot, String[] command) {
514+
private static Path fork(Path workspaceRoot, String[] command, boolean allowNonZeroExit) {
513515
try {
514516
LOG.info("Running " + String.join(" ", command) + " ...");
515517
var output = Files.createTempFile("java-language-server-bazel-output", ".proto");
@@ -524,7 +526,9 @@ private static Path fork(Path workspaceRoot, String[] command) {
524526
var result = process.waitFor();
525527
if (result != 0) {
526528
LOG.severe("`" + String.join(" ", command) + "` returned " + result);
527-
return NOT_FOUND;
529+
if (!allowNonZeroExit) {
530+
return NOT_FOUND;
531+
}
528532
}
529533
return output;
530534
} catch (InterruptedException | IOException e) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.2.4
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
RULES_JVM_EXTERNAL_TAG = "4.2"
4+
RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca"
5+
6+
http_archive(
7+
name = "rules_jvm_external",
8+
sha256 = RULES_JVM_EXTERNAL_SHA,
9+
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
10+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
11+
)
12+
13+
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
14+
15+
rules_jvm_external_deps()
16+
17+
load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
18+
19+
rules_jvm_external_setup()
20+
21+
load("@rules_jvm_external//:defs.bzl", "maven_install")
22+
23+
maven_install(
24+
artifacts = [
25+
"com.google.guava:guava:18.0",
26+
],
27+
repositories = [
28+
"https://jcenter.bintray.com/",
29+
"https://repo1.maven.org/maven2",
30+
],
31+
fetch_sources = True,
32+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fail("intentionally broken")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
java_binary(
2+
name = "main",
3+
srcs = glob(["src/**/*.java"]),
4+
main_class = "Hello",
5+
visibility = ["//visibility:public"],
6+
deps = [
7+
"@maven//:com_google_guava_guava",
8+
],
9+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Hello {
2+
public static void main(String[] args) {
3+
System.out.println("Hello, world!");
4+
}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.2.4
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.2.4

0 commit comments

Comments
 (0)