Skip to content

Bazel compilation changes for unused dependencies checking - #30808

Closed
JonathanPerry651 wants to merge 2 commits into
bazelbuild:masterfrom
JonathanPerry651:unused-deps-bazel
Closed

Bazel compilation changes for unused dependencies checking#30808
JonathanPerry651 wants to merge 2 commits into
bazelbuild:masterfrom
JonathanPerry651:unused-deps-bazel

Conversation

@JonathanPerry651

Copy link
Copy Markdown
Contributor

This pull request contains the Bazel-side changes for unused dependencies checking, splitting the functionality as requested by @hvadehra in #29770. This PR requires the JavaBuilder-side changes from #30807.

@JonathanPerry651
JonathanPerry651 requested review from a team and lberki as code owners August 20, 2026 09:15
@JonathanPerry651
JonathanPerry651 requested review from aranguyen and removed request for a team August 20, 2026 09:15
@github-actions github-actions Bot added team-Configurability platforms, toolchains, cquery, select(), config transitions team-Rules-Java Issues for Java rules awaiting-review PR is awaiting review from an assigned reviewer team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts labels Aug 20, 2026
@JonathanPerry651
JonathanPerry651 requested a review from a team as a code owner August 20, 2026 09:21
@bazel-docs-pr-commenter

bazel-docs-pr-commenter Bot commented Aug 20, 2026

Copy link
Copy Markdown

✅ Bazel docs preview is ready!

Preview URL: https://bazel-pr-30808.mintlify.app/

Updated for 487d3eb8d7d87662069c2b5c609c6878e280be9a

@hvadehra hvadehra removed team-Configurability platforms, toolchains, cquery, select(), config transitions team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts labels Aug 21, 2026
@hvadehra
hvadehra requested review from hvadehra and removed request for aranguyen and lberki August 21, 2026 08:14
@hvadehra

Copy link
Copy Markdown
Member

Please rebase after 61218bb

Also, why does this not match the latest state of #29770 ?

@JonathanPerry651

Copy link
Copy Markdown
Contributor Author

Please rebase after 61218bb

On it.

Also, why does this not match the latest state of #29770 ?

Yeah I was about to post on that - I took the PRs and actually tried them out against my repo, and discovered that the code that i thought was testing the behaviour in fact was not, since it was short-circuiting when it discovered that the functionality wasn't supported! So I fixed the tests to not short circuit, and then fixed the code to...actually work.

Very sorry for the weirdness!

@JonathanPerry651

Copy link
Copy Markdown
Contributor Author

@hvadehra - rebase is done, everything looks clean

@hvadehra

Copy link
Copy Markdown
Member

What about the discussion under #29770 (comment) where we decided to make Bazel unaware of any of these changes and just pass through the args (as you implemented in d1c7648)?

@JonathanPerry651

Copy link
Copy Markdown
Contributor Author

I am so sorry, I got in a git tangle. Let me fix and get back to you.

@JonathanPerry651

Copy link
Copy Markdown
Contributor Author

@hvadehra - OK I unborked myself, sorry again for my silliness.

There's code in the tests which shortcuts the new functionality if the referenced rules_java doesn't contain its side of the code, or else (for the internal test) shortcuts if the packaged JavaBuilder is the 'released' version. I don't feel super comfortable that I've gotten that right, especially given that I got it wrong before (which is why the code I had previously was totally borked :( )

for (CommandLine extraCommandLine : extraCommandLineArgs) {
commandLinesBuilder.addCommandLine(extraCommandLine, PARAM_FILE_INFO);
}
commandLinesBuilder.addCommandLine(classpathLine.build(), PARAM_FILE_INFO);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it important that the classpath line is added last?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No good reason; cleaned up and moved extraCommandLineArgs to the end.

name = "is_unused_deps_supported",
documented = false,
useStarlarkThread = true)
default boolean isUnusedDepsSupported(StarlarkThread thread) throws EvalException {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't need this API, we can derive this from the Bazel version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point — removed is_unused_deps_supported from JavaCommonApi, JavaStarlarkCommon, and java_common.bzl.

? ImmutableList.of()
: Sequence.cast(extraArgs, CommandLineArgsApi.class, "extra_args");
ImmutableList.Builder<CommandLine> extraCommandLineArgs = ImmutableList.builder();
ImmutableSet.Builder<Artifact> extraDirectoryInputs = ImmutableSet.builder();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need this special handling of extraDirectoryInputs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed extraDirectoryInputs. Since create_compilation_action already provides an additional_inputs parameter for any input artifacts, extra_args only needs to forward the CommandLine objects.

.manifestProto(manifestProto)
.build();
Iterable<CommandLineArgsApi> extraArgsList =
(extraArgs == null || extraArgs == Starlark.NONE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could be simpler if we make the default value [].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yup, done

Iterable<CommandLineArgsApi> extraArgsList =
(extraArgs == null || extraArgs == Starlark.NONE)
? ImmutableList.of()
: Sequence.cast(extraArgs, CommandLineArgsApi.class, "extra_args");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lets just Sequence.cast() to Args.class and simplify?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yup, done

}

@CanIgnoreReturnValue
public Builder addAdditionalInputs(Iterable<Artifact> additionalInputs) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed thanks

.addSourceJars(Sequence.cast(sourceJars, Artifact.class, "source_jars"))
.addSourceFiles(Depset.noneableCast(sourceFiles, Artifact.class, "sources").toList())
.addDirectJars(directJars.getSet(Artifact.class))
.addExtraCommandLineArgs(extraCommandLineArgs.build())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Passing this indirectly via JavaTargetsAttributes seems unnecessary. Should we just add it to JavaCompilationHelper instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea, moved extraCommandLineArgs directly to JavaCompilationHelper and removed it from JavaTargetAttributes.

}

@Test
public void testUnusedDepsVerifyFlags() throws Exception {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

from Bazel's perspective, we're just passing through any extra Args supplied from Starlark. Lets rename + simplify the test to just ensure any extra --foo=bar type arguments get propagated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Simplified and renamed test to check --foo=bar propagation.

}

@Test
public void testExtraArgs_invalidElementTypeThrows() throws Exception {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unnecessary - this is exercising Bazel's Starlark builtin machinery, it has nothing to do with java compilation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@github-actions github-actions Bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Aug 27, 2026
@JonathanPerry651

Copy link
Copy Markdown
Contributor Author

@bazel-io flag

Please consider cherry-picking this change into Bazel 9.3.0.

@fmeum

fmeum commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

@bazel-io fork 9.3.0

@fmeum

fmeum commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

@iancha1992 Could you mark this for an 8.9.0 release if we ever have one? This would make it easier to support new rules_java features with old versions of Bazel.

fmeum pushed a commit to fmeum/bazel that referenced this pull request Sep 2, 2026
…compilation_action` (bazelbuild#30808)

This allows faster iteration on the `rules_java` <> `JavaBuilder` contract without having to wait for Bazel changes/releases. In the immediate future, this will be used for the unused-deps checking. Longer term this should also help in migrating off the native compilation code.

Note: the added integration shell test is currently a no-op on CI (since we need the corresponding rules_java changes) but does allow for local testing with an overridden `@rules_java`.

Closes bazelbuild#30808.

PiperOrigin-RevId: 971826913
Change-Id: I8047e057716475a7a4d8bff48b8c7bfe6d79ba1c

(cherry picked from commit 0e6f09d)
@iancha1992 iancha1992 added the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Sep 2, 2026
fmeum pushed a commit to fmeum/bazel that referenced this pull request Sep 3, 2026
…compilation_action` (bazelbuild#30808)

This allows faster iteration on the `rules_java` <> `JavaBuilder` contract without having to wait for Bazel changes/releases. In the immediate future, this will be used for the unused-deps checking. Longer term this should also help in migrating off the native compilation code.

Note: the added integration shell test is currently a no-op on CI (since we need the corresponding rules_java changes) but does allow for local testing with an overridden `@rules_java`.

Closes bazelbuild#30808.

PiperOrigin-RevId: 971826913
Change-Id: I8047e057716475a7a4d8bff48b8c7bfe6d79ba1c

(cherry picked from commit 0e6f09d)
bazel-io pushed a commit to bazel-io/bazel that referenced this pull request Sep 3, 2026
….create_compilation_action` (bazelbuild#30981)

This allows faster iteration on the `rules_java` <> `JavaBuilder`
contract without having to wait for Bazel changes/releases. In the
immediate future, this will be used for the unused-deps checking. Longer
term this should also help in migrating off the native compilation code.

Note: the added integration shell test is currently a no-op on CI (since
we need the corresponding rules_java changes) but does allow for local
testing with an overridden `@rules_java`.

Closes bazelbuild#30808.

PiperOrigin-RevId: 971826913
Change-Id: I8047e057716475a7a4d8bff48b8c7bfe6d79ba1c

(cherry picked from commit 0e6f09d)

9.3.0 adaptation: `java_common.bzl` has no `_ALLOWLIST` constant on this
branch, so `bazel_internal/test_rules` is added to the allowlist passed
inline to `check_private_api`. `JavaCompileActionBuilderTest` needed a
`testutil:TestConstants` dep for the new test. The `bazel_java_test.sh`
cases are left out: they exercise `--experimental_check_unused_deps`,
which is a JavaBuilder option added by bazelbuild#30807 and not present on this
branch, and they are a no-op on CI anyway.
`JavaCompileActionBuilderTest` and all four shards of
`//src/test/shell/bazel:bazel_java_test_jdk17_toolchain_head` pass
locally.

Closes bazelbuild#30968

Co-authored-by: Jonathan Perry <jonathan.perry@gs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone team-Rules-Java Issues for Java rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants