Skip to content

Commit 2a0d89d

Browse files
authored
fix(gradle): build nx graph for gradle projects regardless of build gradle file location (#29783) (#29802)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> With a project structure where build.gradle.kts is defined in a separate buildSrc directory, and not in the root project directory where `gradlew` is defined, the gradle project is not included in nx project graph. This is a valid gradle project configuration, with project-report tasks configured. Running ./gradlew projectReport works and builds the projectReport. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> With the fix, as long as expected tasks - projectReport or projectReportAll - is defined, nx should be able to build the nx graph regardless of the location of the `build.gradle` or `build.gradle.kts` file. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #29783
1 parent e4ea6b9 commit 2a0d89d

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

e2e/gradle/src/gradle-import.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ describe('Nx Import Gradle', () => {
184184
`${directory}/gradlew`,
185185
`${directory}/gradlew.bat`
186186
);
187+
const nxJson = readJson('nx.json');
188+
const gradlePlugin = nxJson.plugins.find(
189+
(plugin) => plugin.plugin === '@nx/gradle'
190+
);
191+
gradlePlugin.exclude = [];
192+
updateJson('nx.json', () => nxJson);
187193
expect(() => {
188194
runCLI(`show projects`);
189195
runCLI('build groovy-app');

packages/gradle/src/utils/get-project-report-lines.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,7 @@ export async function getProjectReportLines(
2222
): Promise<string[]> {
2323
let projectReportBuffer: Buffer;
2424

25-
// if there is no build.gradle or build.gradle.kts file, we cannot run the projectReport nor projectReportAll task
26-
if (
27-
!existsSync(join(dirname(gradlewFile), 'build.gradle')) &&
28-
!existsSync(join(dirname(gradlewFile), 'build.gradle.kts'))
29-
) {
30-
logger.warn(
31-
`Could not find build file near ${gradlewFile}. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks.`
32-
);
33-
return [];
34-
}
35-
25+
// Attempt to run projectReport or projectReportAll task, regardless of build.gradle or build.gradle.kts location
3626
try {
3727
projectReportBuffer = await execGradleAsync(gradlewFile, [
3828
'projectReportAll',

packages/nx/src/command-line/init/implementation/check-compatible-with-plugins.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync } from 'node:fs';
2-
import { join } from 'node:path';
2+
import { join, relative } from 'node:path';
33
import { bold } from 'chalk';
44

55
import { NxJsonConfiguration } from '../../../config/nx-json';
@@ -83,7 +83,13 @@ function findPluginAndFilesWithError(
8383
},
8484
];
8585
}
86-
excludeFiles = excludeFiles.filter(Boolean);
86+
excludeFiles = excludeFiles.filter(Boolean).map((excludeFile) => {
87+
const file = excludeFile.file;
88+
excludeFile.file = file.startsWith(workspaceRoot)
89+
? relative(workspaceRoot, file)
90+
: file;
91+
return excludeFile;
92+
});
8793
return {
8894
pluginIndex,
8995
excludeFiles,

packages/nx/src/project-graph/utils/project-configuration-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ export async function createProjectConfigurationsWithPlugins(
409409
e
410410
: // This represents a single plugin erroring out with a hard error.
411411
new AggregateCreateNodesError([[null, e]], []);
412-
if (pluginIndex) {
412+
if (pluginIndex !== undefined) {
413413
error.pluginIndex = pluginIndex;
414414
}
415415
formatAggregateCreateNodesError(error, pluginName);

0 commit comments

Comments
 (0)