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

-Pcom.palantir.baseline-error-prone.disable #1213

Merged
merged 4 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-1213.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: improvement
improvement:
description: '`./gradlew compileJava -Pcom.palantir.baseline-error-prone.disable`
turns off error-prone, to allow compilation on Java 13 which is not yet supported
by error-prone.'
links:
- https://github.com/palantir/gradle-baseline/pull/1213
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public final class BaselineErrorProne implements Plugin<Project> {
private static final String ERROR_PRONE_JAVAC_VERSION = "9+181-r4173-1";
private static final String PROP_ERROR_PRONE_APPLY = "errorProneApply";
private static final String PROP_REFASTER_APPLY = "refasterApply";
private static final String DISABLE_PROPERY = "com.palantir.baseline-error-prone.disable";

@Override
public void apply(Project project) {
Expand Down Expand Up @@ -110,6 +111,31 @@ private static void applyToJavaProject(Project project) {
task.getRefasterRulesFile().set(refasterRulesFile);
});

// In case of java 8 we need to add errorprone javac compiler to bootstrap classpath of tasks that perform
// compilation or code analysis. ErrorProneJavacPluginPlugin handles JavaCompile cases via errorproneJavac
// configuration and we do similar thing for Test and Javadoc type tasks
if (!JavaVersion.current().isJava9Compatible()) {
project.getDependencies()
.add(
ErrorPronePlugin.JAVAC_CONFIGURATION_NAME,
"com.google.errorprone:javac:" + ERROR_PRONE_JAVAC_VERSION);
project.getConfigurations()
.named(ErrorPronePlugin.JAVAC_CONFIGURATION_NAME)
.configure(
conf -> {
List<File> bootstrapClasspath = Splitter.on(File.pathSeparator)
.splitToList(System.getProperty("sun.boot.class.path"))
.stream()
.map(File::new)
.collect(Collectors.toList());
FileCollection errorProneFiles = conf.plus(project.files(bootstrapClasspath));
project.getTasks().withType(Test.class).configureEach(test ->
test.setBootstrapClasspath(errorProneFiles));
project.getTasks().withType(Javadoc.class).configureEach(javadoc -> javadoc.getOptions()
.setBootClasspath(new LazyConfigurationList(errorProneFiles)));
});
}

project.getTasks().withType(JavaCompile.class).configureEach(javaCompile -> {
((ExtensionAware) javaCompile.getOptions())
.getExtensions()
Expand Down Expand Up @@ -155,42 +181,6 @@ private static void applyToJavaProject(Project project) {
errorProneOptions.check("PreconditionsConstantMessage", CheckSeverity.OFF);
}));
});

project.getTasks().withType(JavaCompile.class).configureEach(javaCompile -> {
((ExtensionAware) javaCompile.getOptions())
.getExtensions()
.configure(ErrorProneOptions.class, errorProneOptions -> {
// Relax some checks for test code
if (errorProneOptions.isCompilingTestOnlyCode()) {
errorProneOptions.check("UnnecessaryLambda", CheckSeverity.OFF);
}
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this block was pretty much an exact duplicate of a block above, so I moved this logic further down.


// In case of java 8 we need to add errorprone javac compiler to bootstrap classpath of tasks that perform
// compilation or code analysis. ErrorProneJavacPluginPlugin handles JavaCompile cases via errorproneJavac
// configuration and we do similar thing for Test and Javadoc type tasks
if (!JavaVersion.current().isJava9Compatible()) {
project.getDependencies()
.add(
ErrorPronePlugin.JAVAC_CONFIGURATION_NAME,
"com.google.errorprone:javac:" + ERROR_PRONE_JAVAC_VERSION);
project.getConfigurations()
.named(ErrorPronePlugin.JAVAC_CONFIGURATION_NAME)
.configure(
conf -> {
List<File> bootstrapClasspath = Splitter.on(File.pathSeparator)
.splitToList(System.getProperty("sun.boot.class.path"))
.stream()
.map(File::new)
.collect(Collectors.toList());
FileCollection errorProneFiles = conf.plus(project.files(bootstrapClasspath));
project.getTasks().withType(Test.class).configureEach(test ->
test.setBootstrapClasspath(errorProneFiles));
project.getTasks().withType(Javadoc.class).configureEach(javadoc -> javadoc.getOptions()
.setBootClasspath(new LazyConfigurationList(errorProneFiles)));
});
}
}

@SuppressWarnings("UnstableApiUsage")
Expand All @@ -204,7 +194,13 @@ private static void configureErrorProneOptions(
JavaVersion jdkVersion =
JavaVersion.toVersion(javaCompile.getToolChain().getVersion());

errorProneOptions.setEnabled(true);
if (project.hasProperty(DISABLE_PROPERY)) {
log.info("Disabling baseline-error-prone for {} due to {}", project, DISABLE_PROPERY);
errorProneOptions.setEnabled(false);
} else {
errorProneOptions.setEnabled(true);
}

errorProneOptions.setDisableWarningsInGeneratedCode(true);
String projectPath = project.getProjectDir().getPath();
String separator = Pattern.quote(Paths.get(projectPath).getFileSystem().getSeparator());
Expand All @@ -221,6 +217,11 @@ private static void configureErrorProneOptions(
errorProneOptions.check("JavaDurationGetSecondsGetNano", CheckSeverity.ERROR);
errorProneOptions.check("URLEqualsHashCode", CheckSeverity.ERROR);

// Relax some checks for test code
if (errorProneOptions.isCompilingTestOnlyCode()) {
errorProneOptions.check("UnnecessaryLambda", CheckSeverity.OFF);
}

if (jdkVersion.compareTo(JavaVersion.toVersion("12.0.1")) >= 0) {
// As of version 2.3.4, Errorprone isn't officially compatible with Java12, but in practise everything
// works apart from this one check: https://github.com/google/error-prone/issues/1106
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ class BaselineErrorProneIntegrationTest extends AbstractPluginTest {
result.output.contains("[ArrayEquals] Reference equality used to compare arrays")
}

def 'error-prone can be suppressed using property'() {
when:
buildFile << standardBuildFile
file('src/main/java/test/Test.java') << invalidJavaFile

then:
BuildResult result = with('compileJava', '-Pcom.palantir.baseline-error-prone.disable').build()
}

def 'compileJava succeeds when error-prone finds no errors'() {
when:
buildFile << standardBuildFile
Expand Down