Skip to content

Commit 85bc540

Browse files
authored
fix(core): quick fix to support trailing commas when reading hoisted … (#29436)
…versions <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> Graph fails when there is a trailing comma in a package.json ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Graph does not fail and works when there is a trailing comma in a package.json ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent da4f55b commit 85bc540

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '../../../project-graph/project-graph-builder';
1414
import { CreateDependenciesContext } from '../../../project-graph/plugins';
1515

16-
jest.mock('fs', () => {
16+
jest.mock('node:fs', () => {
1717
const memFs = require('memfs').fs;
1818
return {
1919
...memFs,

packages/nx/src/plugins/js/lock-file/utils/package-json.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { existsSync, readFileSync } from 'fs';
21
import { PackageJson } from '../../../../utils/package-json';
32
import { workspaceRoot } from '../../../../utils/workspace-root';
3+
import { readJsonFile } from '../../../../utils/fileutils';
44

55
/**
66
* Get version of hoisted package if available
77
*/
88
export function getHoistedPackageVersion(packageName: string): string {
99
const fullPath = `${workspaceRoot}/node_modules/${packageName}/package.json`;
1010

11-
if (existsSync(fullPath)) {
12-
const content = readFileSync(fullPath, 'utf-8');
13-
return JSON.parse(content)?.version;
11+
try {
12+
return readJsonFile(fullPath)?.version;
13+
} catch (e) {
14+
return;
1415
}
15-
return;
1616
}
1717

1818
export type NormalizedPackageJson = Pick<

packages/nx/src/plugins/js/lock-file/utils/pnpm-normalizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
ProjectSnapshot,
1717
ResolvedDependencies,
1818
} from '@pnpm/lockfile-types';
19-
import { existsSync, readFileSync } from 'fs';
19+
import { existsSync, readFileSync } from 'node:fs';
2020
import { valid } from 'semver';
2121
import { workspaceRoot } from '../../../../utils/workspace-root';
2222
import { hashObject } from '../../../../hasher/file-hasher';

packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { PackageJson } from '../../../utils/package-json';
1111
import { ProjectGraphBuilder } from '../../../project-graph/project-graph-builder';
1212
import { CreateDependenciesContext } from '../../../project-graph/plugins';
1313

14-
jest.mock('fs', () => {
14+
jest.mock('node:fs', () => {
1515
const memFs = require('memfs').fs;
1616
return {
1717
...memFs,

0 commit comments

Comments
 (0)