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
[core] Support forcing a specific language from the command-line #3417
Conversation
Generated by 🚫 Danger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat small fix that enables a ton of new possibilities. Love it. Thank you!
Would approve it but I need the main-tainers @adangel and @oowekyala to answer wether they are ok with the modified standard behaviour (see comment) and if more tests are needed.
pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDParameters.java
Outdated
Show resolved
Hide resolved
Thanks for the PR! Just to give a bit of background info, why and for what reasons this cli parameter was introduced in the first place: If a language supports more than one version (Java and Scala being the only languages with multiple versions) you could tell PMD, that - when it has discovered the language Java by extension - it should use a specific version instead of the default (latest) version (some rules are version dependent and would suggest constructs that are only possible in later versions of the language). It's also needed if you want to use java preview features. I have to think a bit more about this as the other idea that was introduced with PMD 5 was, that it should be possible to have one ruleset with multiple languages and PMD should automagically do "the right thing". I guess, providing the language in that way limits the PMD run to exactly this language. I'm not sure how important this feature is really (one PMD execution, single ruleset, multiple languages) and how often this is actually used. Different languages also need different extra parameters to work correctly (besides the specific language version): for Java, it's auxclasspath, for Visualforce it's via environment parameters |
@adangel thanks for providing this background. What if we instead introduce an extensions parameter per ruleset like this
@aidan-harding I guess this would also perfectly cover your flow use case, right? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
To me this looks like a nice workaround until a fix for #461 is introduced. But I don't think we should aim for keeping this in in the long-term... If we consider all input files to be of the given language, then we can't safely ignore things like .gitignore
or resource files that may be in the file tree. They will probably fail parsing, which is a minor inconvenience I guess, since the user can then correct it by excluding the files anyway. I guess, few users will be concerned.
I don't know either how much users actually rely on pmd's language discovery mechanism, but I personally think it's a nice feature. Its only problem is extensibility, like in many places in pmd... We can work on that as we go.
What if we instead introduce an extensions parameter per ruleset like this
Side note, I made a detailed proposal for this in #461 (comment), though its scope is broader. The idea is to allow each rule to match the set of files it wants instead of configuring necessarily per language/ruleset. Language and ruleset would then just provide defaults.
pmd-core/src/main/java/net/sourceforge/pmd/PMDConfiguration.java
Outdated
Show resolved
Hide resolved
pmd-core/src/main/java/net/sourceforge/pmd/PMDConfiguration.java
Outdated
Show resolved
Hide resolved
pmd-core/src/main/java/net/sourceforge/pmd/processor/PmdRunnable.java
Outdated
Show resolved
Hide resolved
@oowekyala thanks for mentioning #461. That looks like a solution to all issues @aidan-harding and I try to cover. What is the status of this issue? Can it be worked on? based on your proposal? Any blockers? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accidentally clicked "Request for Re-Review"
Thanks for the feedback and context, everyone. I agree that the ideal behaviour for a user would be able to point PMD at a set of source files in mixed languages and have it discover the right thing to do. So, a full solution to #461 would be amazing. Hopefully, my changes will help in the interim, though. I've updated the PR as per @oowekyala's comments |
1de6c61
to
1c2298d
Compare
@rsoesemann could you please run the validation workflow for this? I think I'm at the final version of the PR, so it would be good to have the workflow tests complete before @oowekyala looks at the code Using a local build, I tried this in the scenario you cared about and I was able to check an object file from an MDAPI Salesforce project (Account.object) using an XML XPath rule. |
@aidan-harding So @adangel and I chatted on Tuesday and agreed that a CLI argument to force use of a language is useful and we should merge it. However we'd like it to be more explicit, for instance, Currently using So @aidan-harding I think this PR needs a slight rework before we merge it. Could you add a new option We maintainers can take care of adding some integration tests and updating the documentation.
For us:
|
That sounds like a decent way forward. But I wonder if we can make I do understand that, in general, you want PMD to be as smart as possible. So that I can run PMD on a directory containing a mix of files from different languages and some that aren't supported languages at all, and then it would use the right languages and skip other files. That's definitely the ideal. Separating out |
Store the language version provided by a -force-language command-line argument and use that as the default language before falling back to the filename
1c2298d
to
6b8c12b
Compare
@oowekyala I've updated my changes to use a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating the PR!
I'm ok with your solution for scanning all files if a directory is given.
I'll let it run now. If the regression tester is back at "This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.", then we know, that this change at least doesn't introduce any problems.
pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java
Outdated
Show resolved
Hide resolved
pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java
Outdated
Show resolved
Hide resolved
pmd-core/src/main/java/net/sourceforge/pmd/PMDConfiguration.java
Outdated
Show resolved
Hide resolved
pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDParameters.java
Outdated
Show resolved
Hide resolved
Thanks, @adangel, I started to respond to your specific comments, then it turned out that I needed to refresh my browser to see that you'd already fixed up and merged the PR. And I see it's already in a release 🎉 |
Describe the PR
Store the language version provided by a new command-line argument
-force-language
and use that as the default language if determining by file extension doesn't work (e.g. no language module is responsible for the file extension currently analyzed)The
-language
parameter from the CLI is currently being ignored. It it meant to be used together with-version
(see #3426). The language used to analyse a file is based on the file extension. Each language modules knows about its own extensions it is responsible for. If no language module is found, then PMD falls back to a default language, which is currently "java".This makes it hard to analyse XML files which use the "wrong" file extension (not ".xml"). With
-force-language
one can change the default language of PMD so that it doesn't fall back to "java" but to the specified language.Note, that this doesn't affect the way, how PMD searches for files to be analyzed: As the extensions are not changed, specifying a directory with
-d
together with-force-language
won't automatically search for new file extensions. You need to call PMD for each file individually or use-filelist
.The fix here records the language as specified on the command line (if any), and then uses that as the default language for analysis. If no language is supplied, the original behaviour is maintained - falling back to java.
Note: I was only able to add a fairly simple unit test for the first part of this. Running the whole end-to-end CLI is tricky. However, I built with Maven and the original tests work. It may be good to add more tests to
net.sourceforge.pmd.it.AllRulesIT
once we have other standard rulesets which leverage this functionality.NB this is my first PR on PMD, so apologies if anything is missing/wrong!
This is highlighted in #1540 and #2133
Related issues
-force-language pom
with specifying a single file e.g.-d pom.xml
.-filelist
)Ready?
./mvnw clean verify
passes (checked automatically by github actions)