Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): improve packages recognition when the package version is an external package #29529

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions packages/nx/src/project-graph/project-graph-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,76 @@ describe('ProjectGraphBuilder', () => {
packageName: 'external2',
},
});
// originated from: npm:react-helmet-async@npm:@slorber/react-helmet-async@*
builder.addExternalNode({
name: 'npm:external3',
type: 'npm',
data: {
version: 'npm:@company/external3@*',
packageName: 'external3',
},
});
builder.addExternalNode({
name: 'npm:@company1/external3',
type: 'npm',
data: {
version: 'npm:@company2/external3@*',
packageName: '@company1/external3',
},
});
builder.addStaticDependency('npm:external', 'npm:external2');
builder.addStaticDependency(
'npm:external',
'npm:external3@npm:@company/external3@*'
);
builder.addStaticDependency(
'npm:external',
'npm:@company1/external3@npm:@company2/external3@*'
);
builder.addStaticDependency(
'npm:external3@npm:@company/external3@*',
'target'
);
builder.addStaticDependency(
'npm:@company1/external3@npm:@company2/external3@*',
'target'
);
builder.addStaticDependency('source', 'npm:external', 'source/index.ts');
builder.addDynamicDependency('source', 'npm:external2', 'source/second.ts');
const graph = builder.getUpdatedProjectGraph();

expect(graph.dependencies).toMatchInlineSnapshot(`
{
"npm:@company1/external3@npm:@company2/external3@*": [
{
"source": "npm:@company1/external3@npm:@company2/external3@*",
"target": "target",
"type": "static",
},
],
"npm:external": [
{
"source": "npm:external",
"target": "npm:external2",
"type": "static",
},
{
"source": "npm:external",
"target": "npm:external3@npm:@company/external3@*",
"type": "static",
},
{
"source": "npm:external",
"target": "npm:@company1/external3@npm:@company2/external3@*",
"type": "static",
},
],
"npm:external3@npm:@company/external3@*": [
{
"source": "npm:external3@npm:@company/external3@*",
"target": "target",
"type": "static",
},
],
"source": [
{
Expand Down Expand Up @@ -282,12 +339,36 @@ describe('ProjectGraphBuilder', () => {

expect(updatedGraph.dependencies).toMatchInlineSnapshot(`
{
"npm:@company1/external3@npm:@company2/external3@*": [
{
"source": "npm:@company1/external3@npm:@company2/external3@*",
"target": "target",
"type": "static",
},
],
"npm:external": [
{
"source": "npm:external",
"target": "npm:external2",
"type": "static",
},
{
"source": "npm:external",
"target": "npm:external3@npm:@company/external3@*",
"type": "static",
},
{
"source": "npm:external",
"target": "npm:@company1/external3@npm:@company2/external3@*",
"type": "static",
},
],
"npm:external3@npm:@company/external3@*": [
{
"source": "npm:external3@npm:@company/external3@*",
"target": "target",
"type": "static",
},
],
"source": [
{
Expand Down
24 changes: 22 additions & 2 deletions packages/nx/src/project-graph/project-graph-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,12 @@ function validateCommonDependencyRules(
d: RawProjectGraphDependency,
{ externalNodes, projects, fileMap }: CreateDependenciesContext
) {
if (!projects[d.source] && !externalNodes[d.source]) {
if (!projects[d.source] && !checkDependencyExist(d.source, externalNodes)) {
throw new Error(`Source project does not exist: ${d.source}`);
}
if (
!projects[d.target] &&
!externalNodes[d.target] &&
!checkDependencyExist(d.target, externalNodes) &&
!('sourceFile' in d && d.sourceFile)
) {
throw new Error(`Target project does not exist: ${d.target}`);
Expand Down Expand Up @@ -628,3 +628,23 @@ function getFileData(
getNonProjectFileData(sourceFile, nonProjectFiles)
);
}

/**
* Package name might contain external link as a package version
* ("@someCompany/package@npm:@someOtherCompany/package@1.0.0"). In that case
* we won't be able to find such dependency directly within `externalNodes`.
*/
function checkDependencyExist(
dependencyNameAndVersion: string,
externalNodes: CreateDependenciesContext['externalNodes']
) {
const versionPosition = dependencyNameAndVersion.indexOf('@npm:');
const dependencyName = dependencyNameAndVersion.slice(0, versionPosition);

return (
externalNodes[dependencyNameAndVersion] ||
(externalNodes[dependencyName] &&
externalNodes[dependencyName].data.version ===
dependencyNameAndVersion.slice(versionPosition + 1))
);
}