Skip to content

Commit 42b1f11

Browse files
committed
Address review comments
1 parent 1f30836 commit 42b1f11

File tree

7 files changed

+45
-39
lines changed

7 files changed

+45
-39
lines changed

WORKSPACE

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ http_archive(
1616
##########
1717
# Protobuf
1818
##########
19+
# To update this version, copy-paste instructions from https://github.com/bazelbuild/rules_proto/releases
1920
http_archive(
2021
name = "rules_proto",
21-
sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
22-
strip_prefix = "rules_proto-4.0.0",
22+
sha256 = "e017528fd1c91c5a33f15493e3a398181a9e821a804eb7ff5acdd1d2d6c2b18d",
23+
strip_prefix = "rules_proto-4.0.0-3.20.0",
2324
urls = [
24-
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
25-
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
25+
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0-3.20.0.tar.gz",
2626
],
2727
)
2828
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

examples/bazel-example/WORKSPACE

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ local_repository(
2727
##########
2828
# Protobuf
2929
##########
30+
# To update this version, copy-paste instructions from https://github.com/bazelbuild/rules_proto/releases
3031
http_archive(
3132
name = "rules_proto",
32-
sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
33-
strip_prefix = "rules_proto-4.0.0",
33+
sha256 = "e017528fd1c91c5a33f15493e3a398181a9e821a804eb7ff5acdd1d2d6c2b18d",
34+
strip_prefix = "rules_proto-4.0.0-3.20.0",
3435
urls = [
35-
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
36-
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
36+
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0-3.20.0.tar.gz",
3737
],
3838
)
3939
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

examples/bazel-example/bazel-bazel-example

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
public class BazelBuildTool {
1717

18-
1918
public static int runAndReturnExitCode(String[] args) throws IOException {
2019
Optional<BazelOptions> maybeOptions = BazelOptions.parse(args);
2120
if (!maybeOptions.isPresent()) {
@@ -57,8 +56,7 @@ public void error(Throwable e) {
5756
return 0;
5857
}
5958

60-
public static List<MavenPackage> mavenPackages(BazelOptions options)
61-
throws IOException {
59+
public static List<MavenPackage> mavenPackages(BazelOptions options) throws IOException {
6260
ArrayList<MavenPackage> result = new ArrayList<>();
6361
if (!options.isQueryMavenImports) {
6462
return result;
@@ -89,7 +87,7 @@ public static List<MavenPackage> mavenPackages(BazelOptions options)
8987
String[] parts = tag.substring("maven_coordinates=".length()).split(":");
9088
if (parts.length == 3) {
9189
// The jar part is populated via `withJar()` below.
92-
basePackage = new MavenPackage(/* jar = */null, parts[0], parts[1], parts[2]);
90+
basePackage = new MavenPackage(/* jar = */ null, parts[0], parts[1], parts[2]);
9391
}
9492
}
9593
}
@@ -114,10 +112,7 @@ public static Bazelbuild.QueryResult runBazelQuery(BazelOptions options, String
114112
throws IOException {
115113
List<String> command = Arrays.asList(options.bazelBinary, "query", query, "--output=proto");
116114
System.out.println("running: " + String.join(" ", command));
117-
Process bazelQuery =
118-
new ProcessBuilder(command)
119-
.directory(options.sourceroot.toFile())
120-
.start();
115+
Process bazelQuery = new ProcessBuilder(command).directory(options.sourceroot.toFile()).start();
121116
byte[] bytes = InputStreamBytes.readAll(bazelQuery.getInputStream());
122117
if (bazelQuery.isAlive()) {
123118
throw new RuntimeException(new String(InputStreamBytes.readAll(bazelQuery.getErrorStream())));

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ public static void printHelp() {
2020
System.out.println();
2121
System.out.println("Command-line tool to generate LSIF for Java targets in a Bazel build.");
2222
System.out.println(
23-
"The idiomatic way to run this tool is to compile it from source via Bazel\n"
24-
+ "and invoke it through `bazel run ...`.");
23+
"The idiomatic way to run this tool is to compile it from source via Bazel\n"
24+
+ "and invoke it through `bazel run ...`.");
2525
System.out.println();
2626
System.out.println("OPTIONS:");
2727
System.out.println(
28-
" --sourceroot <path> the absolute path to the root directory of the Bazel codebase");
28+
" --sourceroot <path> the absolute path to the root directory of the Bazel codebase");
2929
System.out.println(
30-
" TIP: use --sourceroot $PWD to pass the current working directory");
30+
" TIP: use --sourceroot $PWD to pass the current working directory");
3131
System.out.println(" --output <path> the absolute path to the file that should be generated");
3232
System.out.println(" --parallel whether to process files in parallel");
33-
System.out.println(" --no-query-maven-imports whether to skip the `bazel query @maven//...` phase.");
33+
System.out.println(
34+
" --no-query-maven-imports whether to skip the `bazel query @maven//...` phase.");
3435
}
3536

3637
public static Optional<BazelOptions> parse(String[] args) throws IOException {
@@ -93,18 +94,20 @@ public static Optional<BazelOptions> parse(String[] args) throws IOException {
9394

9495
if (options.sourceroot == null) {
9596
if (args.length == 0) {
96-
errors.add("missing required flag --sourceroot <path>. To fix this problem, pass in the `--sourceroot` flag like this: bazel run @lsif_java//lsif-semanticdb:bazel -- --sourceroot $PWD");
97+
errors.add(
98+
"missing required flag --sourceroot <path>. To fix this problem, pass in the `--sourceroot` flag like this: bazel run @lsif_java//lsif-semanticdb:bazel -- --sourceroot $PWD");
9799
} else {
98-
errors.add("missing required flag --sourceroot <path>");
100+
errors.add("missing required flag --sourceroot <path>");
99101
}
100102
} else if (!options.sourceroot.isAbsolute()) {
101103
// result.sourceroot must be an absolute path because `System.getProperty("user.dir")` is a
102104
// temporary directory that's generated by Bazel.
103105
errors.add(
104-
String.format("relative path --sourceroot '%s'. To fix this problem, pass in an an absolute path.", options.sourceroot));
106+
String.format(
107+
"relative path --sourceroot '%s'. To fix this problem, pass in an an absolute path.",
108+
options.sourceroot));
105109
} else if (!Files.isDirectory(options.sourceroot)) {
106-
errors.add(
107-
String.format("not a directory --sourceroot '%s'", options.sourceroot));
110+
errors.add(String.format("not a directory --sourceroot '%s'", options.sourceroot));
108111
}
109112

110113
if (options.output == null) {

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbJavacOptions.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class SemanticdbJavacOptions {
2323
public boolean includeText = false;
2424
public boolean verboseEnabled = false;
2525
public final ArrayList<String> errors;
26-
public boolean isErrorReported = false;
26+
public boolean alreadyReportedErrors = false;
2727
public UriScheme uriScheme = UriScheme.DEFAULT;
2828

2929
public static String stubClassName = "META-INF-semanticdb-stub";
@@ -78,7 +78,7 @@ public static SemanticdbJavacOptions parse(String[] args, Context ctx) {
7878
if (result.targetroot == null && !useJavacClassesDir) {
7979
result.errors.add(missingRequiredDirectoryOption("targetroot"));
8080
}
81-
if (result.uriScheme != UriScheme.BAZEL && result.sourceroot == null) {
81+
if (!isSourcerootDefined(result)) {
8282
// When using -build-tool:bazel, the sourceroot is automatically inferred from the first
8383
// compilation unit.
8484
// See `SemanticdbTaskListener.inferBazelSourceroot()` for the method that infers the
@@ -88,6 +88,13 @@ public static SemanticdbJavacOptions parse(String[] args, Context ctx) {
8888
return result;
8989
}
9090

91+
private static boolean isSourcerootDefined(SemanticdbJavacOptions options) {
92+
if (options.uriScheme == UriScheme.BAZEL) {
93+
return true; // The sourceroot is automatically inferred for Bazel.
94+
}
95+
return options.sourceroot != null;
96+
}
97+
9198
private static Path getJavacClassesDir(SemanticdbJavacOptions result, Context ctx) {
9299
// I'm not aware of a better way to get the class output directory from javac
93100
Path outputDir = null;

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.sun.source.util.TaskListener;
66
import com.sun.source.util.Trees;
77
import com.sun.tools.javac.model.JavacTypes;
8-
import com.sun.tools.javac.util.Options;
98

109
import javax.tools.Diagnostic;
1110
import javax.tools.JavaFileObject;
@@ -48,8 +47,8 @@ public void started(TaskEvent e) {}
4847
public void finished(TaskEvent e) {
4948
if (e.getKind() != TaskEvent.Kind.ANALYZE) return;
5049
if (!options.errors.isEmpty()) {
51-
if (!options.isErrorReported) {
52-
options.isErrorReported = true;
50+
if (!options.alreadyReportedErrors) {
51+
options.alreadyReportedErrors = true;
5352
for (String error : options.errors) {
5453
trees.printMessage(
5554
Diagnostic.Kind.ERROR,
@@ -129,7 +128,8 @@ public static Path absolutePathFromUri(SemanticdbJavacOptions options, JavaFileO
129128
}
130129
}
131130

132-
// Infers the `-sourceroot:` flag from the provided file
131+
// Infers the `-sourceroot:` flag from the provided file.
132+
// FIXME: add unit tests https://github.com/sourcegraph/lsif-java/issues/444
133133
private void inferBazelSourceroot(JavaFileObject file) {
134134
if (options.uriScheme != UriScheme.BAZEL || options.sourceroot != null) {
135135
return;
@@ -141,12 +141,14 @@ private void inferBazelSourceroot(JavaFileObject file) {
141141
// uriPath is the sandbox/temporary file path, for example
142142
// /private/var/tmp/com/example/Hello.java
143143
//
144-
// We infer sourceroot by iterating the names of both files in reverse order and stop at the
145-
// first entry where the two paths are different. For the example above, we compare
146-
// "Hello.java", then "example", then "com", and when we reach "repo" !+ "tmp" then we guess
147-
// that "/home/repo" is the sourceroot. This logic is brittle and it would be nice to use more
148-
// dedicated APIs, but Bazel actively makes an effort to sandbox compilation and hide access
149-
// to the original workspace, which is why we resort to solutions like this.
144+
// We infer sourceroot by iterating the names of both files in reverse order
145+
// and stop at the first entry where the two paths are different. For the
146+
// example above, we compare "Hello.java", then "example", then "com", and
147+
// when we reach "repo" != "tmp" then we guess that "/home/repo" is the
148+
// sourceroot. This logic is brittle and it would be nice to use more
149+
// dedicated APIs, but Bazel actively makes an effort to sandbox
150+
// compilation and hide access to the original workspace, which is why we
151+
// resort to solutions like this.
150152
int relativePathDepth = 0;
151153
int uriPathDepth = uriPath.getNameCount();
152154
int absolutePathDepth = absolutePath.getNameCount();

0 commit comments

Comments
 (0)