Skip to content

Commit

Permalink
fix(core): plugin should not override existing external nodes (#15219)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9a014c7)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Mar 2, 2023
1 parent fd71c6e commit f342bac
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
4 changes: 2 additions & 2 deletions e2e/js/src/js-swc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { satisfies } from 'semver';
import { execSync } from 'child_process';
import { writeFileSync } from 'fs-extra';
import {
checkFilesDoNotExist,
checkFilesExist,
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('js e2e', () => {
const swcHelpersFromDist = readJson(`dist/libs/${lib}/package.json`)
.peerDependencies['@swc/helpers'];

expect(swcHelpersFromDist).toEqual(swcHelpersFromRoot);
expect(satisfies(swcHelpersFromDist, swcHelpersFromRoot)).toBeTruthy();

updateJson(`libs/${lib}/.swcrc`, (json) => {
json.jsc.externalHelpers = false;
Expand Down
7 changes: 5 additions & 2 deletions e2e/js/src/js-tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ describe('js e2e', () => {
const rootPackageJson = readJson(`package.json`);

expect(
readJson(`dist/libs/${lib}/package.json`).peerDependencies.tslib
).toEqual(rootPackageJson.dependencies.tslib);
satisfies(
readJson(`dist/libs/${lib}/package.json`).peerDependencies.tslib,
rootPackageJson.dependencies.tslib
)
).toBeTruthy();

updateJson(`libs/${lib}/tsconfig.json`, (json) => {
json.compilerOptions = { ...json.compilerOptions, importHelpers: false };
Expand Down
46 changes: 31 additions & 15 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { exec, execSync } from 'child_process';
import * as http from 'http';
import { getLockFileName } from 'nx/src/lock-file/lock-file';
import { satisfies } from 'semver';

function getData(port, path = '/api'): Promise<any> {
return new Promise((resolve) => {
Expand Down Expand Up @@ -263,21 +264,36 @@ describe('Build Node apps', () => {
})
);

expect(packageJson.dependencies['@nestjs/common']).toEqual(
rootPackageJson.dependencies['@nestjs/common']
);
expect(packageJson.dependencies['@nestjs/core']).toEqual(
rootPackageJson.dependencies['@nestjs/core']
);
expect(packageJson.dependencies['reflect-metadata']).toEqual(
rootPackageJson.dependencies['reflect-metadata']
);
expect(packageJson.dependencies['rxjs']).toEqual(
rootPackageJson.dependencies['rxjs']
);
expect(packageJson.dependencies['tslib']).toEqual(
rootPackageJson.dependencies['tslib']
);
expect(
satisfies(
packageJson.dependencies['@nestjs/common'],
rootPackageJson.dependencies['@nestjs/common']
)
).toBeTruthy();
expect(
satisfies(
packageJson.dependencies['@nestjs/core'],
rootPackageJson.dependencies['@nestjs/core']
)
).toBeTruthy();
expect(
satisfies(
packageJson.dependencies['reflect-metadata'],
rootPackageJson.dependencies['reflect-metadata']
)
).toBeTruthy();
expect(
satisfies(
packageJson.dependencies['rxjs'],
rootPackageJson.dependencies['rxjs']
)
).toBeTruthy();
expect(
satisfies(
packageJson.dependencies['tslib'],
rootPackageJson.dependencies['tslib']
)
).toBeTruthy();

checkFilesExist(
`dist/apps/${nestapp}/${packageManagerLockFile[packageManager]}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync } from 'fs';
import { defaultHashing } from '../../../../hasher/hashing-impl';
import { join } from 'path';

import { ProjectGraphBuilder } from '../../../../project-graph/project-graph-builder';
Expand All @@ -16,13 +17,16 @@ export function buildNpmPackageNodes(builder: ProjectGraphBuilder) {
...packageJson.devDependencies,
};
Object.keys(deps).forEach((d) => {
builder.addExternalNode({
type: 'npm',
name: `npm:${d}`,
data: {
version: deps[d],
packageName: d,
},
});
if (!builder.graph.externalNodes[`npm:${d}`]) {
builder.addExternalNode({
type: 'npm',
name: `npm:${d}`,
data: {
version: deps[d],
packageName: d,
hash: defaultHashing.hashArray([d, deps[d]]),
},
});
}
});
}
10 changes: 10 additions & 0 deletions packages/nx/src/project-graph/project-graph-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ export class ProjectGraphBuilder {
* Adds a external node to the project graph
*/
addExternalNode(node: ProjectGraphExternalNode): void {
// Check if project with the same name already exists
if (this.graph.externalNodes[node.name]) {
throw new Error(
`Multiple projects are named "${node.name}". One has version "${
node.data.version
}" and the other has version "${
this.graph.externalNodes[node.name].data.version
}". Please resolve the conflicting package names.`
);
}
this.graph.externalNodes[node.name] = node;
}

Expand Down

0 comments on commit f342bac

Please sign in to comment.