Skip to content

Commit 15975b6

Browse files
committed
Bazel: add support for DirectoryFileObject
Previously, the scip-java Bazel support only supported SimpleFileObject in the Java compiler API and it crashed when encountering other types of file objects like DirectoryFileObject, which are used in some Bazel builds in the wild. This commit adds support for DirectoryFileObject. Additionally, this commit adds more detailed documentation on how to use scip-java with Bazel.
1 parent d3e8f93 commit 15975b6

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

docs/getting-started.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,8 @@ projects, with the following caveats:
328328

329329
### Bazel
330330

331-
Bazel is supported by scip-java but it requires custom configuration to work
332-
correctly. Note that the `scip-java index` command does not automatically index
333-
Bazel builds.
331+
Bazel is supported by scip-java, but it requires custom configuration to work
332+
correctly. The `scip-java index` command does not automatically index Bazel builds.
334333

335334
The Bazel integration for scip-java is specifically designed to be compatible
336335
with the Bazel build cache to enable incremental indexing. To achieve this,
@@ -343,6 +342,38 @@ repository contains an example for how to configure everything.
343342
configured `java_library` and `java_binary` targets to be indexed with
344343
scip-java.
345344

345+
Once configured, build the codebase with the SemanticDB compiler plugin.
346+
```sh
347+
bazel build //... --@scip_java//semanticdb-javac:enabled=true
348+
```
349+
350+
Next, run the following command to generate the SCIP index (`index.scip`).
351+
352+
```
353+
bazel run @scip_java//scip-semanticdb:bazel -- --sourceroot $PWD
354+
355+
# (optional) Validate that SemanticDB files were generated.
356+
# The command below works for the `examples/bazel-example` directory in the sourcegraph/scip-java repository.
357+
❯ jar tf bazel-bin/src/main/java/example/libexample.jar | grep semanticdb$
358+
META-INF/semanticdb/src/main/java/example/Example.java.semanticdb
359+
```
360+
361+
Finally, run the following commands to upload the SCIP index to Sourcegraph.
362+
363+
```
364+
# 1. Install src
365+
npm install -g @sourcegraph/src # Or yarn global add @sourcegraph/src
366+
367+
# 2. Authenticate with Sourcegraph
368+
export SRC_ACCESS_TOKEN=sgp_YOUR_ACCESS_TOKEN
369+
export SRC_ENDPOINT=https://sourcegraph.example.com
370+
src login # validate the token authenticates correctly
371+
372+
# 3. Upload SCIP index to Sourcegraph
373+
src code-intel upload # requires index.scip file to exist
374+
```
375+
376+
346377
Don't hesitate to open an issue in the
347378
[scip-java repository](https://github.com/sourcegraph/scip-java) if you have any
348379
questions about using scip-java with Bazel builds.

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ public static Path absolutePathFromUri(SemanticdbJavacOptions options, JavaFileO
114114
} else if (options.uriScheme == UriScheme.BAZEL) {
115115
String toString = file.toString();
116116
// This solution is hacky, and it would be very nice to use a dedicated API instead.
117-
// The Bazel Java compiler constructs `SimpleFileObject` with a "user-friendly" name that
118-
// points to the original source file and an underlying/actual file path in a temporary
119-
// directory. We're constrained by having to use only public APIs of the Java compiler
120-
// and `toString()` seems to be the only way to access the user-friendly path.
121-
if (toString.startsWith("SimpleFileObject[") && toString.endsWith("]")) {
122-
return Paths.get(toString.substring("SimpleFileObject[".length(), toString.length() - 1));
123-
} else {
124-
throw new IllegalArgumentException("unsupported source file: " + toString);
117+
// The Bazel Java compiler constructs `SimpleFileObject/DirectoryFileObject` with a
118+
// "user-friendly" name that points to the original source file and an underlying/actual
119+
// file path in a temporary directory. We're constrained by having to use only public APIs of
120+
// the Java compiler and `toString()` seems to be the only way to access the user-friendly
121+
// path.
122+
String[] knownBazelToStringPatterns =
123+
new String[] {"SimpleFileObject[", "DirectoryFileObject["};
124+
for (String pattern : knownBazelToStringPatterns) {
125+
if (toString.startsWith(pattern) && toString.endsWith("]")) {
126+
return Paths.get(toString.substring(pattern.length(), toString.length() - 1));
127+
}
125128
}
129+
throw new IllegalArgumentException("unsupported source file: " + toString);
126130
} else {
127131
return Paths.get(uri);
128132
}

0 commit comments

Comments
 (0)