Repository navigation
[#2430] Bugfix: annotation processor should allow @Spec in ArgGroup classes - #2538
Open
sharanggupta wants to merge 1 commit into
Open
sharanggupta wants to merge 1 commit into
sharanggupta wants to merge 1 commit into
Conversation
…gGroup classes The annotation processor raised the compile error "@SPEC must be enclosed in a @command, or in a class that implements IVersionProvider" when a `@Spec`-annotated field was declared in an `@ArgGroup` class. At runtime picocli has supported `@Spec` in ArgGroup classes since 4.6, so the compile-time check rejected valid code. Before 4.7.7 the check happened to pass for ArgGroup classes because options inside such a class were looked up by the `@ArgGroup` variable rather than by its type, missed the group, and fell through to `getOrCreateCommandSpecForArg`, which registered a `CommandSpec` for the ArgGroup class as a side effect. 4.7.7 fixed that lookup, and the latent check started to fire. Remove the check, as the maintainer suggested in the issue: `@Spec` is valid in several places that are not commands (ArgGroup classes, mixins, IVersionProvider implementations), and picocli injects the `CommandSpec` at runtime without the processor needing to attach it to a model object. Elements that are not enclosed in a command are now traced at FINE level instead of being reported as errors. Closes remkop#2430
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 #2430.
Problem. With the annotation processor enabled, a
@Spec CommandSpec spec;field inside an@ArgGroupclass fails compilation with@Spec must be enclosed in a @Command, or in a class that implements IVersionProvider but was .... Picocli supports@Specin ArgGroup classes at runtime since 4.6 (user manual, "@SPEC Annotation" tip), so the processor rejected valid code. The reporter saw this appear in 4.7.7.Cause. The check in
AbstractCommandSpecProcessor.Context.connectModel()has existed since 4.5 (#1134), but before 4.7.7 it passed by accident for ArgGroup classes:@Options inside an ArgGroup class were looked up by the@ArgGroupvariable instead of its type, missed the group, fell through togetOrCreateCommandSpecForArg, and that registered aCommandSpecfor the ArgGroup class as a side effect. The 4.7.7 fix to look groups up by type removed that side effect and the latent check started to fire.Fix. As suggested in the issue, the check is removed rather than extended:
@Specis legitimately used in several non-command classes (ArgGroup classes, mixins,IVersionProviderimplementations) and picocli injects theCommandSpecat runtime, so there is nothing the processor needs to attach for them. A FINE-level log line replaces the error. I considered relaxing the check to also accept ArgGroup types (keys ofargGroupElementsByType) to keep a diagnostic for stray@Specfields, but that is the same allow-list pattern that produced this false positive and the runtime has no equivalent error; happy to switch to that if you prefer.Tests.
Issue2430Testinpicocli-codegen-tests-java9pluscompiles the reporter's example (ArgGroup class with@Specinside a@Mixin) and a variant with the ArgGroup directly on the command; both failed with the exact message above before the change and pass after it.:picocli-codegen:testand:picocli-codegen-tests-java9plus:testare green.