Skip to content

Commit

Permalink
fix(core): ensure yarn runs install with correct version (#17997)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Jul 7, 2023
1 parent 21007d8 commit d0c3772
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/create-nx-workspace/src/create-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function createSandbox(packageManager: PackageManager) {
`Installing dependencies with ${packageManager}`
).start();

const { install } = getPackageManagerCommand(packageManager);
const { install, preInstall } = getPackageManagerCommand(packageManager);

const tmpDir = dirSync().name;
try {
Expand All @@ -39,6 +39,10 @@ export async function createSandbox(packageManager: PackageManager) {
);
generatePackageManagerFiles(tmpDir, packageManager);

if (preInstall) {
await execAndWait(preInstall, tmpDir);
}

await execAndWait(install, tmpDir);

installSpinner.succeed();
Expand Down
4 changes: 4 additions & 0 deletions packages/create-nx-workspace/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function getPackageManagerCommand(
): {
install: string;
exec: string;
preInstall?: string;
} {
const [pmMajor, pmMinor] =
getPackageManagerVersion(packageManager).split('.');
Expand All @@ -45,6 +46,9 @@ export function getPackageManagerCommand(
const useBerry = +pmMajor >= 2;
const installCommand = 'yarn install --silent';
return {
preInstall: useBerry
? 'yarn set version stable'
: 'yarn set version classic',
install: useBerry
? installCommand
: `${installCommand} --ignore-scripts`,
Expand Down
10 changes: 9 additions & 1 deletion packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,20 @@ export function ensurePackage<T extends any = any>(

console.log(`Fetching ${pkg}...`);
const packageManager = detectPackageManager();
const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
const preInstallCommand = getPackageManagerCommand(packageManager).preInstall;
if (preInstallCommand) {
// ensure package.json and repo in tmp folder is set to a proper package manager state
execSync(preInstallCommand, {
cwd: tempDir,
stdio: isVerbose ? 'inherit' : 'ignore',
});
}
let addCommand = getPackageManagerCommand(packageManager).addDev;
if (packageManager === 'pnpm') {
addCommand = 'pnpm add -D'; // we need to ensure that we are not using workspace command
}

const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
execSync(`${addCommand} ${pkg}@${requiredVersion}`, {
cwd: tempDir,
stdio: isVerbose ? 'inherit' : 'ignore',
Expand Down
4 changes: 4 additions & 0 deletions packages/nx/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const execAsync = promisify(exec);
export type PackageManager = 'yarn' | 'pnpm' | 'npm';

export interface PackageManagerCommands {
preInstall?: string;
install: string;
ciInstall: string;
add: string;
Expand Down Expand Up @@ -64,6 +65,9 @@ export function getPackageManagerCommand(
const useBerry = gte(yarnVersion, '2.0.0');

return {
preInstall: useBerry
? 'yarn set version stable'
: 'yarn set version classic',
install: 'yarn',
ciInstall: useBerry
? 'yarn install --immutable'
Expand Down

1 comment on commit d0c3772

@vercel
Copy link

@vercel vercel bot commented on d0c3772 Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.