Skip to content

Commit

Permalink
fix(core): use package.json version in yarn parser when node_modules …
Browse files Browse the repository at this point in the history
…are unavailable (#18121)

(cherry picked from commit d9a95b6)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Jul 20, 2023
1 parent 445a527 commit 81a661c
Show file tree
Hide file tree
Showing 3 changed files with 452 additions and 81 deletions.
10 changes: 6 additions & 4 deletions packages/nx/src/plugins/js/lock-file/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import { workspaceRoot } from '../../../utils/workspace-root';
import { ProjectGraph } from '../../../config/project-graph';
import { ProjectGraphBuilder } from '../../../project-graph/project-graph-builder';
import { PackageJson } from '../../../utils/package-json';
import { fileHasher, hashArray } from '../../../hasher/file-hasher';
import { hashArray } from '../../../hasher/file-hasher';
import { output } from '../../../utils/output';

import { parseNpmLockfile, stringifyNpmLockfile } from './npm-parser';
import { parsePnpmLockfile, stringifyPnpmLockfile } from './pnpm-parser';
import { parseYarnLockfile, stringifyYarnLockfile } from './yarn-parser';
import { pruneProjectGraph } from './project-graph-pruning';
import { normalizePackageJson } from './utils/package-json';
import { readJsonFile } from '../../../utils/fileutils';

const YARN_LOCK_FILE = 'yarn.lock';
const NPM_LOCK_FILE = 'package-lock.json';
Expand Down Expand Up @@ -86,7 +87,8 @@ export function parseLockFile(
try {
if (packageManager === 'yarn') {
const content = readFileSync(YARN_LOCK_PATH, 'utf8');
parseYarnLockfile(content, builder);
const packageJson = readJsonFile('package.json');
parseYarnLockfile(content, packageJson, builder);
return builder.getUpdatedProjectGraph();
}
if (packageManager === 'pnpm') {
Expand Down Expand Up @@ -145,19 +147,19 @@ export function createLockFile(
): string {
const normalizedPackageJson = normalizePackageJson(packageJson);
const content = readFileSync(getLockFileName(packageManager), 'utf8');
const rootPackageJson = readJsonFile('package.json');

const builder = new ProjectGraphBuilder();

try {
if (packageManager === 'yarn') {
parseYarnLockfile(content, builder);
parseYarnLockfile(content, rootPackageJson, builder);
const graph = builder.getUpdatedProjectGraph();
const prunedGraph = pruneProjectGraph(graph, packageJson);
return stringifyYarnLockfile(prunedGraph, content, normalizedPackageJson);
}
if (packageManager === 'pnpm') {
parsePnpmLockfile(content, builder);

const graph = builder.getUpdatedProjectGraph();
const prunedGraph = pruneProjectGraph(graph, packageJson);
return stringifyPnpmLockfile(prunedGraph, content, normalizedPackageJson);
Expand Down
Loading

0 comments on commit 81a661c

Please sign in to comment.