fix(core): check for lerna before parsing lockfiles to prevent errors (#18884)

This commit is contained in:
Jack Hsu 2023-08-29 12:00:05 -04:00 committed by GitHub
parent 829076d0e7
commit e188775ffb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View File

@ -35,6 +35,7 @@ export type TargetDependencies = Record<
export interface NrwlJsPluginConfig { export interface NrwlJsPluginConfig {
analyzeSourceFiles?: boolean; analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean; analyzePackageJson?: boolean;
analyzeLockfile?: boolean;
} }
interface NxInstallationConfiguration { interface NxInstallationConfiguration {

View File

@ -18,7 +18,7 @@ import {
import { NrwlJsPluginConfig, NxJsonConfiguration } from '../../config/nx-json'; import { NrwlJsPluginConfig, NxJsonConfiguration } from '../../config/nx-json';
import { dirname, join } from 'path'; import { dirname, join } from 'path';
import { projectGraphCacheDirectory } from '../../utils/cache-directory'; import { projectGraphCacheDirectory } from '../../utils/cache-directory';
import { readFileSync, writeFileSync } from 'fs'; import { existsSync, readFileSync, writeFileSync } from 'fs';
import { workspaceRoot } from '../../utils/workspace-root'; import { workspaceRoot } from '../../utils/workspace-root';
import { ensureDirSync } from 'fs-extra'; import { ensureDirSync } from 'fs-extra';
import { performance } from 'perf_hooks'; import { performance } from 'perf_hooks';
@ -49,8 +49,11 @@ export const processProjectGraph: ProjectGraphProcessor = async (
const pluginConfig = jsPluginConfig(readNxJson()); const pluginConfig = jsPluginConfig(readNxJson());
if (pluginConfig.analyzePackageJson) { if (pluginConfig.analyzePackageJson) {
if (
// during the create-nx-workspace lock file might not exists yet // during the create-nx-workspace lock file might not exists yet
if (lockFileExists()) { lockFileExists() &&
pluginConfig.analyzeLockfile
) {
const lockHash = lockFileHash(); const lockHash = lockFileHash();
let parsedLockFile: ProjectGraph; let parsedLockFile: ProjectGraph;
if (lockFileNeedsReprocessing(lockHash)) { if (lockFileNeedsReprocessing(lockHash)) {
@ -114,16 +117,27 @@ function jsPluginConfig(
const nxJsonConfig: NrwlJsPluginConfig = const nxJsonConfig: NrwlJsPluginConfig =
nxJson?.pluginsConfig?.['@nx/js'] ?? nxJson?.pluginsConfig?.['@nrwl/js']; nxJson?.pluginsConfig?.['@nx/js'] ?? nxJson?.pluginsConfig?.['@nrwl/js'];
// using lerna _before_ installing deps is causing an issue when parsing lockfile.
// See: https://github.com/lerna/lerna/issues/3807
// Note that previous attempt to fix this caused issues with Nx itself, thus we're checking
// for Lerna explicitly.
// See: https://github.com/nrwl/nx/pull/18784/commits/5416138e1ddc1945d5b289672dfb468e8c544e14
const analyzeLockfile =
!existsSync(join(workspaceRoot, 'lerna.json')) ||
existsSync(join(workspaceRoot, 'nx.json'));
if (nxJsonConfig) { if (nxJsonConfig) {
return { return {
analyzePackageJson: true, analyzePackageJson: true,
analyzeSourceFiles: true, analyzeSourceFiles: true,
analyzeLockfile,
...nxJsonConfig, ...nxJsonConfig,
}; };
} }
if (!fileExists(join(workspaceRoot, 'package.json'))) { if (!fileExists(join(workspaceRoot, 'package.json'))) {
return { return {
analyzeLockfile: false,
analyzePackageJson: false, analyzePackageJson: false,
analyzeSourceFiles: false, analyzeSourceFiles: false,
}; };
@ -153,8 +167,16 @@ function jsPluginConfig(
packageJsonDeps['@nrwl/angular'] || packageJsonDeps['@nrwl/angular'] ||
packageJsonDeps['@nrwl/web'] packageJsonDeps['@nrwl/web']
) { ) {
return { analyzePackageJson: true, analyzeSourceFiles: true }; return {
analyzePackageJson: true,
analyzeLockfile,
analyzeSourceFiles: true,
};
} else { } else {
return { analyzePackageJson: true, analyzeSourceFiles: false }; return {
analyzePackageJson: true,
analyzeLockfile,
analyzeSourceFiles: false,
};
} }
} }