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): check for lerna before parsing lockfiles to prevent errors #18884

Merged
merged 1 commit into from
Aug 29, 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
1 change: 1 addition & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type TargetDependencies = Record<
export interface NrwlJsPluginConfig {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
analyzeLockfile?: boolean;
}

interface NxInstallationConfiguration {
Expand Down
32 changes: 27 additions & 5 deletions packages/nx/src/plugins/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { NrwlJsPluginConfig, NxJsonConfiguration } from '../../config/nx-json';
import { dirname, join } from 'path';
import { projectGraphCacheDirectory } from '../../utils/cache-directory';
import { readFileSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { workspaceRoot } from '../../utils/workspace-root';
import { ensureDirSync } from 'fs-extra';
import { performance } from 'perf_hooks';
Expand Down Expand Up @@ -49,8 +49,11 @@ export const processProjectGraph: ProjectGraphProcessor = async (
const pluginConfig = jsPluginConfig(readNxJson());

if (pluginConfig.analyzePackageJson) {
// during the create-nx-workspace lock file might not exists yet
if (lockFileExists()) {
if (
// during the create-nx-workspace lock file might not exists yet
lockFileExists() &&
pluginConfig.analyzeLockfile
) {
const lockHash = lockFileHash();
let parsedLockFile: ProjectGraph;
if (lockFileNeedsReprocessing(lockHash)) {
Expand Down Expand Up @@ -114,16 +117,27 @@ function jsPluginConfig(
const nxJsonConfig: NrwlJsPluginConfig =
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) {
return {
analyzePackageJson: true,
analyzeSourceFiles: true,
analyzeLockfile,
...nxJsonConfig,
};
}

if (!fileExists(join(workspaceRoot, 'package.json'))) {
return {
analyzeLockfile: false,
analyzePackageJson: false,
analyzeSourceFiles: false,
};
Expand Down Expand Up @@ -153,8 +167,16 @@ function jsPluginConfig(
packageJsonDeps['@nrwl/angular'] ||
packageJsonDeps['@nrwl/web']
) {
return { analyzePackageJson: true, analyzeSourceFiles: true };
return {
analyzePackageJson: true,
analyzeLockfile,
analyzeSourceFiles: true,
};
} else {
return { analyzePackageJson: true, analyzeSourceFiles: false };
return {
analyzePackageJson: true,
analyzeLockfile,
analyzeSourceFiles: false,
};
}
}