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 lsif-java command-line tool with Gradle and Maven support. #100

Merged
merged 9 commits into from
Mar 3, 2021

Conversation

olafurpg
Copy link
Member

@olafurpg olafurpg commented Mar 1, 2021

Previously, users needed to add manual configuration to their
Gradle/Maven builds in order to use lsif-java. Now, users can run the
new lsif-java command-line tool to automatically produce LSIF.

In order to automatically produce LSIF, lsif-java needs to compile all
the Java sources with an additional Java compiler options
-Xplugin:semanticdb. Most build tools support a way to add custom
compiler options but that typically requires updating build
configuration files such as pom.xml for Maven and build.gradle for
Gradle. However, we want to avoid updating build configuration files.

The exact solution to update the compilation options varies between
build tools:

  • Maven: you can customize the javac executable through the
    command-line flag mvn -Dmaven.compiler.executable=PATH compile
  • Gradle: you can pass in a custom Gradle script via
    gradle --init-script=PATH, which allows you to query information out
    of the build and configure settings. By default, lsif-java will try
    to configure the build to use a custom javac script similar to
    Maven. However, this approach does not work for Gradle builds that use
    the the newly released "Java toolchains" feature. For such builds, we
    can provide a custom java binary that registers a custom Java agent
    that ensures that Gradle always enabled the required
    -Xplugin:semanticdb flag.

Fixes #87
Fixes #56

olafurpg added 2 commits March 1, 2021 16:50
Anonymous class symbols can't escape a single compilation unit so they
can use local symbols according to the SemanticDB spec, and for
consistency with the Scala implementation.
Previously, users needed to add manual configuration to their
Gradle/Maven builds in order to use lsif-java. Now, users can run the
new `lsif-java` command-line tool to automatically produce LSIF.

In order to automatically produce LSIF, `lsif-java` needs to compile all
the Java sources with an additional Java compiler options
`-Xplugin:semanticdb`. Most build tools support a way to add custom
compiler options but that typically requires updating build
configuration files such as `pom.xml` for Maven and `build.gradle` for
Gradle. However, we want to avoid updating build configuration files.

The exact solution to update the compilation options varies between
build tools:

* Maven: you can customize the `javac` executable through the
  command-line flag `mvn -Dmaven.compiler.executable=PATH compile`
* Gradle: you can pass in a custom Gradle script via
  `gradle --init-script=PATH`, which allows you to query information out
  of the build and configure settings. By default, `lsif-java` will try
  to configure the build to use a custom `javac` script similar to
  Maven. However, this approach does not work for Gradle builds that use
  the the newly released "Java toolchains" feature.  For such builds, we
  can provide a custom `java` binary that registers a custom Java agent
  that ensures that Gradle always enabled the required
  `-Xplugin:semanticdb` flag.
@olafurpg olafurpg requested a review from Strum355 March 1, 2021 16:00
@olafurpg olafurpg marked this pull request as ready for review March 1, 2021 16:01
README.md Outdated
| Java 12 | Untested, may work |
| Java 13 | Untested, may work |
| Java 14 | Untested, may work |
| Java 15 | Untested, may work |
Copy link
Contributor

Choose a reason for hiding this comment

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

Works on Java 15, just not formally in the CI test, so Im assuming youre leaving it out for that reason?

Copy link
Member Author

Choose a reason for hiding this comment

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

Strictly speaking, the "Java 8" row should have the same message as Java 15. The next item on my agenda is to get cross-java CI working so this is only temporary.

s"-Porg.gradle.java.installations.paths=${toolchains.paths()}",
s"--no-daemon"
)
buildCommand ++= index.finalBuildCommand(List("clean", "compileTestJava"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious what this is for, why compileTestJava is hardcoded

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a better default command for gradle? I tried -x test build but it often skips compilation of test files and it also triggers integration tests. The user can override this command like this

lsif-java -- my custom gradle command

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah okay didn't know its overridable, seems fine then 🙂 Ive a feeling some of the android projects arent emitting semanticdb files due to using different compile task names, but Im not sure, would've thought build would catch it

Copy link
Member Author

Choose a reason for hiding this comment

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

The injected gradle settings specifically target the java plugin, which isn't used in android projects. I saw that the errorprone plugin had android specific logic, which we may need to learn from

olafurpg added 4 commits March 2, 2021 14:19
This makes it easy to open the workspace in Ubuntu to reproduce CI
issues locally.
The `lsif-semanticdb` binary was not available on PATH.
@olafurpg olafurpg force-pushed the build-tools branch 3 times, most recently from 0bce763 to 117ebbf Compare March 2, 2021 14:07
Previously, the build used the Java version that `sbt` was launched
with. Now, the build automatically downloads appropriate Java versions
for each project.

* Build by default with Java 11.
* Build compiler plugin with Java 8.
* Cross-build the "minimized" project for Java 8/11/15.
import java.nio.file.Paths;
import java.util.List;

public class InjectSemanticdbOptions {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for the sake of someone coming to read this in the future, it mightnt be a bad idea to briefly explain how this works. Its a fairly dense function 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! This is the hairiest part of the PR and it was definitely missing documentation. I was considering writing this logic in a bash script originally, but figured it would be easier to maintain as a small Java program.

Copy link
Contributor

@Strum355 Strum355 left a comment

Choose a reason for hiding this comment

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

Nothing glaring stands out, great work on this 👍 Time for me to really learn Scala now lol

@olafurpg olafurpg merged commit acff3fc into sourcegraph:main Mar 3, 2021
@olafurpg olafurpg deleted the build-tools branch March 3, 2021 15:31
This was referenced Mar 3, 2021
@Strum355 Strum355 added this to the Code Intelligence Sprint 7 milestone Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gradle support Maven support
2 participants