From 7307263f824d3552e39a197be3289624b3ebe71e Mon Sep 17 00:00:00 2001 From: Rafael Chaves Date: Wed, 4 Oct 2023 13:32:12 -0300 Subject: [PATCH] Stop relying on Settings from BuildLayout As mentioned by @adammurdoch: > Its root directory should be the root > directory of the build, taken from the > BuildLocations rather than the mutable project > directory of the root project. Issue: #26521 --- .../api/file/BuildLayoutIntegrationTest.groovy | 3 ++- .../gradle/api/internal/file/DefaultBuildLayout.java | 12 ++++++------ .../service/scopes/SettingsScopeServices.java | 6 ++++-- .../internal/file/DefaultProjectLayoutTest.groovy | 3 ++- .../api/internal/project/DefaultProjectTest.groovy | 3 ++- .../groovy/org/gradle/util/TestUtil.groovy | 5 ++++- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/subprojects/core/src/integTest/groovy/org/gradle/api/file/BuildLayoutIntegrationTest.groovy b/subprojects/core/src/integTest/groovy/org/gradle/api/file/BuildLayoutIntegrationTest.groovy index 4d6f2dccc07f..fee2daf2ea24 100644 --- a/subprojects/core/src/integTest/groovy/org/gradle/api/file/BuildLayoutIntegrationTest.groovy +++ b/subprojects/core/src/integTest/groovy/org/gradle/api/file/BuildLayoutIntegrationTest.groovy @@ -77,6 +77,7 @@ class BuildLayoutIntegrationTest extends AbstractIntegrationSpec { // setting a custom settings location is deprecated executer.noDeprecationChecks() groovyFile(customSettingsFile, """ + // changing the root project dir does not impact the build dir obtained from BuildLayout rootProject.projectDir = file('..') ${printLocations()} """) @@ -85,7 +86,7 @@ class BuildLayoutIntegrationTest extends AbstractIntegrationSpec { run("help", "--settings-file", customSettingsPath) then: - outputContains("settings root dir: " + testDirectory + ".") + outputContains("settings root dir: " + customSettingsDir + ".") outputContains("settings dir: " + customSettingsDir + ".") outputContains("settings source file: " + customSettingsFile + ".") } diff --git a/subprojects/core/src/main/java/org/gradle/api/internal/file/DefaultBuildLayout.java b/subprojects/core/src/main/java/org/gradle/api/internal/file/DefaultBuildLayout.java index 4606fb94b890..ca82fedcaf44 100644 --- a/subprojects/core/src/main/java/org/gradle/api/internal/file/DefaultBuildLayout.java +++ b/subprojects/core/src/main/java/org/gradle/api/internal/file/DefaultBuildLayout.java @@ -18,25 +18,25 @@ import org.gradle.api.file.BuildLayout; import org.gradle.api.file.Directory; -import org.gradle.api.initialization.Settings; +import org.gradle.initialization.layout.BuildLocations; public class DefaultBuildLayout implements BuildLayout { protected final FileFactory fileFactory; - private final Settings settings; + private final BuildLocations buildLocations; - public DefaultBuildLayout(Settings settings, FileFactory fileFactory) { + public DefaultBuildLayout(BuildLocations buildLocations, FileFactory fileFactory) { this.fileFactory = fileFactory; - this.settings = settings; + this.buildLocations = buildLocations; } @Override public Directory getSettingsDirectory() { - return fileFactory.dir(settings.getSettingsDir()); + return fileFactory.dir(buildLocations.getSettingsDir()); } @Override public Directory getRootDirectory() { - return fileFactory.dir(settings.getRootDir()); + return fileFactory.dir(buildLocations.getRootDirectory()); } } diff --git a/subprojects/core/src/main/java/org/gradle/internal/service/scopes/SettingsScopeServices.java b/subprojects/core/src/main/java/org/gradle/internal/service/scopes/SettingsScopeServices.java index 8b4ad849f999..dd186e62a88b 100644 --- a/subprojects/core/src/main/java/org/gradle/internal/service/scopes/SettingsScopeServices.java +++ b/subprojects/core/src/main/java/org/gradle/internal/service/scopes/SettingsScopeServices.java @@ -36,6 +36,8 @@ import org.gradle.api.model.ObjectFactory; import org.gradle.cache.internal.LegacyCacheCleanupEnablement; import org.gradle.configuration.ConfigurationTargetIdentifier; +import org.gradle.initialization.layout.BuildLocations; +import org.gradle.internal.code.UserCodeApplicationContext; import org.gradle.initialization.DefaultProjectDescriptorRegistry; import org.gradle.internal.code.UserCodeApplicationContext; import org.gradle.internal.instantiation.InstantiatorFactory; @@ -60,8 +62,8 @@ public SettingsScopeServices(final ServiceRegistry parent, final SettingsInterna }); } - protected BuildLayout createBuildLayout(FileFactory fileFactory) { - return new DefaultBuildLayout(settings, fileFactory); + protected BuildLayout createBuildLayout(FileFactory fileFactory, BuildLocations buildLocations) { + return new DefaultBuildLayout(buildLocations, fileFactory); } protected FileResolver createFileResolver(FileLookup fileLookup) { diff --git a/subprojects/core/src/test/groovy/org/gradle/api/internal/file/DefaultProjectLayoutTest.groovy b/subprojects/core/src/test/groovy/org/gradle/api/internal/file/DefaultProjectLayoutTest.groovy index 7e099c1eb0e4..54a48e45d3f4 100644 --- a/subprojects/core/src/test/groovy/org/gradle/api/internal/file/DefaultProjectLayoutTest.groovy +++ b/subprojects/core/src/test/groovy/org/gradle/api/internal/file/DefaultProjectLayoutTest.groovy @@ -18,6 +18,7 @@ package org.gradle.api.internal.file import org.gradle.api.internal.provider.PropertyHost import org.gradle.api.internal.tasks.TaskDependencyFactory +import org.gradle.initialization.layout.BuildLocations import org.gradle.internal.Factory import org.gradle.test.fixtures.file.TestFile import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider @@ -36,7 +37,7 @@ class DefaultProjectLayoutTest extends Specification { def setup() { projectDir = tmpDir.createDir("project") - layout = new DefaultProjectLayout(projectDir, TestFiles.resolver(projectDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), TestFiles.fileCollectionFactory(projectDir), TestFiles.filePropertyFactory(projectDir), TestFiles.fileFactory()) + layout = new DefaultProjectLayout(projectDir, TestFiles.resolver(projectDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), TestFiles.fileCollectionFactory(projectDir), TestFiles.filePropertyFactory(projectDir), TestFiles.fileFactory(), new BuildLocations(projectDir, null, null, null)) } def "can query the project directory"() { diff --git a/subprojects/core/src/test/groovy/org/gradle/api/internal/project/DefaultProjectTest.groovy b/subprojects/core/src/test/groovy/org/gradle/api/internal/project/DefaultProjectTest.groovy index 9f027348049c..e988e30d030f 100644 --- a/subprojects/core/src/test/groovy/org/gradle/api/internal/project/DefaultProjectTest.groovy +++ b/subprojects/core/src/test/groovy/org/gradle/api/internal/project/DefaultProjectTest.groovy @@ -80,6 +80,7 @@ import org.gradle.configuration.project.ProjectEvaluator import org.gradle.groovy.scripts.EmptyScript import org.gradle.groovy.scripts.ScriptSource import org.gradle.initialization.ClassLoaderScopeRegistryListener +import org.gradle.initialization.layout.BuildLocations import org.gradle.internal.Factory import org.gradle.internal.instantiation.InstantiatorFactory import org.gradle.internal.logging.LoggingManagerInternal @@ -243,7 +244,7 @@ class DefaultProjectTest extends Specification { ModelSchemaStore modelSchemaStore = Stub(ModelSchemaStore) serviceRegistryMock.get((Type) ModelSchemaStore) >> modelSchemaStore serviceRegistryMock.get(ModelSchemaStore) >> modelSchemaStore - serviceRegistryMock.get((Type) DefaultProjectLayout) >> new DefaultProjectLayout(rootDir, TestFiles.resolver(rootDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), Stub(FileCollectionFactory), TestFiles.filePropertyFactory(), TestFiles.fileFactory()) + serviceRegistryMock.get((Type) DefaultProjectLayout) >> new DefaultProjectLayout(rootDir, TestFiles.resolver(rootDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), Stub(FileCollectionFactory), TestFiles.filePropertyFactory(), TestFiles.fileFactory(), new BuildLocations(rootDir, null, null, null)) build.getProjectEvaluationBroadcaster() >> Stub(ProjectEvaluationListener) build.getParent() >> null diff --git a/subprojects/core/src/testFixtures/groovy/org/gradle/util/TestUtil.groovy b/subprojects/core/src/testFixtures/groovy/org/gradle/util/TestUtil.groovy index d15cee21c60a..649e6d99e8c6 100644 --- a/subprojects/core/src/testFixtures/groovy/org/gradle/util/TestUtil.groovy +++ b/subprojects/core/src/testFixtures/groovy/org/gradle/util/TestUtil.groovy @@ -44,6 +44,7 @@ import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.ProviderFactory import org.gradle.api.tasks.util.internal.PatternSets import org.gradle.cache.internal.TestCrossBuildInMemoryCacheFactory +import org.gradle.initialization.layout.BuildLocations import org.gradle.internal.hash.ChecksumService import org.gradle.internal.hash.HashCode import org.gradle.internal.hash.Hashing @@ -158,7 +159,9 @@ class TestUtil { ProjectLayout createProjectLayout() { def filePropertyFactory = new DefaultFilePropertyFactory(PropertyHost.NO_OP, fileResolver, fileCollectionFactory) - return new DefaultProjectLayout(fileResolver.resolve("."), fileResolver, DefaultTaskDependencyFactory.withNoAssociatedProject(), PatternSets.getNonCachingPatternSetFactory(), PropertyHost.NO_OP, fileCollectionFactory, filePropertyFactory, filePropertyFactory) + def projectDir = fileResolver.resolve(".") + def buildLocations = new BuildLocations(projectDir, null, null, null) + return new DefaultProjectLayout(projectDir, fileResolver, DefaultTaskDependencyFactory.withNoAssociatedProject(), PatternSets.getNonCachingPatternSetFactory(), PropertyHost.NO_OP, fileCollectionFactory, filePropertyFactory, filePropertyFactory, buildLocations) } ChecksumService createChecksumService() {