From 2a0d89ddc306d36f53703267176b0c55a45e6c44 Mon Sep 17 00:00:00 2001 From: Roman Lorenzo Balayan Date: Thu, 20 Mar 2025 01:35:27 +0800 Subject: [PATCH] fix(gradle): build nx graph for gradle projects regardless of build gradle file location (#29783) (#29802) ## Current Behavior 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 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) Fixes #29783 --- e2e/gradle/src/gradle-import.test.ts | 6 ++++++ .../gradle/src/utils/get-project-report-lines.ts | 12 +----------- .../implementation/check-compatible-with-plugins.ts | 10 ++++++++-- .../utils/project-configuration-utils.ts | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/e2e/gradle/src/gradle-import.test.ts b/e2e/gradle/src/gradle-import.test.ts index 18f14d75d6..67cbe04d78 100644 --- a/e2e/gradle/src/gradle-import.test.ts +++ b/e2e/gradle/src/gradle-import.test.ts @@ -184,6 +184,12 @@ describe('Nx Import Gradle', () => { `${directory}/gradlew`, `${directory}/gradlew.bat` ); + const nxJson = readJson('nx.json'); + const gradlePlugin = nxJson.plugins.find( + (plugin) => plugin.plugin === '@nx/gradle' + ); + gradlePlugin.exclude = []; + updateJson('nx.json', () => nxJson); expect(() => { runCLI(`show projects`); runCLI('build groovy-app'); diff --git a/packages/gradle/src/utils/get-project-report-lines.ts b/packages/gradle/src/utils/get-project-report-lines.ts index 319b4d40d9..3c5ae4606b 100644 --- a/packages/gradle/src/utils/get-project-report-lines.ts +++ b/packages/gradle/src/utils/get-project-report-lines.ts @@ -22,17 +22,7 @@ export async function getProjectReportLines( ): Promise { let projectReportBuffer: Buffer; - // if there is no build.gradle or build.gradle.kts file, we cannot run the projectReport nor projectReportAll task - if ( - !existsSync(join(dirname(gradlewFile), 'build.gradle')) && - !existsSync(join(dirname(gradlewFile), 'build.gradle.kts')) - ) { - logger.warn( - `Could not find build file near ${gradlewFile}. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks.` - ); - return []; - } - + // Attempt to run projectReport or projectReportAll task, regardless of build.gradle or build.gradle.kts location try { projectReportBuffer = await execGradleAsync(gradlewFile, [ 'projectReportAll', diff --git a/packages/nx/src/command-line/init/implementation/check-compatible-with-plugins.ts b/packages/nx/src/command-line/init/implementation/check-compatible-with-plugins.ts index 3f2ccf40c2..9cb3d51121 100644 --- a/packages/nx/src/command-line/init/implementation/check-compatible-with-plugins.ts +++ b/packages/nx/src/command-line/init/implementation/check-compatible-with-plugins.ts @@ -1,5 +1,5 @@ import { existsSync } from 'node:fs'; -import { join } from 'node:path'; +import { join, relative } from 'node:path'; import { bold } from 'chalk'; import { NxJsonConfiguration } from '../../../config/nx-json'; @@ -83,7 +83,13 @@ function findPluginAndFilesWithError( }, ]; } - excludeFiles = excludeFiles.filter(Boolean); + excludeFiles = excludeFiles.filter(Boolean).map((excludeFile) => { + const file = excludeFile.file; + excludeFile.file = file.startsWith(workspaceRoot) + ? relative(workspaceRoot, file) + : file; + return excludeFile; + }); return { pluginIndex, excludeFiles, diff --git a/packages/nx/src/project-graph/utils/project-configuration-utils.ts b/packages/nx/src/project-graph/utils/project-configuration-utils.ts index 03acafe6b7..7b259671cf 100644 --- a/packages/nx/src/project-graph/utils/project-configuration-utils.ts +++ b/packages/nx/src/project-graph/utils/project-configuration-utils.ts @@ -409,7 +409,7 @@ export async function createProjectConfigurationsWithPlugins( e : // This represents a single plugin erroring out with a hard error. new AggregateCreateNodesError([[null, e]], []); - if (pluginIndex) { + if (pluginIndex !== undefined) { error.pluginIndex = pluginIndex; } formatAggregateCreateNodesError(error, pluginName);