Skip to content
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

Add an option to exclude paths from being analyzed #599

Closed
wants to merge 6 commits into from

Conversation

kageiit
Copy link

@kageiit kageiit commented Apr 11, 2017

This adds an option to suppress some paths entirely from being analyzed by the error prone analyzer. This is particularly useful for generated code or third party code which consumers may not want to analyze.

Fixes #523

The input patterns are treated as regular expressions for path matching. For example:
-XepExcludedPaths:".*/build/generated/.*\.java" will exclude all java files inside any build/generated/ directories

@kageiit
Copy link
Author

kageiit commented Apr 11, 2017

cc @msridhar

@vanniktech
Copy link

This would also fix #511

@kageiit
Copy link
Author

kageiit commented Jun 7, 2017

@cushon ping on this

@nick-someone
Copy link
Member

Hello! We discussed this a bit:

We believe that the combination of -XepDisableWarningsInGeneratedCode and separating the compilation of generated code into its own compiler invocation (perhaps without Error Prone enabled, or with Error Prone configured to turn off a select set of checks) may be a better long term idea.

The flag allows error prone to skip generating warnings for code annotated with @Generated, but will still break the build if the generated code violates the error-level patterns (i.e.: the stuff that would break your build if you wrote it by hand). We've selected the list of error-level checks based on our impression that these represent real issues that should be corrected.

If you find that the generated code is consistently violating these error-level checks, we may want to consider reclassifying the checks, or fixing the code generator if the code it's generation contains the buggy code.

@kageiit
Copy link
Author

kageiit commented Jun 22, 2017

Hey @nglorioso This is for more than just generated code. There are cases where we have vendor/third party code that needs to be part of the same sourceset. Also, not all code generators add the @Generated annotation, so just the existing -XepDisableWarningsInGeneratedCode options will not work for those cases.

I think having an option like this would make it very flexible and let consumers suppress generated code issues etc. and also any other potential future use cases like during a migration when two modules are merged etc, to turn off EP on a particular path so it can be done incrementally etc. when dealing with large projects at scale

@ZacSweers
Copy link

Ping on this?

@vladgurovich
Copy link

vladgurovich commented Oct 12, 2017

Furthermore, this seems like something that would enable a gradual rollout of this tool on a legacy code that would otherwise generate hundreds of errors or warnings:
I can enable error-prone on a smaller subset of files/classes and gradually increase that subset as i fix the bugs that error-prone uncovers

*/
private boolean isExcludedPath(URI uri) {
Pattern excludedPattern = errorProneOptions.getExcludedPattern();
return (excludedPattern != null) && excludedPattern.matcher(uri.toString()).matches();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you use ASTHelpers.getFileNameFromUri instead of URI.toString? The former has special handling for jar entries, which I think it's possible to see here.

@kageiit
Copy link
Author

kageiit commented Nov 1, 2017

@cushon updated as per your suggestion

@cushon
Copy link
Collaborator

cushon commented Nov 1, 2017

LGTM, but can you add test coverage?

This was leftover code that shouldn't have made it here
@googlebot
Copy link
Collaborator

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this State. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@googlebot googlebot added cla: no and removed cla: yes labels Nov 3, 2017
@msridhar
Copy link
Contributor

msridhar commented Nov 3, 2017

I'm ok with my commits being included

} else if (finishedCompilation(path.getCompilationUnit())) {
// Otherwise this TaskEvent is for a ClassTree, and we can scan the whole
// CompilationUnitTree once we've seen all the enclosed classes.
transformer.get().apply(new TreePath(compilation), subContext, descriptionListener);
if (!isExcludedPath(compilation.getSourceFile().toUri())) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice to de-duplicate this logic.

@@ -412,4 +435,12 @@ public static ErrorProneOptions processArgs(String[] args) {
Preconditions.checkNotNull(args);
return processArgs(Arrays.asList(args));
}

private static Pattern getExcludedPattern(Iterable<String> excludeSubStrings) {
Copy link
Collaborator

@cushon cushon Nov 3, 2017

Choose a reason for hiding this comment

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

WDYT about making the flag take a regex instead of a comma-separated list that gets processed into a regex? Making it an arbitrary regex avoids the need to define what the syntax is.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense; will change

@@ -164,6 +166,20 @@ public void recognizesCompilingTestOnlyCode() {
}

@Test
public void recognizesExcludedPaths() {
Copy link
Collaborator

@cushon cushon Nov 3, 2017

Choose a reason for hiding this comment

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

Can you add an integration test that verifies the option is used to filter the compilation units that get analyzed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Will look into it; is there an existing test whose structure I can copy?

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are test cases in ErrorProneJavaCompilerTest that use flags to enable/disable checks and change severities. Maybe something like that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, thanks! I added a test case there.

@cushon
Copy link
Collaborator

cushon commented Nov 8, 2017

Thanks! I'm merging this change internally and it will show up on github in the next day or so.

Can you also create a PR to add documentation to http://errorprone.info/docs/flags?

@msridhar
Copy link
Contributor

msridhar commented Nov 8, 2017

Awesome! Will create a documentation PR.

@cushon cushon closed this in 92254ee Nov 9, 2017
ronshapiro pushed a commit that referenced this pull request Nov 9, 2017
Fixes #599

RELNOTES: Add an option to exclude paths from being analyzed

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175049602
ronshapiro pushed a commit that referenced this pull request Nov 9, 2017
Fixes #599

RELNOTES: Add an option to exclude paths from being analyzed

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175049602
ronshapiro pushed a commit that referenced this pull request Nov 13, 2017
Fixes #599

RELNOTES: Add an option to exclude paths from being analyzed

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175049602
@vorburger
Copy link
Member

FTR: Not release as of right now, but #821 says about to...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants