From 85bc540a15524e0b36f6deacf5b6a4f3feff9758 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Sat, 26 Apr 2025 20:12:44 -0400 Subject: [PATCH] =?UTF-8?q?fix(core):=20quick=20fix=20to=20support=20trail?= =?UTF-8?q?ing=20commas=20when=20reading=20hoisted=20=E2=80=A6=20(#29436)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …versions ## Current Behavior Graph fails when there is a trailing comma in a package.json ## Expected Behavior Graph does not fail and works when there is a trailing comma in a package.json ## Related Issue(s) Fixes # --- .../nx/src/plugins/js/lock-file/pnpm-parser.spec.ts | 2 +- .../nx/src/plugins/js/lock-file/utils/package-json.ts | 10 +++++----- .../src/plugins/js/lock-file/utils/pnpm-normalizer.ts | 2 +- .../nx/src/plugins/js/lock-file/yarn-parser.spec.ts | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts b/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts index 5b341239bb..0a0312436c 100644 --- a/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts +++ b/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts @@ -13,7 +13,7 @@ import { } from '../../../project-graph/project-graph-builder'; import { CreateDependenciesContext } from '../../../project-graph/plugins'; -jest.mock('fs', () => { +jest.mock('node:fs', () => { const memFs = require('memfs').fs; return { ...memFs, diff --git a/packages/nx/src/plugins/js/lock-file/utils/package-json.ts b/packages/nx/src/plugins/js/lock-file/utils/package-json.ts index 976535a1ec..ee74b68039 100644 --- a/packages/nx/src/plugins/js/lock-file/utils/package-json.ts +++ b/packages/nx/src/plugins/js/lock-file/utils/package-json.ts @@ -1,6 +1,6 @@ -import { existsSync, readFileSync } from 'fs'; import { PackageJson } from '../../../../utils/package-json'; import { workspaceRoot } from '../../../../utils/workspace-root'; +import { readJsonFile } from '../../../../utils/fileutils'; /** * Get version of hoisted package if available @@ -8,11 +8,11 @@ import { workspaceRoot } from '../../../../utils/workspace-root'; export function getHoistedPackageVersion(packageName: string): string { const fullPath = `${workspaceRoot}/node_modules/${packageName}/package.json`; - if (existsSync(fullPath)) { - const content = readFileSync(fullPath, 'utf-8'); - return JSON.parse(content)?.version; + try { + return readJsonFile(fullPath)?.version; + } catch (e) { + return; } - return; } export type NormalizedPackageJson = Pick< diff --git a/packages/nx/src/plugins/js/lock-file/utils/pnpm-normalizer.ts b/packages/nx/src/plugins/js/lock-file/utils/pnpm-normalizer.ts index c502a62a0f..ecaa930931 100644 --- a/packages/nx/src/plugins/js/lock-file/utils/pnpm-normalizer.ts +++ b/packages/nx/src/plugins/js/lock-file/utils/pnpm-normalizer.ts @@ -16,7 +16,7 @@ import type { ProjectSnapshot, ResolvedDependencies, } from '@pnpm/lockfile-types'; -import { existsSync, readFileSync } from 'fs'; +import { existsSync, readFileSync } from 'node:fs'; import { valid } from 'semver'; import { workspaceRoot } from '../../../../utils/workspace-root'; import { hashObject } from '../../../../hasher/file-hasher'; diff --git a/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts b/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts index 4be5f57196..5a913f1210 100644 --- a/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts +++ b/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts @@ -11,7 +11,7 @@ import { PackageJson } from '../../../utils/package-json'; import { ProjectGraphBuilder } from '../../../project-graph/project-graph-builder'; import { CreateDependenciesContext } from '../../../project-graph/plugins'; -jest.mock('fs', () => { +jest.mock('node:fs', () => { const memFs = require('memfs').fs; return { ...memFs,