Bazel support

Detailed information on how the Moderne CLI interacts with the Bazel build tool.

Moderne CLI interacts with the Bazel build tool to parse repositories into the Lossless Semantic Tree (LST). Bazel build files are discovered automatically by mod build in the same way other build tools like Maven or Gradle are discovered.

Moderne CLI will execute Bazel with a bazelisk wrapper if present, otherwise falling back on a PATH installed Bazel executable.

Configuring the Bazel rule to use

By default the CLI executes the Bazel rule java-maven-lib, which is likely not the rule that you need.

CommandDescription

Globally sets the rule for all Bazel repositories that don't have a rule configured explicitly.

mod config build bazel rule edit <RULE> --local=<REPO_DIR>

Sets the rule for the repository (or repositories) rooted at <REPO_DIR> . This overrides the global rule configuration for this repo. The configuration is saved in an implicitly gitignored file so isn't seen as a tracked change to git.

mod config build bazel rule edit <RULE> --local=<REPO_DIR> --save

Sets the rule for the repository (or repositories) rooted at <REPO_DIR> . This overrides the global rule configuration for this repo. The configuration is saved in a .modernecfg file that is seen as a tracked change to git, and can be committed to source control for others to benefit from the configuration with their own CLIs.

Example Bazel repository

Moderne maintains a public example Bazel repository.

  1. Clone https://github.com/moderneinc/bazel-examples and cd into the directory containing the cloned repository.

  2. Execute mod build .

  3. Examine the .moderne/build/<DATE_TIME_HASH>/manifest.csv file to see that the CLI correctly identified Java source files as such and parsed them into J.CompilationUnit.

n,sourcePath,sourceFileType,buildTool,buildToolVersion,checksum,lineCount
1,"LICENSE",Quark,mod,3.6.2,86d3f3a95c324c9479bd8986968f4327,0
2,"README.md",PlainText,mod,3.6.2,9806770b48fee4ff1a0eb2bce1c66176,2
3,"bazel-java-maven/.gitignore",PlainText,mod,3.6.2,a8edf8bbdab71497f7cd11360a582a86,5
...
10,"bazel-java-maven/src/main/java/com/example/myproject/App.java",,mod,3.6.2,4b01908c328578022630cccdb1719d04,0
11,"bazel-java-maven/src/test/java/com/example/myproject/TestApp.java",,mod,3.6.2,b8b7619c3321903bd9549b4f0b8bfd88,0

Troubleshooting: Repositories that have both Bazel build files and another build tool like Gradle configured at the same folder level

The Moderne CLI will choose build tools in the following order of precedence:

  1. Maven

  2. Gradle

  3. Bazel

The presence of a build file for one of these tools prevents a subsequent build tool from being considered for that folder.

Troubleshooting: What Bazel command did the Moderne CLI execute to produce the LST?

The Bazel command that the Moderne CLI is executing is logged at this path relative to the root of the repository being parsed.

.moderne/build/<DATA_TIME_HASH>/build.log

Notice that the rule that has been configured with mod config build bazel rule edit <RULE> is being used here.

Troubleshooting: Why were Java files not parsed as Java LSTs?

At the end of build, a manifest is created that itemizes each file that is contained in the LST and how it was parsed. A sure sign that the Bazel rule configuration is not correctly identfying Java source sets is a series of Java file entries in the manifest that are listed as "Quarks", as seen in the image below.

A quark is an LST type that simply indicates the presence of a file, but does not keep its contents. Java-specific recipes will not operate on Quarks to make changes.

The Moderne CLI augments the build with an Aspect that emits information on matching rules into a file in bazel-out/<ARCH>/bin/<RULE>.compilation_info.json.

The JSON structure is easily human readable, and gives insight into the Moderne CLI's understanding of the Java source files that the rule is responsible for and the classpath that should be used to parse those source files.

Last updated