-
Notifications
You must be signed in to change notification settings - Fork 134
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
Remove afterEvaluate from baseline immutables #1752
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: fix | ||
fix: | ||
description: Build compiler args for `com.palantir.baseline-immutables` plugin lazily to avoid `afterEvaluate` ordering issues. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1752 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,23 +16,33 @@ | |
|
||
package com.palantir.baseline.plugins; | ||
|
||
import java.util.Collections; | ||
import java.util.Objects; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.Dependency; | ||
import org.gradle.api.plugins.JavaPluginConvention; | ||
import org.gradle.api.tasks.SourceSet; | ||
import org.gradle.api.tasks.SourceSetContainer; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
|
||
public final class BaselineImmutables implements Plugin<Project> { | ||
|
||
@Override | ||
public void apply(Project project) { | ||
project.getPluginManager().withPlugin("java", unused -> { | ||
project.afterEvaluate(proj -> { | ||
proj.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream() | ||
.filter(sourceSet -> hasImmutablesProcessor(project, sourceSet)) | ||
.forEach(sourceSet -> addImmutablesIncrementalCompilerArg(project, sourceSet)); | ||
project.getExtensions().getByType(SourceSetContainer.class).configureEach(sourceSet -> { | ||
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. Can we use 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. Hmm, what would we match on here? 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.
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. This is what we do in some other plugins. 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 don't think this will be as lazy as it can be, as we currently check for immutables the moment the compiler args are requested, where as with Also just be aware that there's a lot of really old stuff in this repo and a lot of it is a bit dodgy (but works well enough) 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 tend to avoid fluent code because it makes the behavior less obvious to readers, especially as ordering is concerned. 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. Cool, make sense. Thanks for the explanation! |
||
project.getTasks() | ||
.named(sourceSet.getCompileJavaTaskName(), JavaCompile.class) | ||
.get() | ||
.getOptions() | ||
.getCompilerArgumentProviders() | ||
.add(() -> { | ||
if (hasImmutablesProcessor(project, sourceSet)) { | ||
return Collections.singletonList("-Aimmutables.gradle.incremental"); | ||
} | ||
|
||
return Collections.emptyList(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
@@ -49,13 +59,4 @@ private static boolean hasImmutablesProcessor(Project project, SourceSet sourceS | |
private static boolean isImmutablesValue(Dependency dep) { | ||
return Objects.equals(dep.getGroup(), "org.immutables") && Objects.equals(dep.getName(), "value"); | ||
} | ||
|
||
private static void addImmutablesIncrementalCompilerArg(Project project, SourceSet sourceSet) { | ||
project.getTasks() | ||
.named(sourceSet.getCompileJavaTaskName(), JavaCompile.class) | ||
.get() | ||
.getOptions() | ||
.getCompilerArgs() | ||
.add("-Aimmutables.gradle.incremental"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* (c) Copyright 2021 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 | ||
|
||
class BaselineImmutablesTest extends IntegrationSpec { | ||
private static final String IMMUTABLES = 'org.immutables:value:2.8.8' | ||
|
||
def 'inserts incremental compilation args into source sets that have immutables'() { | ||
buildFile << """ | ||
plugins { | ||
id 'org.unbroken-dome.test-sets' version '3.0.1' | ||
} | ||
|
||
apply plugin: 'com.palantir.baseline-immutables' | ||
apply plugin: 'java' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
testSets { | ||
hasImmutables | ||
doesNotHaveImmutables | ||
hasImmutablesAddedInAfterEvaluate | ||
} | ||
|
||
afterEvaluate { | ||
dependencies { | ||
hasImmutablesAddedInAfterEvaluateAnnotationProcessor '$IMMUTABLES' | ||
} | ||
} | ||
|
||
dependencies { | ||
annotationProcessor '$IMMUTABLES' | ||
|
||
hasImmutablesAnnotationProcessor '$IMMUTABLES' | ||
} | ||
|
||
task compileAll | ||
|
||
tasks.withType(JavaCompile) { javaCompile -> | ||
doFirst { | ||
logger.lifecycle "\${javaCompile.name}: \${javaCompile.options.allCompilerArgs}" | ||
} | ||
|
||
tasks.compileAll.dependsOn javaCompile | ||
} | ||
""".stripIndent() | ||
|
||
['main', 'hasImmutables', 'doesNotHaveImmutables', 'hasImmutablesAddedInAfterEvaluate'].each { | ||
writeJavaSourceFile ''' | ||
public class Foo {} | ||
'''.stripIndent(), "src/$it/java" | ||
} | ||
|
||
when: | ||
def stdout = runTasksSuccessfully('compileAll').standardOutput | ||
println stdout | ||
|
||
then: | ||
stdout.contains 'compileJava: [-Aimmutables.gradle.incremental]' | ||
stdout.contains 'compileHasImmutablesJava: [-Aimmutables.gradle.incremental]' | ||
stdout.contains 'compileDoesNotHaveImmutablesJava: []' | ||
stdout.contains 'compileHasImmutablesAddedInAfterEvaluateJava: [-Aimmutables.gradle.incremental]' | ||
} | ||
} |
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.
Calling
.stream()
on aDomainObjectCollection
is pretty much always a bug, as it doesn't include objects added later (and encourages people to use afterEvaluate). I wonder if we should make this an error prone check?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.
Sounds like a great idea
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.
That said, we do use a
.stream()
below correctly....