Skip to content
Merged
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
37 changes: 34 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,8 @@ projects, with the following caveats:

### Bazel

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

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

Once configured, build the codebase with the SemanticDB compiler plugin.
```sh
bazel build //... --@scip_java//semanticdb-javac:enabled=true
```

Next, run the following command to generate the SCIP index (`index.scip`).

```
bazel run @scip_java//scip-semanticdb:bazel -- --sourceroot $PWD

# (optional) Validate that SemanticDB files were generated.
# The command below works for the `examples/bazel-example` directory in the sourcegraph/scip-java repository.
❯ jar tf bazel-bin/src/main/java/example/libexample.jar | grep semanticdb$
META-INF/semanticdb/src/main/java/example/Example.java.semanticdb
```

Finally, run the following commands to upload the SCIP index to Sourcegraph.

```
# 1. Install src
npm install -g @sourcegraph/src # Or yarn global add @sourcegraph/src

# 2. Authenticate with Sourcegraph
export SRC_ACCESS_TOKEN=sgp_YOUR_ACCESS_TOKEN
export SRC_ENDPOINT=https://sourcegraph.example.com
src login # validate the token authenticates correctly

# 3. Upload SCIP index to Sourcegraph
src code-intel upload # requires index.scip file to exist
```


Don't hesitate to open an issue in the
[scip-java repository](https://github.com/sourcegraph/scip-java) if you have any
questions about using scip-java with Bazel builds.
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ public static Path absolutePathFromUri(SemanticdbJavacOptions options, JavaFileO
} else if (options.uriScheme == UriScheme.BAZEL) {
String toString = file.toString();
// This solution is hacky, and it would be very nice to use a dedicated API instead.
// The Bazel Java compiler constructs `SimpleFileObject` with a "user-friendly" name that
// points to the original source file and an underlying/actual file path in a temporary
// directory. We're constrained by having to use only public APIs of the Java compiler
// and `toString()` seems to be the only way to access the user-friendly path.
if (toString.startsWith("SimpleFileObject[") && toString.endsWith("]")) {
return Paths.get(toString.substring("SimpleFileObject[".length(), toString.length() - 1));
} else {
throw new IllegalArgumentException("unsupported source file: " + toString);
// The Bazel Java compiler constructs `SimpleFileObject/DirectoryFileObject` with a
// "user-friendly" name that points to the original source file and an underlying/actual
// file path in a temporary directory. We're constrained by having to use only public APIs of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're constrained by having to use only public APIs of the Java compiler

Is this a bazel restriction? I dont think this is true for scip-java, we almost definitely use non public parts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bazel restriction. We try to limit non-public API usage as much as possible.

// the Java compiler and `toString()` seems to be the only way to access the user-friendly
// path.
String[] knownBazelToStringPatterns =
new String[] {"SimpleFileObject[", "DirectoryFileObject["};
for (String pattern : knownBazelToStringPatterns) {
if (toString.startsWith(pattern) && toString.endsWith("]")) {
return Paths.get(toString.substring(pattern.length(), toString.length() - 1));
}
}
throw new IllegalArgumentException("unsupported source file: " + toString);
} else {
return Paths.get(uri);
}
Expand Down