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(core): update ensurePackage util with workaround for bad module cache #14786

Merged
merged 1 commit into from
Feb 7, 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
10 changes: 10 additions & 0 deletions packages/devkit/src/utils/package-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ describe('ensurePackage', () => {
tree = createTree();
});

it('should return successfully when package is present', async () => {
writeJson(tree, 'package.json', {});

await expect(
ensurePackage(tree, '@nrwl/devkit', '>=15.0.0', {
throwOnMissing: true,
})
).resolves.toBeUndefined(); // return void
});

it('should throw when dependencies are missing', async () => {
writeJson(tree, 'package.json', {});

Expand Down
49 changes: 37 additions & 12 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { execSync } from 'child_process';
import { readJson, updateJson } from 'nx/src/generators/utils/json';
import { installPackagesTask } from '../tasks/install-packages-task';
import type { Tree } from 'nx/src/generators/tree';
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';
import { workspaceRoot } from 'nx/src/utils/workspace-root';

import { installPackagesTask } from '../tasks/install-packages-task';

const UNIDENTIFIED_VERSION = 'UNIDENTIFIED_VERSION';
const NON_SEMVER_TAGS = {
Expand Down Expand Up @@ -389,19 +390,12 @@ export async function ensurePackage(
throwOnMissing?: boolean;
} = {}
): Promise<void> {
let version: string;

// Read package and version from root package.json file.
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();

// Try to resolve the actual version from resolved module.
try {
version = readModulePackageJson(pkg).packageJson.version;
} catch {
// ignore
}
let version = getPackageVersion(pkg);

// Otherwise try to read in from package.json. This is needed for E2E tests to pass.
if (!version) {
Expand All @@ -410,7 +404,15 @@ export async function ensurePackage(
version = packageJson[field]?.[pkg];
}

if (!satisfies(version, requiredVersion)) {
if (
// Special case: When running Nx unit tests, the version read from package.json is "0.0.1".
!(
pkg.startsWith('@nrwl/') &&
(version === '0.0.1' || requiredVersion === '0.0.1')
) &&
// Normal case
!satisfies(version, requiredVersion, { includePrerelease: true })
) {
const installCmd = `${
dev ? pmc.addDev : pmc.add
} ${pkg}@${requiredVersion}`;
Expand All @@ -426,3 +428,26 @@ export async function ensurePackage(
}
}
}

/**
* Use another process to resolve the package.json path of the package (if it exists).
* Cannot use `require.resolve` here since there is an unclearable internal cache used by Node that can lead to issues
* when resolving the package after installation.
*
* See: https://github.com/nodejs/node/issues/31803
*/
function getPackageVersion(pkg: string): undefined | string {
try {
return execSync(
`node -e "console.log(require('${pkg}/package.json').version)"`,
{
cwd: workspaceRoot,
stdio: ['pipe', 'pipe', 'ignore'],
}
)
.toString()
.trim();
} catch (e) {
return undefined;
}
}