-
Notifications
You must be signed in to change notification settings - Fork 135
baseline-reproducibility validates that sourceCompatibility is set explicitly #1574
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d06d3c5
New 'checkExplicitSourceCompatibility' task
iamdanfox d8f3f28
Clearer error message
iamdanfox 8b2373f
Fail harder
iamdanfox d810d54
Integration tests
iamdanfox 3fade5c
Add generated changelog entries
iamdanfox 0ddbdec
Update gradle-baseline-java/src/main/groovy/com/palantir/baseline/tas…
iamdanfox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| type: feature | ||
| feature: | ||
| description: baseline-reproducibility validates that sourceCompatibility is set | ||
| explicitly | ||
| links: | ||
| - https://github.com/palantir/gradle-baseline/pull/1574 |
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
110 changes: 110 additions & 0 deletions
110
...ava/src/main/groovy/com/palantir/baseline/tasks/CheckExplicitSourceCompatibilityTask.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.palantir.baseline.tasks; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.Collections; | ||
| import javax.inject.Inject; | ||
| import org.gradle.api.DefaultTask; | ||
| import org.gradle.api.GradleException; | ||
| import org.gradle.api.JavaVersion; | ||
| import org.gradle.api.Task; | ||
| import org.gradle.api.model.ObjectFactory; | ||
| import org.gradle.api.plugins.JavaPluginConvention; | ||
| import org.gradle.api.provider.Property; | ||
| import org.gradle.api.publish.PublishingExtension; | ||
| import org.gradle.api.specs.Spec; | ||
| import org.gradle.api.tasks.TaskAction; | ||
| import org.gradle.api.tasks.options.Option; | ||
|
|
||
| /** | ||
| * By default, Gradle will infer sourceCompat based on whatever JVM is currently being used to evaluate the | ||
| * build.gradle files. This is bad for reproducibility because if we make an automated PR to upgrade the Java major | ||
| * version (e.g. 11 -> 15) then a library might unintentionally start publishing jars containing Java15 bytecode! | ||
| * | ||
| * Better to just require everyone to specify sourceCompatibility explicitly! | ||
| */ | ||
| public class CheckExplicitSourceCompatibilityTask extends DefaultTask { | ||
|
|
||
| private final Property<Boolean> shouldFix; | ||
|
|
||
| @Inject | ||
| public CheckExplicitSourceCompatibilityTask(ObjectFactory objectFactory) { | ||
| setGroup("Verification"); | ||
| setDescription("Ensures build.gradle specifies sourceCompatibility explicitly, otherwise it is inferred based" | ||
| + " on $JAVA_HOME which is fragile."); | ||
| this.shouldFix = objectFactory.property(Boolean.class); | ||
| this.shouldFix.set(false); | ||
|
|
||
| onlyIf(new Spec<Task>() { | ||
| @Override | ||
| public boolean isSatisfiedBy(Task element) { | ||
| // sometimes people apply the 'java' plugin to projects that doesn't actually have any java code in it | ||
| // (e.g. the root project), so if they're not publishing anything, then we don't bother enforcing the | ||
| // sourceCompat thing | ||
| PublishingExtension publishing = getProject().getExtensions().findByType(PublishingExtension.class); | ||
| return publishing != null; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Option(option = "fix", description = "Whether to apply the suggested fix to build.gradle") | ||
| public final void setShouldFix(boolean value) { | ||
| shouldFix.set(value); | ||
| } | ||
|
|
||
| @TaskAction | ||
| public final void taskAction() throws IOException { | ||
| // We're doing this naughty casting because we need access to the `getRawSourceCompatibility` method. | ||
| org.gradle.api.plugins.internal.DefaultJavaPluginConvention convention = | ||
| (org.gradle.api.plugins.internal.DefaultJavaPluginConvention) | ||
| getProject().getConvention().getPlugin(JavaPluginConvention.class); | ||
|
|
||
| if (convention.getRawSourceCompatibility() != null) { | ||
| // In theory, users could configure the fancy new 'java toolchain' as an alternative to explicit | ||
| // sourceCompatibility, but there's no method to access this yet (as of Gradle 6.8). | ||
| return; | ||
| } | ||
|
|
||
| if (shouldFix.get()) { | ||
| Files.write( | ||
| getProject().getBuildFile().toPath(), | ||
| Collections.singletonList(String.format("%nsourceCompatibility = %s", JavaVersion.current())), | ||
| StandardCharsets.UTF_8, | ||
| StandardOpenOption.APPEND, | ||
| StandardOpenOption.CREATE); | ||
| return; | ||
| } | ||
|
|
||
| throw new GradleException(String.format( | ||
| "%s must set sourceCompatibility explicitly in '%s', " | ||
| + "otherwise compilation will not be reproducible but instead depends on the Java version " | ||
| + "that Gradle is currently running with (%s). To auto-fix, run%n" | ||
| + "%n" | ||
| + " ./gradlew %s --fix%n" | ||
| + "%n" | ||
| + "This will automatically add a suggested line " | ||
| + "(you may need to adjust the number, e.g. to '1.8' for maximum compatibility).", | ||
| getProject(), | ||
| getProject().getRootProject().relativePath(getProject().getBuildFile()), | ||
| JavaVersion.current(), | ||
| getPath())); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I broke tracing-java and tried it out, error message looks like this: |
||
| } | ||
| } | ||
89 changes: 89 additions & 0 deletions
89
...c/test/groovy/com/palantir/baseline/plugins/BaselineReproducibilityIntegrationSpec.groovy
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.palantir.baseline.plugins | ||
|
|
||
| import nebula.test.IntegrationSpec | ||
| import nebula.test.functional.ExecutionResult | ||
|
|
||
| class BaselineReproducibilityIntegrationSpec extends IntegrationSpec { | ||
|
|
||
| def 'task surfaces the badness'() { | ||
| when: | ||
| buildFile << """ | ||
| ${applyPlugin(BaselineReproducibility.class)} | ||
| apply plugin: 'java' | ||
| apply plugin: 'maven-publish' | ||
|
|
||
| version '1.2.3' | ||
|
|
||
| publishing { | ||
| publications { | ||
| maven(MavenPublication) { | ||
| from components.java | ||
| } | ||
| } | ||
| } | ||
| """.stripIndent() | ||
|
|
||
| writeHelloWorld() | ||
|
|
||
| then: | ||
| ExecutionResult output = runTasksWithFailure("check") | ||
| output.getStandardError().contains("./gradlew :checkExplicitSourceCompatibility --fix") | ||
| } | ||
|
|
||
| def 'task passes when explicitly set'() { | ||
| when: | ||
| buildFile << """ | ||
| ${applyPlugin(BaselineReproducibility.class)} | ||
| apply plugin: 'java' | ||
| apply plugin: 'maven-publish' | ||
|
|
||
| version '1.2.3' | ||
|
|
||
| sourceCompatibility = 1.8 | ||
|
|
||
| publishing { | ||
| publications { | ||
| maven(MavenPublication) { | ||
| from components.java | ||
| } | ||
| } | ||
| } | ||
| """.stripIndent() | ||
|
|
||
| writeHelloWorld() | ||
|
|
||
| then: | ||
| runTasksSuccessfully("checkExplicitSourceCompatibility") | ||
| } | ||
|
|
||
| def 'no-op if nothing is published'() { | ||
| when: | ||
| buildFile << """ | ||
| ${applyPlugin(BaselineReproducibility.class)} | ||
| apply plugin: 'java' | ||
| version '1.2.3' | ||
| """.stripIndent() | ||
|
|
||
| writeHelloWorld() | ||
|
|
||
| then: | ||
| def output = runTasksSuccessfully("check") | ||
| output.getStandardOutput().contains("> Task :checkExplicitSourceCompatibility SKIPPED") | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.