Skip to content

Commit

Permalink
Preserve Develocity behavior when Isolated Projects is not enabled
Browse files Browse the repository at this point in the history
Fixes #29013
  • Loading branch information
bamboo committed May 9, 2024
1 parent 8920d62 commit 7e1db85
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.gradle.internal.enterprise

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.executer.GradleContextualExecuter
import spock.lang.IgnoreIf

class DevelocityBuildLifecycleServiceIntegrationTest extends AbstractIntegrationSpec {

Expand All @@ -25,7 +27,7 @@ class DevelocityBuildLifecycleServiceIntegrationTest extends AbstractIntegration
include 'foo', 'bar'
includeBuild 'included'
gradle.services.get(${DevelocityBuildLifecycleService.name}).beforeProject {
$beforeProject {
def projectPath = it.buildTreePath
println "Configuring '\$projectPath'"
it.tasks.register("myTask") {
Expand All @@ -50,6 +52,41 @@ class DevelocityBuildLifecycleServiceIntegrationTest extends AbstractIntegration
outputDoesNotContain("Configuring ':included")
}

@IgnoreIf(
value = { GradleContextualExecuter.isolatedProjects },
reason = "accessing `tasks` from subprojects is in violation of Isolated Projects"
)
def "lifecycle applied build logic runs before subprojects configuration logic"() {
given:
settingsFile """
include 'sub'
$beforeProject {
it.tasks.register("myTask") {
}
}
"""
createDir 'sub'

and:
buildFile '''
subprojects {
tasks.named("myTask") {
def projectName = project.name
doLast {
println "${projectName}.MyTask.doLast is running"
}
}
}
'''

when:
run 'myTask'

then:
outputContains 'sub.MyTask.doLast is running'
}

def "can use lifecycle service in included build"() {
settingsFile << """
include 'foo', 'bar'
Expand All @@ -59,7 +96,7 @@ class DevelocityBuildLifecycleServiceIntegrationTest extends AbstractIntegration
file('included/settings.gradle') << """
include 'sub1', 'sub2'
gradle.services.get(${DevelocityBuildLifecycleService.name}).beforeProject {
$beforeProject {
def projectPath = it.buildTreePath
println "Configuring '\$projectPath'"
it.tasks.register("myTask") {
Expand All @@ -77,4 +114,8 @@ class DevelocityBuildLifecycleServiceIntegrationTest extends AbstractIntegration
outputDoesNotContain("Configuring ':foo")
outputDoesNotContain("Configuring ':bar")
}

def getBeforeProject() {
"gradle.services.get(${DevelocityBuildLifecycleService.name}).beforeProject"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,39 @@

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.configuration.BuildFeatures;
import org.gradle.api.invocation.Gradle;
import org.gradle.internal.enterprise.DevelocityBuildLifecycleService;

import javax.inject.Inject;

public class DefaultDevelocityBuildLifecycleService implements DevelocityBuildLifecycleService {

private final Gradle gradle;
private final BuildFeatures features;

public DefaultDevelocityBuildLifecycleService(Gradle gradle) {
@Inject
public DefaultDevelocityBuildLifecycleService(Gradle gradle, BuildFeatures features) {
this.gradle = gradle;
this.features = features;
}

@Override
public void beforeProject(Action<? super Project> action) {
gradle.getLifecycle().beforeProject(action::execute);
// Preserve behavior when Isolated Projects is not enabled:
// - `allprojects` executes eagerly before any project has been evaluated, allowing its effects
// to be observable from other eager configuration blocks (e.g., `subprojects { ... }`).
// - `lifecycle.beforeProject` executes just before each project is evaluated. Therefore, its effects
// are not observable from eager configuration blocks, which would anyway be incompatible with
// Isolated Projects.
if (isIsolatedProjects()) {
gradle.getLifecycle().beforeProject(action::execute);
} else {
gradle.allprojects(action);
}
}

private boolean isIsolatedProjects() {
return features.getIsolatedProjects().getActive().getOrElse(false);
}
}

0 comments on commit 7e1db85

Please sign in to comment.