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

Invalid file path pattern on Windows #2394

Open
ben-manes opened this issue Jun 13, 2021 · 6 comments
Open

Invalid file path pattern on Windows #2394

ben-manes opened this issue Jun 13, 2021 · 6 comments

Comments

@ben-manes
Copy link

A student trying to run the simulator in Caffeine faced a compilation error because of a malformed regex. I reviewed the Gradle build configuration (build, options) and do not see a mistake there. Since I am not very familiar with Windows my debugging is limited.

I was able to reproduce his problem by running a Windows 10 trial virtual machine. The attached screenshot is from ".\gradlew.bat simulator:run -S". I disabled the errorprone plugin and provided the student those steps so that his project is unblocked.

2

@ben-manes
Copy link
Author

The problem appears to be due to excluding generated source code from the errorprone analysis.

} else if (arg.startsWith(EXCLUDED_PATHS_PREFIX)) {
String pathRegex = arg.substring(EXCLUDED_PATHS_PREFIX.length());
builder.setExcludedPattern(Pattern.compile(pathRegex));

where my codeQuality.gradle file includes the option,

excludedPaths = "${buildDir}/generated-sources/.*"

From the error this looks like a wrong assumption that the path is UNIX style, so a Windows path separator must be escaped.

@ben-manes
Copy link
Author

ben-manes commented Jun 13, 2021

From the docs, I suppose one could argue that the above is a user error that violates the param as it is described as

Additionally, you can completely exclude certain paths from any Error Prone checking via the -XepExcludedPaths flag. The flag takes as an argument a regular expression that is matched against a source file’s path to determine whether it should be excluded. So, to exclude files in any sub-directory of a path containing build/generated, use the option:

-XepExcludedPaths:.*/build/generated/.*

So while using buildDir is a best-practice in Gradle (as build is merely the default name), it results in an invalid regex. Switching to mirror the example should work. Perhaps better error handling would be helpful?

ben-manes added a commit to ben-manes/caffeine that referenced this issue Jun 13, 2021
The excludePaths option for ErrorProne is used to ignore warnings in
generated code. That supplied the path using `buildDir`, but in fact
this is documented as a regular expression. On Windows the separator /
is an invalid regex character if not escaped. By instead using a more
generic pattern, rather than a file path, the build is cross-platform.

google/error-prone#2394
ben-manes added a commit to ben-manes/caffeine that referenced this issue Jun 13, 2021
The excludePaths option for ErrorProne is used to ignore warnings in
generated code. That supplied the path using `buildDir`, but in fact
this is documented as a regular expression. On Windows the separator \
is an invalid regex character if not escaped. By instead using a more
generic pattern, rather than a file path, the build is cross-platform.

google/error-prone#2394
@Stephan202
Copy link
Contributor

We had a similar report from a Windows user and ended up modifying the regex to clarify that the initial path should be interpreted verbatim:

-XepExcludedPaths:\Q${project.build.directory}${file.separator}\E.*

(We're using Maven, but I imagine this trick will also work with Gradle.)

Perhaps it's worthwhile to document this trick somewhere.

@tbroyer
Copy link
Contributor

tbroyer commented Jun 13, 2021

Fwiw, in Gradle, using absolute paths make the task non-relocatable, so using a relative path like in the docs would actually be better as it makes your compile task cacheable (and that cache relocatable).
So the best would then probably be to use something like """\Q${rootProject.relativePath(project.buildDir) + File.separator}\Egenerated-sources\Q${File.separator}\E.*""" (this is Kotlin, I don't know Groovy)

@ben-manes
Copy link
Author

Thanks @tbroyer and @Stephan202. Those are helpful insights. I followed up by removing the exclusion as it only suppressed a warning due to using the @AutoBuilder extension, which errorprone doesn't expect.

@Stephan202
Copy link
Contributor

We had a similar report from a Windows user and ended up modifying the regex to clarify that the initial path should be interpreted verbatim:

-XepExcludedPaths:\Q${project.build.directory}${file.separator}\E.*

(We're using Maven, but I imagine this trick will also work with Gradle.)

Perhaps it's worthwhile to document this trick somewhere.

So... it turns out that -XepExcludedPaths:\Q${project.build.directory}${file.separator}\E.* doesn't actually work (at least not anymore). What does seem to work is -XepExcludedPaths:(?!.*/src/[^/]+/java/.*).*; see PicnicSupermarket/error-prone-support#927 for details.

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

No branches or pull requests

3 participants