Skip to content

Commit

Permalink
fix(core): run ensurePackage with node_modules for yarn berry (#18144)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2c1d4ba)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Jul 20, 2023
1 parent 95d99e4 commit d4f6c0b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { installPackagesTask } from '../tasks/install-packages-task';
import { requireNx } from '../../nx';
import { dirSync } from 'tmp';
import { join } from 'path';
import type { PackageManager } from 'nx/src/utils/package-manager';
import { writeFileSync } from 'fs';

const {
readJson,
Expand All @@ -18,6 +20,7 @@ const {
workspaceRoot,
detectPackageManager,
createTempNpmDirectory,
getPackageManagerVersion,
} = requireNx();

const UNIDENTIFIED_VERSION = 'UNIDENTIFIED_VERSION';
Expand Down Expand Up @@ -465,6 +468,7 @@ export function ensurePackage<T extends any = any>(
console.log(`Fetching ${pkg}...`);
const packageManager = detectPackageManager();
const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
generatePackageManagerFiles(tempDir, packageManager);
const preInstallCommand = getPackageManagerCommand(packageManager).preInstall;
if (preInstallCommand) {
// ensure package.json and repo in tmp folder is set to a proper package manager state
Expand Down Expand Up @@ -508,6 +512,27 @@ export function ensurePackage<T extends any = any>(
}
}

/**
* Generates necessary files needed for the package manager to work
* and for the node_modules to be accessible.
*/
function generatePackageManagerFiles(
root: string,
packageManager: PackageManager = detectPackageManager()
) {
const [pmMajor] = getPackageManagerVersion(packageManager).split('.');
switch (packageManager) {
case 'yarn':
if (+pmMajor >= 2) {
writeFileSync(
join(root, '.yarnrc.yml'),
'nodeLinker: node-modules\nenableScripts: false'
);
}
break;
}
}

function addToNodePath(dir: string) {
// NODE_PATH is a delimited list of paths.
// The delimiter is different for windows.
Expand Down

0 comments on commit d4f6c0b

Please sign in to comment.