Skip to content

Commit

Permalink
fix(core): cross-workspace implicitDependencies should be safely igno…
Browse files Browse the repository at this point in the history
…red (#28845)
  • Loading branch information
JamesHenry authored Nov 11, 2024
1 parent cc251e4 commit 0fd3442
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
42 changes: 42 additions & 0 deletions e2e/nx/src/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,45 @@ describe('global installation', () => {
});
});
});

describe('cross-workspace implicit dependencies', () => {
beforeAll(() =>
newProject({
packages: ['@nx/js'],
})
);

afterAll(() => cleanupProject());

it('should successfully build a project graph when cross-workspace implicit dependencies are present', () => {
const npmPackage = uniq('npm-package');
runCLI(`generate @nx/workspace:npm-package ${npmPackage}`);

function setImplicitDependencies(deps: string[]) {
updateFile(join(npmPackage, 'package.json'), (content) => {
const json = JSON.parse(content);
json.nx = {
...json.nx,
implicitDependencies: deps,
};
return JSON.stringify(json, null, 2);
});
}

// First set the implicit dependencies to an intentionally invalid value to prove the command fails during project graph construction
setImplicitDependencies(['this-project-does-not-exist']);
expect(
runCLI(`test ${npmPackage}`, {
silenceError: true,
})
).toContain('Failed to process project graph');

// Now set the implicit dependencies to a cross-workspace reference to prove that it is valid, despite not being resolvable in the current workspace
setImplicitDependencies(['nx-cloud:another-workspace']);
expect(
runCLI(`test ${npmPackage}`, {
silenceError: true,
})
).toContain('Successfully ran target test');
});
});
5 changes: 4 additions & 1 deletion packages/nx/src/utils/assert-workspace-validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ function detectAndSetInvalidProjectGlobValues(
const projectName = implicit.startsWith('!')
? implicit.substring(1)
: implicit;

// Do not error on cross-workspace implicit dependency references
if (projectName.startsWith('nx-cloud:')) {
return false;
}
return !(
projectConfigurations[projectName] ||
findMatchingProjects([implicit], projects).length
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export function findMatchingProjects(
}

for (const stringPattern of patterns) {
if (!stringPattern.length) {
// Do not waste time attempting to look up cross-workspace references which will never match
if (!stringPattern.length || stringPattern.startsWith('nx-cloud:')) {
continue;
}

Expand Down

0 comments on commit 0fd3442

Please sign in to comment.