fix(devkit): always read package version from the actual package in e… (#14623)

This commit is contained in:
Jack Hsu 2023-01-25 17:56:37 -05:00 committed by GitHub
parent f3376419ac
commit 243b7423f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 23 deletions

View File

@ -342,20 +342,6 @@ describe('ensurePackage', () => {
tree = createTree();
});
it('should return without error when dependency is satisfied', async () => {
writeJson(tree, 'package.json', {
devDependencies: {
'@nrwl/vite': '15.0.0',
},
});
await expect(
ensurePackage(tree, '@nrwl/vite', '>=15.0.0', {
throwOnMissing: true,
})
).resolves.toBeUndefined();
});
it('should throw when dependencies are missing', async () => {
writeJson(tree, 'package.json', {});

View File

@ -5,6 +5,7 @@ import { GeneratorCallback } from 'nx/src/config/misc-interfaces';
import { clean, coerce, gt, satisfies } from 'semver';
import { getPackageManagerCommand } from 'nx/src/utils/package-manager';
import { execSync } from 'child_process';
import { readModulePackageJson } from 'nx/src/utils/package-json';
const NON_SEMVER_TAGS = {
'*': 2,
@ -346,21 +347,22 @@ export async function ensurePackage(
let version: string;
// Read package and version from root package.json file.
const packageJson = readJson(tree, 'package.json');
const dev = options.dev ?? true;
const throwOnMissing = options.throwOnMissing ?? !!process.env.NX_DRY_RUN; // NX_DRY_RUN is set in `packages/nx/src/command-line/generate.ts`
const pmc = getPackageManagerCommand();
const field = dev ? 'devDependencies' : 'dependencies';
version = packageJson[field]?.[pkg];
// Try to resolve the actual version from resolved module.
try {
version = readModulePackageJson(pkg).packageJson.version;
} catch {
// ignore
}
// If package not found, try to resolve it using Node and get its version.
// Otherwise try to read in from package.json. This is needed for E2E tests to pass.
if (!version) {
try {
version = require(`${pkg}/package.json`).version;
} catch {
// ignore
}
const packageJson = readJson(tree, 'package.json');
const field = dev ? 'devDependencies' : 'dependencies';
version = packageJson[field]?.[pkg];
}
if (!satisfies(version, requiredVersion)) {