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

Remove afterEvaluate from baseline immutables #1752

Merged
merged 4 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1752.v2.yml
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
Expand Up @@ -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()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling .stream() on a DomainObjectCollection 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?

Copy link
Contributor

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

Copy link
Contributor Author

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....

.filter(sourceSet -> hasImmutablesProcessor(project, sourceSet))
.forEach(sourceSet -> addImmutablesIncrementalCompilerArg(project, sourceSet));
project.getExtensions().getByType(SourceSetContainer.class).configureEach(sourceSet -> {
Copy link
Member

Choose a reason for hiding this comment

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

Can we use .matching(...) here to keep this fluent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, what would we match on here?

Copy link
Member

Choose a reason for hiding this comment

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

hasImmutablesProcessor

Copy link
Member

@pkoenig10 pkoenig10 May 4, 2021

Choose a reason for hiding this comment

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

This is what we do in some other plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 .matching on the source sets would check for immutables the earliest of when each source set is created or when configureEach is called.

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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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();
});
});
});
}
Expand All @@ -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]'
}
}