From 3a313a4ca662846b6ed87cde7312e9cd7b3348eb Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Sat, 26 Mar 2022 03:29:52 -0500 Subject: [PATCH] Fix create-next-app failing without yarn installed (#35608) This fixes our package manager detection which was changed slightly in https://github.com/vercel/next.js/pull/34947 for `pnpm` support to ensure the default case still attempts detecting the available package manager if no preference is specified in the command args. This also adds a test case to ensure the install succeeds when we fail to detect a valid `yarn` binary and no preference is set via the `npm_config_user_agent` env variable. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` Fixes: https://github.com/vercel/next.js/issues/35607 Fixes: https://github.com/vercel/next.js/issues/35599 --- packages/create-next-app/index.ts | 2 +- .../integration/create-next-app/index.test.ts | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/create-next-app/index.ts b/packages/create-next-app/index.ts index 8d7f25427610..3c16c40a7dd6 100644 --- a/packages/create-next-app/index.ts +++ b/packages/create-next-app/index.ts @@ -129,7 +129,7 @@ async function run(): Promise { ? 'npm' : !!program.usePnpm ? 'pnpm' - : 'yarn' + : getPkgManager() const example = typeof program.example === 'string' && program.example.trim() try { diff --git a/test/integration/create-next-app/index.test.ts b/test/integration/create-next-app/index.test.ts index 1f68f56875c0..b0707196dedf 100644 --- a/test/integration/create-next-app/index.test.ts +++ b/test/integration/create-next-app/index.test.ts @@ -305,7 +305,30 @@ describe('create next app', () => { it('should create a project in the current directory', async () => { await usingTempDir(async (cwd) => { - const res = await run(['.'], { cwd }) + const env = { ...process.env } + const tmpBin = path.join(__dirname, 'bin') + const tmpYarn = path.join(tmpBin, 'yarn') + + if (process.platform !== 'win32') { + // ensure install succeeds with invalid yarn binary + // which simulates no yarn binary being available as + // an alternative to removing the binary and reinstalling + await fs.remove(tmpBin) + await fs.mkdir(tmpBin) + await fs.writeFile(tmpYarn, '#!/bin/sh\nexit 1') + await fs.chmod(tmpYarn, '755') + env.PATH = `${tmpBin}:${env.PATH}` + delete env.npm_config_user_agent + } + + const res = await run(['.'], { + cwd, + env, + extendEnv: false, + stdio: 'inherit', + }) + await fs.remove(tmpBin) + expect(res.exitCode).toBe(0) const files = [