Skip to content

Commit

Permalink
feat(core): store lock file data in nx cache
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Sep 27, 2022
1 parent 951fbd1 commit b8fd520
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock('nx/src/utils/workspace-root', () => ({

describe('project graph', () => {
let packageJson: any;
let packageLockJson: any;
let workspaceJson: WorkspaceJsonConfiguration;
let tsConfigJson: any;
let nxJson: NxJsonConfiguration;
Expand All @@ -24,6 +25,7 @@ describe('project graph', () => {
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
packageJson = {
name: '@nrwl/workspace-src',
version: '0.0.0',
scripts: {
deploy: 'echo deploy',
},
Expand All @@ -34,6 +36,43 @@ describe('project graph', () => {
'@nrwl/workspace': '8.0.0',
},
};
packageLockJson = {
name: '@nrwl/workspace-src',
version: '0.0.0',
lockfileVersion: 2,
requires: true,
packages: {
'': packageJson,
'node_modules/@nrwl/workspace': {
version: '15.0.0',
resolved:
'https://registry.npmjs.org/@nrwl/workspace/-/@nrwl/workspace-15.0.0.tgz',
integrity: 'sha512-12345678==',
dev: true,
},
'node_modules/happy-nrwl': {
version: '4.0.0',
resolved:
'https://registry.npmjs.org/happy-nrwl/-/happy-nrwl-1.0.0.tgz',
integrity: 'sha512-12345678==',
},
},
dependencies: {
'@nrwl/workspace': {
version: '15.0.0',
resolved:
'https://registry.npmjs.org/@nrwl/workspace/-/@nrwl/workspace-15.0.0.tgz',
integrity: 'sha512-12345678==',
dev: true,
},
'happy-nrwl': {
version: '1.0.0',
resolved:
'https://registry.npmjs.org/happy-nrwl/-/happy-nrwl-1.0.0.tgz',
integrity: 'sha512-12345678==',
},
},
};
workspaceJson = {
version: 2,
projects: {
Expand Down Expand Up @@ -110,6 +149,7 @@ describe('project graph', () => {
import * as happyNrwl from 'happy-nrwl';
`,
'./package.json': JSON.stringify(packageJson),
'./package-lock.json': JSON.stringify(packageLockJson),
'./nx.json': JSON.stringify(nxJson),
'./workspace.json': JSON.stringify(workspaceJson),
'./tsconfig.base.json': JSON.stringify(tsConfigJson),
Expand Down
53 changes: 53 additions & 0 deletions packages/nx/src/project-graph/build-project-graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DependencyType } from '../config/project-graph';

describe('project graph', () => {
let packageJson: any;
let packageLockJson: any;
let workspaceJson: WorkspaceJsonConfiguration;
let nxJson: NxJsonConfiguration;
let tsConfigJson: any;
Expand All @@ -22,6 +23,7 @@ describe('project graph', () => {
defaultFileHasher.ensureInitialized();
packageJson = {
name: '@nrwl/workspace-src',
version: '0.0.0',
dependencies: {
express: '4.0.0',
'happy-nrwl': '1.0.0',
Expand All @@ -30,6 +32,56 @@ describe('project graph', () => {
'@nrwl/workspace': '*',
},
};
packageLockJson = {
name: '@nrwl/workspace-src',
version: '0.0.0',
lockfileVersion: 2,
requires: true,
packages: {
'': packageJson,
'node_modules/@nrwl/workspace': {
version: '15.0.0',
resolved:
'https://registry.npmjs.org/@nrwl/workspace/-/@nrwl/workspace-15.0.0.tgz',
integrity: 'sha512-12345678==',
dev: true,
},
'node_modules/express': {
version: '4.0.0',
resolved: 'https://registry.npmjs.org/express/-/express-4.0.0.tgz',
integrity: 'sha512-12345678==',
engines: {
node: '>=4.2.0',
},
},
'node_modules/happy-nrwl': {
version: '4.0.0',
resolved:
'https://registry.npmjs.org/happy-nrwl/-/happy-nrwl-1.0.0.tgz',
integrity: 'sha512-12345678==',
},
},
dependencies: {
'@nrwl/workspace': {
version: '15.0.0',
resolved:
'https://registry.npmjs.org/@nrwl/workspace/-/@nrwl/workspace-15.0.0.tgz',
integrity: 'sha512-12345678==',
dev: true,
},
express: {
version: '4.0.0',
resolved: 'https://registry.npmjs.org/express/-/express-4.0.0.tgz',
integrity: 'sha512-12345678==',
},
'happy-nrwl': {
version: '1.0.0',
resolved:
'https://registry.npmjs.org/happy-nrwl/-/happy-nrwl-1.0.0.tgz',
integrity: 'sha512-12345678==',
},
},
};
workspaceJson = {
version: 2,
projects: {
Expand Down Expand Up @@ -125,6 +177,7 @@ describe('project graph', () => {
export const LAZY = 'lazy lib';
`,
'./package.json': JSON.stringify(packageJson),
'./package-lock.json': JSON.stringify(packageLockJson),
'./nx.json': JSON.stringify(nxJson),
'./workspace.json': JSON.stringify(workspaceJson),
'./tsconfig.base.json': JSON.stringify(tsConfigJson),
Expand Down
10 changes: 9 additions & 1 deletion packages/nx/src/project-graph/build-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
readAllWorkspaceConfiguration,
readNxJson,
} from '../config/configuration';
import { lockFileHash, parseLockFile } from '../utils/lock-file/lock-file';

export async function buildProjectGraph() {
const projectConfigurations = readAllWorkspaceConfiguration();
Expand Down Expand Up @@ -95,6 +96,12 @@ export async function buildProjectGraphUsingProjectFileMap(
filesToProcess = projectFileMap;
cachedFileData = {};
}
let lockFileData;
if (cache && cache.lockFile && cache.lockFile.hash === lockFileHash()) {
lockFileData = cache.lockFile;
} else {
lockFileData = parseLockFile();
}
const context = createContext(
projectsConfigurations,
nxJson,
Expand All @@ -112,7 +119,8 @@ export async function buildProjectGraphUsingProjectFileMap(
nxJson,
packageJsonDeps,
projectGraph,
rootTsConfig
rootTsConfig,
lockFileData
);
if (shouldWriteCache) {
writeCache(projectGraphCache);
Expand Down
11 changes: 8 additions & 3 deletions packages/nx/src/project-graph/nx-deps-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ProjectGraphCache,
shouldRecomputeWholeGraph,
} from './nx-deps-cache';
import { createCache } from './nx-deps-cache';
import { ProjectGraph } from '../config/project-graph';
import { WorkspaceJsonConfiguration } from '../config/workspace-json-project-json';
import { NxJsonConfiguration } from '../config/nx-json';
Expand Down Expand Up @@ -299,7 +298,8 @@ describe('nx deps utils', () => {
createNxJson({}),
createPackageJsonDeps({}),
createCache({}) as ProjectGraph,
{}
{},
{ dependencies: {}, hash: '' }
);
});

Expand All @@ -308,7 +308,8 @@ describe('nx deps utils', () => {
createNxJson({}),
createPackageJsonDeps({}),
createCache({}) as ProjectGraph,
undefined
undefined,
{ dependencies: {}, hash: '' }
);

expect(result).toBeDefined();
Expand All @@ -322,6 +323,10 @@ describe('nx deps utils', () => {
'@nrwl/workspace': '12.0.0',
plugin: '1.0.0',
},
lockFile: {
dependencies: {},
hash: '',
},
pathMappings: {
mylib: ['libs/mylib/index.ts'],
},
Expand Down
6 changes: 5 additions & 1 deletion packages/nx/src/project-graph/nx-deps-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import {
import { readJsonFile, writeJsonFile } from '../utils/fileutils';
import { NxJsonConfiguration } from '../config/nx-json';
import { ProjectsConfigurations } from '../config/workspace-json-project-json';
import { LockFileData } from '../utils/lock-file/lock-file-type';

export interface ProjectGraphCache {
version: string;
deps: Record<string, string>;
lockFile: LockFileData;
pathMappings: Record<string, any>;
nxJsonPlugins: { name: string; version: string }[];
pluginsConfig?: any;
Expand Down Expand Up @@ -81,7 +83,8 @@ export function createCache(
nxJson: NxJsonConfiguration<'*' | string[]>,
packageJsonDeps: Record<string, string>,
projectGraph: ProjectGraph,
tsConfig: { compilerOptions?: { paths?: { [p: string]: any } } }
tsConfig: { compilerOptions?: { paths?: { [p: string]: any } } },
lockFileData: LockFileData
) {
const nxJsonPlugins = (nxJson.plugins || []).map((p) => ({
name: p,
Expand All @@ -90,6 +93,7 @@ export function createCache(
const newValue: ProjectGraphCache = {
version: projectGraph.version || '5.0',
deps: packageJsonDeps,
lockFile: lockFileData,
// compilerOptions may not exist, especially for repos converted through add-nx-to-monorepo
pathMappings: tsConfig?.compilerOptions?.paths || {},
nxJsonPlugins,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export function projectGraphAdapter(
if (sourceVersion === targetVersion) {
return projectGraph;
}
if (sourceVersion === '5.0' && targetVersion === '4.0') {
if (+sourceVersion >= 5 && targetVersion === '4.0') {
return projectGraphCompat5to4(projectGraph as ProjectGraph);
}
throw new Error(
Expand Down
16 changes: 9 additions & 7 deletions packages/nx/src/utils/lock-file/lock-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, writeFileSync } from 'fs-extra';
import { readFileSync, writeFileSync } from 'fs';
import { detectPackageManager, PackageManager } from '../package-manager';
import {
parseYarnLockFile,
Expand All @@ -17,19 +17,21 @@ import {
} from './pnpm';
import { LockFileData } from './lock-file-type';
import { hashLockFile } from './utils';
import { workspaceRoot } from '../workspace-root';
import { join } from 'path';

export function lockFileHash(
packageManager: PackageManager = detectPackageManager()
): string {
let file: string;
if (packageManager === 'yarn') {
file = readFileSync('yarn.lock', 'utf8');
file = readFileSync(join(workspaceRoot, 'yarn.lock'), 'utf8');
}
if (packageManager === 'pnpm') {
file = readFileSync('pnpm-lock.yaml', 'utf8');
file = readFileSync(join(workspaceRoot, 'pnpm-lock.yaml'), 'utf8');
}
if (packageManager === 'npm') {
file = readFileSync('package-lock.json', 'utf8');
file = readFileSync(join(workspaceRoot, 'package-lock.json'), 'utf8');
}
if (file) {
return hashLockFile(file);
Expand All @@ -47,15 +49,15 @@ export function parseLockFile(
packageManager: PackageManager = detectPackageManager()
): LockFileData {
if (packageManager === 'yarn') {
const file = readFileSync('yarn.lock', 'utf8');
const file = readFileSync(join(workspaceRoot, 'yarn.lock'), 'utf8');
return parseYarnLockFile(file);
}
if (packageManager === 'pnpm') {
const file = readFileSync('pnpm-lock.yaml', 'utf8');
const file = readFileSync(join(workspaceRoot, 'pnpm-lock.yaml'), 'utf8');
return parsePnpmLockFile(file);
}
if (packageManager === 'npm') {
const file = readFileSync('package-lock.json', 'utf8');
const file = readFileSync(join(workspaceRoot, 'package-lock.json'), 'utf8');
return parseNpmLockFile(file);
}
throw Error(`Unknown package manager: ${packageManager}`);
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/utils/lock-file/npm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { defaultHashing } from '../../hasher/hashing-impl';
import { LockFileData, PackageDependency } from './lock-file-type';
import { hashLockFile, sortObject } from './utils';

Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/utils/lock-file/pnpm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LockFileData, PackageDependency } from './lock-file-type';
import { load, dump } from '@zkochan/js-yaml';
import { hashLockFile, sortObject } from './utils';
import { defaultHashing } from '../../hasher/hashing-impl';

type PackageMeta = {
key: string;
Expand Down

0 comments on commit b8fd520

Please sign in to comment.