Skip to content

Commit 69acda0

Browse files
committed
Deprecate implicit rootProject.name assignment
Also assigns 'root' if the name is empty. Fixes #15655
1 parent 9c93f04 commit 69acda0

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gradle.api
18+
19+
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
20+
import spock.lang.Ignore
21+
import spock.lang.Issue
22+
23+
@Issue("https://github.com/gradle/gradle/issues/15655")
24+
class RootProjectNameIntegrationTest extends AbstractIntegrationSpec {
25+
@Ignore("This is hard to implement.")
26+
def "rootProject name is 'root' if the settings file is in the filesystem root"() {
27+
}
28+
29+
def "deprecation warning is raised if rootProject name is not explicitly set"() {
30+
settingsFile << """
31+
"""
32+
33+
executer.expectDeprecationWarning(
34+
"Implicit rootProject.name has been deprecated. This will fail with an error in Gradle 8.0. Set the 'rootProject.name' in the settings file."
35+
)
36+
37+
expect:
38+
succeeds()
39+
}
40+
}

subprojects/core/src/main/java/org/gradle/initialization/DefaultProjectDescriptor.java

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class DefaultProjectDescriptor implements ProjectDescriptor, ProjectIdent
5050
private ProjectDescriptorRegistry projectDescriptorRegistry;
5151
private Path path;
5252
private String buildFileName;
53+
private boolean nameChanged;
5354

5455
public DefaultProjectDescriptor(@Nullable DefaultProjectDescriptor parent, String name, File dir,
5556
ProjectDescriptorRegistry projectDescriptorRegistry, PathToFileResolver fileResolver) {
@@ -102,6 +103,11 @@ public void setName(String name) {
102103
INVALID_NAME_IN_INCLUDE_HINT);
103104
projectDescriptorRegistry.changeDescriptorPath(path, path(name));
104105
this.name = name;
106+
this.nameChanged = true;
107+
}
108+
109+
public boolean isNameChanged() {
110+
return nameChanged;
105111
}
106112

107113
@Override

subprojects/core/src/main/java/org/gradle/initialization/DefaultSettings.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ public DefaultSettings(ServiceRegistryFactory serviceRegistryFactory,
9191
this.settingsScript = settingsScript;
9292
this.startParameter = startParameter;
9393
this.services = serviceRegistryFactory.createFor(this);
94-
this.rootProjectDescriptor = createProjectDescriptor(null, settingsDir.getName(), settingsDir);
94+
String name = settingsDir.getName();
95+
if (name.isEmpty()) {
96+
name = "root";
97+
}
98+
this.rootProjectDescriptor = createProjectDescriptor(null, name, settingsDir);
9599
this.dependencyResolutionManagement = services.get(DependencyResolutionManagementInternal.class);
96100
}
97101

subprojects/core/src/main/java/org/gradle/initialization/SettingsEvaluatedCallbackFiringSettingsProcessor.java

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.gradle.api.internal.GradleInternal;
2121
import org.gradle.api.internal.SettingsInternal;
2222
import org.gradle.api.internal.initialization.ClassLoaderScope;
23+
import org.gradle.internal.deprecation.DeprecationLogger;
2324

2425
public class SettingsEvaluatedCallbackFiringSettingsProcessor implements SettingsProcessor {
2526

@@ -34,6 +35,13 @@ public SettingsInternal process(GradleInternal gradle, SettingsLocation settings
3435
SettingsInternal settings = delegate.process(gradle, settingsLocation, buildRootClassLoaderScope, startParameter);
3536
gradle.getBuildListenerBroadcaster().settingsEvaluated(settings);
3637
settings.preventFromFurtherMutation();
38+
if (!((DefaultProjectDescriptor) settings.getRootProject()).isNameChanged()) {
39+
DeprecationLogger.deprecate("Implicit rootProject.name")
40+
.withAdvice("Set the 'rootProject.name' in the settings file.")
41+
.willBecomeAnErrorInGradle8()
42+
.withUserManual("multi_project_builds", "naming_recommendations")
43+
.nagUser();
44+
}
3745
return settings;
3846
}
3947
}

0 commit comments

Comments
 (0)