Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions packages/devkit/src/utils/package-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,20 +318,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', {});

Expand Down
20 changes: 11 additions & 9 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -317,21 +318,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)) {
Expand Down