Skip to content

Commit 253f0ad

Browse files
jaysooFrozenPandaz
authored andcommitted
fix(devkit): always read package version from the actual package in e… (#14623)
(cherry picked from commit 243b742)
1 parent e2acf80 commit 253f0ad

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

packages/devkit/src/utils/package-json.spec.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,6 @@ describe('ensurePackage', () => {
342342
tree = createTree();
343343
});
344344

345-
it('should return without error when dependency is satisfied', async () => {
346-
writeJson(tree, 'package.json', {
347-
devDependencies: {
348-
'@nrwl/vite': '15.0.0',
349-
},
350-
});
351-
352-
await expect(
353-
ensurePackage(tree, '@nrwl/vite', '>=15.0.0', {
354-
throwOnMissing: true,
355-
})
356-
).resolves.toBeUndefined();
357-
});
358-
359345
it('should throw when dependencies are missing', async () => {
360346
writeJson(tree, 'package.json', {});
361347

packages/devkit/src/utils/package-json.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GeneratorCallback } from 'nx/src/config/misc-interfaces';
55
import { clean, coerce, gt, satisfies } from 'semver';
66
import { getPackageManagerCommand } from 'nx/src/utils/package-manager';
77
import { execSync } from 'child_process';
8+
import { readModulePackageJson } from 'nx/src/utils/package-json';
89

910
const NON_SEMVER_TAGS = {
1011
'*': 2,
@@ -346,21 +347,22 @@ export async function ensurePackage(
346347
let version: string;
347348

348349
// Read package and version from root package.json file.
349-
const packageJson = readJson(tree, 'package.json');
350350
const dev = options.dev ?? true;
351351
const throwOnMissing = options.throwOnMissing ?? !!process.env.NX_DRY_RUN; // NX_DRY_RUN is set in `packages/nx/src/command-line/generate.ts`
352352
const pmc = getPackageManagerCommand();
353-
const field = dev ? 'devDependencies' : 'dependencies';
354353

355-
version = packageJson[field]?.[pkg];
354+
// Try to resolve the actual version from resolved module.
355+
try {
356+
version = readModulePackageJson(pkg).packageJson.version;
357+
} catch {
358+
// ignore
359+
}
356360

357-
// If package not found, try to resolve it using Node and get its version.
361+
// Otherwise try to read in from package.json. This is needed for E2E tests to pass.
358362
if (!version) {
359-
try {
360-
version = require(`${pkg}/package.json`).version;
361-
} catch {
362-
// ignore
363-
}
363+
const packageJson = readJson(tree, 'package.json');
364+
const field = dev ? 'devDependencies' : 'dependencies';
365+
version = packageJson[field]?.[pkg];
364366
}
365367

366368
if (!satisfies(version, requiredVersion)) {

0 commit comments

Comments
 (0)