Repository navigation
Keep imports used by Markdown doc comments in removeUnusedImports - #3163
Open
1AbdullahButt wants to merge 2 commits into
Open
1AbdullahButt wants to merge 2 commits into
1AbdullahButt wants to merge 2 commits into
Conversation
On JDK 23+, javac parses Markdown doc comments (`///`) with a transformer from the jdk.internal.md module, which it finds with ServiceLoader on the thread's context class loader. Gradle and Maven run Spotless with a context class loader that cannot see that module, so javac fell back to treating the comments as plain text, and google-java-format removed imports used only by their links. Run google-java-format's RemoveUnusedImports with javac's own class loader as the context class loader. Fixes diffplug#3161 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…adoc-imports # Conflicts: # CHANGES.md # plugin-gradle/CHANGES.md # plugin-maven/CHANGES.md
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3161
Cause
On JDK 23+, javac turns Markdown doc comments (
///) into doc trees with aDocCommentTreeTransformerfrom thejdk.internal.mdmodule.JavacTrees.initDocCommentTreeTransformer()finds it withServiceLoader.load(...), which uses the thread's context class loader.jdk.internal.md(likejdk.compiler) is defined to the JDK's app class loader. Gradle and Maven run Spotless with a context class loader whose parent chain doesn't include the app loader, so the lookup finds nothing and javac quietly uses its identity transformer. google-java-format'sRemoveUnusedImportsthen never sees the[ArrayList#add]style links, so it drops the imports they use.The fix is in Spotless, not google-java-format: GJF just calls
JavacTrees.getDocCommentTree, which works when the context class loader is the app loader (for example, the GJF CLI).Fix
A small glue helper,
GoogleJavaFormatUnusedImports, callsRemoveUnusedImports.removeUnusedImportswith javac's own class loader (DocTrees.class.getClassLoader()) as the context class loader, and restores the original loader afterwards. It's used by bothremoveUnusedImports()andgoogleJavaFormat(), since the latter also runsRemoveUnusedImports. On JDKs before 23 there is no provider, so nothing changes.Tests
RemoveUnusedImportsStep_withGoogleJavaFormatTest.markdownJavadoc()(JDK 23+) with theMarkdownJavadoc*.testresources. It sets the context class loader to the platform class loader to simulate the build tools.RemoveUnusedImportsStep_withGoogleJavaFormatTestandGoogleJavaFormatStepTestall pass, and:lib:spotlessCheckand:testlib:spotlessCheckpass.CHANGES.md, plugin-gradle/CHANGES.md and plugin-maven/CHANGES.md are updated under
[Unreleased].This change was written with Claude Code (AI).
🤖 Generated with Claude Code