From a84723816a81908214a5eb69fbf55fec0326b35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Tue, 14 Nov 2023 20:03:33 +0100 Subject: [PATCH] Fixes the projectRoot detection --- src/cli/index.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/cli/index.js b/src/cli/index.js index 02ca91b980..f9a8889354 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -36,6 +36,29 @@ process.stdout.prependListener('error', err => { throw err; }); +function findPackageManager(base: string): ?string { + let prev = null; + let dir = base; + + do { + const p = path.join(dir, constants.NODE_PACKAGE_JSON); + + let data; + try { + data = JSON.parse(fs.readFileSync(p)); + } catch (err) {} + + if (data && typeof data.packageManager === `string`) { + return data.packageManager; + } + + prev = dir; + dir = path.dirname(dir); + } while (dir !== prev); + + return null; +} + function findProjectRoot(base: string): string { let prev = null; let dir = base; @@ -266,16 +289,13 @@ export async function main({ const config = new Config(reporter); - const projectRoot = findProjectRoot(commander.cwd); - const cwd = command.shouldRunInCurrentCwd ? commander.cwd : projectRoot; - - if (!process.env.COREPACK_ROOT) { - const rootManifest = await config.readRootManifest(projectRoot); - if (typeof rootManifest.packageManager === `string`) { - if (!rootManifest.packageManager.match(/^yarn@[01]\./)) { + if (!process.env.COREPACK_ROOT && !process.env.SKIP_YARN_COREPACK_CHECK) { + const packageManager = findPackageManager(commander.cwd); + if (packageManager !== null) { + if (!packageManager.match(/^yarn@[01]\./)) { reporter.error( `This project's package.json defines ${chalk.gray('"packageManager": "yarn@')}${chalk.yellow( - `${rootManifest.packageManager.replace(/^yarn@/, ``).replace(/\+.*/, ``)}`, + `${packageManager.replace(/^yarn@/, ``).replace(/\+.*/, ``)}`, )}${chalk.gray(`"`)}. However the current global version of Yarn is ${chalk.yellow(version)}.`, ); @@ -501,6 +521,8 @@ export async function main({ }); }; + const cwd = command.shouldRunInCurrentCwd ? commander.cwd : findProjectRoot(commander.cwd); + const folderOptionKeys = ['linkFolder', 'globalFolder', 'preferredCacheFolder', 'cacheFolder', 'modulesFolder']; // Resolve all folder options relative to cwd @@ -588,6 +610,8 @@ export async function main({ if (err instanceof MessageError) { reporter.error(err.message); + } else { + reporter.error(err.stack); } if (command.getDocsInfo) {