From 90ebb2200436321f43c259e4932b91875b00237a Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Tue, 27 Aug 2024 12:44:09 -0400 Subject: [PATCH] fix(gradle): fix find root for projects (#27651) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # (cherry picked from commit 6cb072000984f673d8c8f6a2c0e1182d9471287c) --- e2e/gradle/project.json | 2 +- e2e/gradle/src/gradle.test.ts | 10 +++++----- packages/gradle/src/plugin/dependencies.spec.ts | 17 +++++++++++++---- packages/gradle/src/plugin/dependencies.ts | 12 ++++++++---- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/e2e/gradle/project.json b/e2e/gradle/project.json index 342f9d78374ea..91fb40684f8ea 100644 --- a/e2e/gradle/project.json +++ b/e2e/gradle/project.json @@ -3,7 +3,7 @@ "$schema": "../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "e2e/gradle", "projectType": "application", - "implicitDependencies": ["eslint"], + "implicitDependencies": ["gradle"], "// targets": "to see all targets run: nx show project e2e-gradle --web", "targets": {} } diff --git a/e2e/gradle/src/gradle.test.ts b/e2e/gradle/src/gradle.test.ts index 7625873107813..3fd43d352578b 100644 --- a/e2e/gradle/src/gradle.test.ts +++ b/e2e/gradle/src/gradle.test.ts @@ -34,8 +34,8 @@ describe('Gradle', () => { const buildOutput = runCLI('build app', { verbose: true }); // app depends on list and utilities - expect(buildOutput).toContain('nx run list:build'); - expect(buildOutput).toContain('nx run utilities:build'); + expect(buildOutput).toContain(':list:classes'); + expect(buildOutput).toContain(':utilities:classes'); checkFilesExist( `app/build/libs/app.jar`, @@ -85,9 +85,9 @@ dependencies { return content; } ); - const buildOutput = runCLI('build app2', { verbose: true }); - // app2 depends on app - expect(buildOutput).toContain('nx run app:build'); + expect(() => { + runCLI('build app2', { verbose: true }); + }).not.toThrow(); }); } ); diff --git a/packages/gradle/src/plugin/dependencies.spec.ts b/packages/gradle/src/plugin/dependencies.spec.ts index bd50d496a8a20..6b79e4616351c 100644 --- a/packages/gradle/src/plugin/dependencies.spec.ts +++ b/packages/gradle/src/plugin/dependencies.spec.ts @@ -24,12 +24,18 @@ describe('processGradleDependencies', () => { 'app', { projects: { - 'utilities/number-utils': { + 'number-utils': { + root: 'utilities/number-utils', name: 'number-utils', }, - 'utilities/string-utils': { + 'string-utils': { + root: 'utilities/string-utils', name: 'string-utils', }, + utilities: { + root: 'utilities', + name: 'utilities', + }, }, } as any, dependencies @@ -68,13 +74,16 @@ describe('processGradleDependencies', () => { 'app', { projects: { - 'utilities/number-utils': { + 'number-utils': { + root: 'utilities/number-utils', name: 'number-utils', }, - 'utilities/string-utils': { + 'string-utils': { + root: 'utilities/string-utils', name: 'string-utils', }, utilities: { + root: 'utilities', name: 'utilities', }, }, diff --git a/packages/gradle/src/plugin/dependencies.ts b/packages/gradle/src/plugin/dependencies.ts index bfeb616e3b5f6..ffbad26c728f9 100644 --- a/packages/gradle/src/plugin/dependencies.ts +++ b/packages/gradle/src/plugin/dependencies.ts @@ -35,7 +35,9 @@ export const createDependencies: CreateDependencies = async ( for (const gradleFile of gradleFiles) { const gradleProject = gradleFileToGradleProjectMap.get(gradleFile); - const projectName = context.projects[dirname(gradleFile)].name; + const projectName = Object.values(context.projects).find( + (project) => project.root === dirname(gradleFile) + )?.name; const depsFile = buildFileToDepsMap.get(gradleFile); if (projectName && depsFile) { @@ -125,11 +127,13 @@ export function processGradleDependencies( const targetProjectRoot = gradleProjectNameToProjectRoot.get( gradleProjectName ) as string; - const target = context.projects[targetProjectRoot]?.name; - if (target) { + const targetProjectName = Object.values(context.projects).find( + (project) => project.root === targetProjectRoot + )?.name; + if (targetProjectName) { const dependency: RawProjectGraphDependency = { source: sourceProjectName, - target, + target: targetProjectName, type: DependencyType.static, sourceFile: gradleFile, };