Skip to content

Commit

Permalink
fix(gradle): fix child project deps (#27643)
Browse files Browse the repository at this point in the history
## Current Behavior
<!-- This is the behavior we have today -->

When the name of a gradle project is overwritten by a different plugin,
the plugin throws an error creating dependencies.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

The plugin uses the `CreateDependenciesContext` to get the name of a
project at the project root and uses that to create the dependency.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz authored Aug 26, 2024
1 parent f02e2fc commit 036e5fc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
33 changes: 27 additions & 6 deletions packages/gradle/src/plugin/dependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ describe('processGradleDependencies', () => {
processGradleDependencies(
depFilePath,
new Map([
[':my-utils:number-utils', 'number-utils'],
[':my-utils:string-utils', 'string-utils'],
[':my-utils:number-utils', 'utilities/number-utils'],
[':my-utils:string-utils', 'utilities/string-utils'],
]),
'app',
'app',
{} as any,
{
projects: {
'utilities/number-utils': {
name: 'number-utils',
},
'utilities/string-utils': {
name: 'string-utils',
},
},
} as any,
dependencies
);
expect(Array.from(dependencies)).toEqual([
Expand Down Expand Up @@ -51,13 +60,25 @@ describe('processGradleDependencies', () => {
processGradleDependencies(
depFilePath,
new Map([
[':my-utils:number-utils', 'number-utils'],
[':my-utils:string-utils', 'string-utils'],
[':my-utils:number-utils', 'utilities/number-utils'],
[':my-utils:string-utils', 'utilities/string-utils'],
[':utilities', 'utilities'],
]),
'app',
'app',
{} as any,
{
projects: {
'utilities/number-utils': {
name: 'number-utils',
},
'utilities/string-utils': {
name: 'string-utils',
},
utilities: {
name: 'utilities',
},
},
} as any,
dependencies
);
expect(Array.from(dependencies)).toEqual([
Expand Down
13 changes: 7 additions & 6 deletions packages/gradle/src/plugin/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
validateDependency,
} from '@nx/devkit';
import { readFileSync } from 'node:fs';
import { basename } from 'node:path';
import { basename, dirname } from 'node:path';

import {
GRADLE_BUILD_FILES,
Expand All @@ -27,21 +27,21 @@ export const createDependencies: CreateDependencies = async (
const gradleDependenciesStart = performance.mark('gradleDependencies:start');
const {
gradleFileToGradleProjectMap,
gradleProjectToProjectName,
gradleProjectNameToProjectRootMap,
buildFileToDepsMap,
gradleProjectToChildProjects,
} = getCurrentGradleReport();
const dependencies: Set<RawProjectGraphDependency> = new Set();

for (const gradleFile of gradleFiles) {
const gradleProject = gradleFileToGradleProjectMap.get(gradleFile);
const projectName = gradleProjectToProjectName.get(gradleProject);
const projectName = context.projects[dirname(gradleFile)].name;
const depsFile = buildFileToDepsMap.get(gradleFile);

if (projectName && depsFile) {
processGradleDependencies(
depsFile,
gradleProjectToProjectName,
gradleProjectNameToProjectRootMap,
projectName,
gradleFile,
context,
Expand Down Expand Up @@ -88,7 +88,7 @@ function findGradleFiles(fileMap: FileMap): string[] {

export function processGradleDependencies(
depsFile: string,
gradleProjectToProjectName: Map<string, string>,
gradleProjectNameToProjectRoot: Map<string, string>,
sourceProjectName: string,
gradleFile: string,
context: CreateDependenciesContext,
Expand Down Expand Up @@ -122,9 +122,10 @@ export function processGradleDependencies(
const [_, projectName] = dep.split('-> project');
gradleProjectName = projectName.trim();
}
const target = gradleProjectToProjectName.get(
const targetProjectRoot = gradleProjectNameToProjectRoot.get(
gradleProjectName
) as string;
const target = context.projects[targetProjectRoot]?.name;
if (target) {
const dependency: RawProjectGraphDependency = {
source: sourceProjectName,
Expand Down
9 changes: 9 additions & 0 deletions packages/gradle/src/plugin/nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ describe('@nx/gradle/plugin', () => {
['proj', new Map([['test', 'Verification']])],
]),
gradleProjectToProjectName: new Map<string, string>([['proj', 'proj']]),
gradleProjectNameToProjectRootMap: new Map<string, string>([
['proj', 'proj'],
]),
gradleProjectToChildProjects: new Map<string, string[]>(),
};
cwd = process.cwd();
Expand Down Expand Up @@ -136,6 +139,9 @@ describe('@nx/gradle/plugin', () => {
['proj', new Map([['test', 'Verification']])],
]),
gradleProjectToProjectName: new Map<string, string>([['proj', 'proj']]),
gradleProjectNameToProjectRootMap: new Map<string, string>([
['proj', 'proj'],
]),
gradleProjectToChildProjects: new Map<string, string[]>(),
};
await tempFs.createFiles({
Expand Down Expand Up @@ -218,6 +224,9 @@ describe('@nx/gradle/plugin', () => {
['proj', new Map([['test', 'Test']])],
]),
gradleProjectToProjectName: new Map<string, string>([['proj', 'proj']]),
gradleProjectNameToProjectRootMap: new Map<string, string>([
['proj', 'proj'],
]),
gradleProjectToChildProjects: new Map<string, string[]>(),
};
await tempFs.createFiles({
Expand Down
5 changes: 5 additions & 0 deletions packages/gradle/src/utils/get-gradle-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { combineGlobPatterns } from 'nx/src/utils/globs';

import { execGradleAsync } from './exec-gradle';
import { hashWithWorkspaceContext } from 'nx/src/utils/workspace-context';
import { dirname } from 'path';

export const fileSeparator = process.platform.startsWith('win')
? 'file:///'
Expand All @@ -26,6 +27,7 @@ export interface GradleReport {
gradleFileToOutputDirsMap: Map<string, Map<string, string>>;
gradleProjectToTasksTypeMap: Map<string, Map<string, string>>;
gradleProjectToProjectName: Map<string, string>;
gradleProjectNameToProjectRootMap: Map<string, string>;
gradleProjectToChildProjects: Map<string, string[]>;
}

Expand Down Expand Up @@ -125,6 +127,7 @@ export function processProjectReports(
*/
const gradleProjectToTasksTypeMap = new Map<string, Map<string, string>>();
const gradleProjectToProjectName = new Map<string, string>();
const gradleProjectNameToProjectRootMap = new Map<string, string>();
/**
* Map of buildFile to dependencies report path
*/
Expand Down Expand Up @@ -225,6 +228,7 @@ export function processProjectReports(
gradleFileToOutputDirsMap.set(buildFile, outputDirMap);
gradleFileToGradleProjectMap.set(buildFile, gradleProject);
gradleProjectToProjectName.set(gradleProject, projectName);
gradleProjectNameToProjectRootMap.set(projectName, dirname(buildFile));
}
if (line.endsWith('taskReport')) {
const gradleProject = line.substring(
Expand Down Expand Up @@ -276,6 +280,7 @@ export function processProjectReports(
gradleFileToOutputDirsMap,
gradleProjectToTasksTypeMap,
gradleProjectToProjectName,
gradleProjectNameToProjectRootMap,
gradleProjectToChildProjects,
};
}

0 comments on commit 036e5fc

Please sign in to comment.