Skip to content

Commit

Permalink
feat(core): map lock file data to external dependencies (#12185)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Oct 6, 2022
1 parent 25e2074 commit 5e293eb
Show file tree
Hide file tree
Showing 29 changed files with 1,396 additions and 104 deletions.
21 changes: 12 additions & 9 deletions e2e/js/src/js.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { satisfies } from 'semver';
import {
checkFilesDoNotExist,
checkFilesExist,
Expand Down Expand Up @@ -150,10 +151,12 @@ describe('js e2e', () => {

const rootPackageJson = readJson(`package.json`);

expect(readJson(`dist/libs/${lib}/package.json`)).toHaveProperty(
'peerDependencies.tslib',
rootPackageJson.dependencies.tslib
);
expect(
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 Expand Up @@ -232,12 +235,12 @@ describe('js e2e', () => {

runCLI(`build ${lib}`);

const rootPackageJson = readJson(`package.json`);
const swcHelpersFromRoot =
readJson(`package.json`).dependencies['@swc/helpers'];
const swcHelpersFromDist = readJson(`dist/libs/${lib}/package.json`)
.peerDependencies['@swc/helpers'];

expect(readJson(`dist/libs/${lib}/package.json`)).toHaveProperty(
'peerDependencies.@swc/helpers',
rootPackageJson.dependencies['@swc/helpers']
);
expect(satisfies(swcHelpersFromDist, swcHelpersFromRoot)).toBeTruthy();

updateJson(`libs/${lib}/.lib.swcrc`, (json) => {
json.jsc.externalHelpers = false;
Expand Down
24 changes: 15 additions & 9 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
packageInstall,
promisifiedTreeKill,
readFile,
removeFile,
runCLI,
runCLIAsync,
runCommandUntil,
Expand All @@ -21,6 +20,7 @@ import {
} from '@nrwl/e2e/utils';
import { exec, execSync } from 'child_process';
import * as http from 'http';
import { satisfies } from 'semver';

function getData(port): Promise<any> {
return new Promise((resolve) => {
Expand Down Expand Up @@ -276,19 +276,25 @@ describe('Build Node apps', () => {
);
expect(packageJson).toEqual(
expect.objectContaining({
dependencies: {
'@nestjs/common': '^9.0.0',
'@nestjs/core': '^9.0.0',
'@nestjs/platform-express': '^9.0.0',
'reflect-metadata': '^0.1.13',
rxjs: '^7.0.0',
tslib: '^2.3.0',
},
main: 'main.js',
name: expect.any(String),
version: '0.0.1',
})
);
expect(
satisfies(packageJson.dependencies['@nestjs/common'], '^9.0.0')
).toBeTruthy();
expect(
satisfies(packageJson.dependencies['@nestjs/core'], '^9.0.0')
).toBeTruthy();
expect(
satisfies(packageJson.dependencies['@nestjs/platform-express'], '^9.0.0')
).toBeTruthy();
expect(
satisfies(packageJson.dependencies['reflect-metadata'], '^0.1.13')
).toBeTruthy();
expect(satisfies(packageJson.dependencies['rxjs'], '^7.0.0')).toBeTruthy();
expect(satisfies(packageJson.dependencies['tslib'], '^2.3.0')).toBeTruthy();

const nodeapp = uniq('nodeapp');
runCLI(`generate @nrwl/node:app ${nodeapp}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export const nxVersion = require('../../package.json').version;

export const esbuildVersion = '^0.15.7';
export const swcCliVersion = '~0.1.55';
export const swcHelpersVersion = '~0.3.3';
export const swcHelpersVersion = '~0.4.11';
export const typesNodeVersion = '18.7.1';
10 changes: 9 additions & 1 deletion packages/nx/src/command-line/print-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ async function createTasks(

function serializeProjectGraph(projectGraph: ProjectGraph) {
const nodes = Object.values(projectGraph.nodes).map((n) => n.name);
return { nodes, dependencies: projectGraph.dependencies };
const dependencies = {};
// we don't need external dependencies' dependencies for print-affected
// having them included makes the output unreadable
Object.keys(projectGraph.dependencies).forEach((key) => {
if (!key.startsWith('npm:')) {
dependencies[key] = projectGraph.dependencies[key];
}
});
return { nodes, dependencies };
}

export function selectPrintAffected(wholeJson: any, wholeSelect: string) {
Expand Down
8 changes: 8 additions & 0 deletions packages/nx/src/config/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ export interface ProjectGraphProjectNode<T = any> {

/**
* A node describing an external dependency
* `name` has as form of:
* - `npm:packageName` for root dependencies or
* - `npm:packageName@version` for nested transitive dependencies
*
* This is vital for our node discovery to always point to root dependencies,
* while allowing tracking of the full tree of different nested versions
*
*/
export interface ProjectGraphExternalNode {
type: 'npm';
name: `npm:${string}`;
data: {
version: string;
packageName: string;
hash?: string;
};
}

Expand Down
39 changes: 20 additions & 19 deletions packages/nx/src/hasher/hasher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('Hasher', () => {
root: 'libs/parent',
targets: {
build: {
executor: 'unknown',
inputs: [
'default',
'^default',
Expand All @@ -95,6 +96,7 @@ describe('Hasher', () => {
dependencies: {
parent: [],
},
externalNodes: {},
allWorkspaceFiles,
},
{} as any,
Expand All @@ -110,7 +112,6 @@ describe('Hasher', () => {
overrides: { prop: 'prop-value' },
});

expect(hash.value).toContain('yarn.lock.hash'); //implicits
expect(hash.value).toContain('file.hash'); //project files
expect(hash.value).toContain('prop-value'); //overrides
expect(hash.value).toContain('parent'); //project
Expand All @@ -122,10 +123,8 @@ describe('Hasher', () => {
expect(hash.details.command).toEqual('parent|build||{"prop":"prop-value"}');
expect(hash.details.nodes).toEqual({
'parent:{projectRoot}/**/*':
'/file|file.hash|{"root":"libs/parent","targets":{"build":{"inputs":["default","^default",{"runtime":"echo runtime123"},{"env":"TESTENV"},{"env":"NONEXISTENTENV"}]}}}|{"compilerOptions":{"paths":{"@nrwl/parent":["libs/parent/src/index.ts"],"@nrwl/child":["libs/child/src/index.ts"]}}}',
'{workspaceRoot}/yarn.lock': 'yarn.lock.hash',
'{workspaceRoot}/package-lock.json': 'package-lock.json.hash',
'{workspaceRoot}/pnpm-lock.yaml': 'pnpm-lock.yaml.hash',
'/file|file.hash|{"root":"libs/parent","targets":{"build":{"executor":"unknown","inputs":["default","^default",{"runtime":"echo runtime123"},{"env":"TESTENV"},{"env":"NONEXISTENTENV"}]}}}|{"compilerOptions":{"paths":{"@nrwl/parent":["libs/parent/src/index.ts"],"@nrwl/child":["libs/child/src/index.ts"]}}}',
parent: 'unknown',
'{workspaceRoot}/nx.json': 'nx.json.hash',
'{workspaceRoot}/.gitignore': '',
'{workspaceRoot}/.nxignore': '',
Expand All @@ -145,7 +144,7 @@ describe('Hasher', () => {
type: 'lib',
data: {
root: 'libs/parent',
targets: { build: {} },
targets: { build: { executor: 'unknown' } },
files: [
{ file: '/filea.ts', hash: 'a.hash' },
{ file: '/filea.spec.ts', hash: 'a.spec.hash' },
Expand Down Expand Up @@ -205,6 +204,7 @@ describe('Hasher', () => {
targets: {
build: {
inputs: ['prod', '^prod'],
executor: 'unknown',
},
},
files: [
Expand All @@ -221,7 +221,7 @@ describe('Hasher', () => {
namedInputs: {
prod: ['default'],
},
targets: { build: {} },
targets: { build: { executor: 'unknown' } },
files: [
{ file: 'libs/child/fileb.ts', hash: 'b.hash' },
{ file: 'libs/child/fileb.spec.ts', hash: 'b.spec.hash' },
Expand Down Expand Up @@ -273,10 +273,12 @@ describe('Hasher', () => {
targets: {
build: {
inputs: ['prod'],
executor: 'unknown',
},
test: {
inputs: ['default'],
dependsOn: ['build'],
executor: 'unknown',
},
},
files: [
Expand Down Expand Up @@ -339,6 +341,7 @@ describe('Hasher', () => {
targets: {
test: {
inputs: ['default', '^prod'],
executor: 'unknown',
},
},
files: [
Expand All @@ -362,6 +365,7 @@ describe('Hasher', () => {
targets: {
test: {
inputs: ['default'],
executor: 'unknown',
},
},
files: [
Expand Down Expand Up @@ -440,7 +444,7 @@ describe('Hasher', () => {
data: {
root: 'libs/parent',
targets: {
build: {},
build: { executor: '@nrwl/workspace:run-commands' },
},
files: [
{ file: 'libs/parent/filea.ts', hash: 'a.hash' },
Expand All @@ -453,7 +457,7 @@ describe('Hasher', () => {
type: 'lib',
data: {
root: 'libs/child',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [
{ file: 'libs/child/fileb.ts', hash: 'b.hash' },
{ file: 'libs/child/fileb.spec.ts', hash: 'b.spec.hash' },
Expand Down Expand Up @@ -507,7 +511,7 @@ describe('Hasher', () => {
type: 'lib',
data: {
root: 'libs/parent',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [{ file: '/file', hash: 'file.hash' }],
},
},
Expand All @@ -531,7 +535,6 @@ describe('Hasher', () => {
overrides: { prop: 'prop-value' },
});

expect(hash.value).toContain('yarn.lock.hash'); //implicits
expect(hash.value).toContain('file.hash'); //project files
expect(hash.value).toContain('prop-value'); //overrides
expect(hash.value).toContain('parent'); //project
Expand All @@ -557,7 +560,7 @@ describe('Hasher', () => {
type: 'lib',
data: {
root: 'libs/parent',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [{ file: '/filea.ts', hash: 'a.hash' }],
},
},
Expand All @@ -566,7 +569,7 @@ describe('Hasher', () => {
type: 'lib',
data: {
root: 'libs/child',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [{ file: '/fileb.ts', hash: 'b.hash' }],
},
},
Expand All @@ -588,7 +591,6 @@ describe('Hasher', () => {
overrides: { prop: 'prop-value' },
});

expect(tasksHash.value).toContain('yarn.lock.hash'); //implicits
expect(tasksHash.value).toContain('a.hash'); //project files
expect(tasksHash.value).toContain('b.hash'); //project files
expect(tasksHash.value).toContain('prop-value'); //overrides
Expand All @@ -612,7 +614,6 @@ describe('Hasher', () => {
overrides: { prop: 'prop-value' },
});

expect(hashb.value).toContain('yarn.lock.hash'); //implicits
expect(hashb.value).toContain('a.hash'); //project files
expect(hashb.value).toContain('b.hash'); //project files
expect(hashb.value).toContain('prop-value'); //overrides
Expand Down Expand Up @@ -640,7 +641,7 @@ describe('Hasher', () => {
type: 'lib',
data: {
root: 'libs/parent',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [{ file: '/file', hash: 'some-hash' }],
},
},
Expand Down Expand Up @@ -682,7 +683,7 @@ describe('Hasher', () => {
type: 'lib',
data: {
root: 'libs/parents',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [],
},
},
Expand Down Expand Up @@ -718,7 +719,7 @@ describe('Hasher', () => {
type: 'app',
data: {
root: 'apps/app',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [{ file: '/filea.ts', hash: 'a.hash' }],
},
},
Expand Down Expand Up @@ -767,7 +768,7 @@ describe('Hasher', () => {
type: 'app',
data: {
root: 'apps/app',
targets: { build: {} },
targets: { build: { executor: '@nrwl/workspace:run-commands' } },
files: [{ file: '/filea.ts', hash: 'a.hash' }],
},
},
Expand Down
Loading

1 comment on commit 5e293eb

@vercel
Copy link

@vercel vercel bot commented on 5e293eb Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.