Skip to content

Commit a847238

Browse files
committed
Fixes the projectRoot detection
1 parent efdccc1 commit a847238

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/cli/index.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ process.stdout.prependListener('error', err => {
3636
throw err;
3737
});
3838

39+
function findPackageManager(base: string): ?string {
40+
let prev = null;
41+
let dir = base;
42+
43+
do {
44+
const p = path.join(dir, constants.NODE_PACKAGE_JSON);
45+
46+
let data;
47+
try {
48+
data = JSON.parse(fs.readFileSync(p));
49+
} catch (err) {}
50+
51+
if (data && typeof data.packageManager === `string`) {
52+
return data.packageManager;
53+
}
54+
55+
prev = dir;
56+
dir = path.dirname(dir);
57+
} while (dir !== prev);
58+
59+
return null;
60+
}
61+
3962
function findProjectRoot(base: string): string {
4063
let prev = null;
4164
let dir = base;
@@ -266,16 +289,13 @@ export async function main({
266289

267290
const config = new Config(reporter);
268291

269-
const projectRoot = findProjectRoot(commander.cwd);
270-
const cwd = command.shouldRunInCurrentCwd ? commander.cwd : projectRoot;
271-
272-
if (!process.env.COREPACK_ROOT) {
273-
const rootManifest = await config.readRootManifest(projectRoot);
274-
if (typeof rootManifest.packageManager === `string`) {
275-
if (!rootManifest.packageManager.match(/^yarn@[01]\./)) {
292+
if (!process.env.COREPACK_ROOT && !process.env.SKIP_YARN_COREPACK_CHECK) {
293+
const packageManager = findPackageManager(commander.cwd);
294+
if (packageManager !== null) {
295+
if (!packageManager.match(/^yarn@[01]\./)) {
276296
reporter.error(
277297
`This project's package.json defines ${chalk.gray('"packageManager": "yarn@')}${chalk.yellow(
278-
`${rootManifest.packageManager.replace(/^yarn@/, ``).replace(/\+.*/, ``)}`,
298+
`${packageManager.replace(/^yarn@/, ``).replace(/\+.*/, ``)}`,
279299
)}${chalk.gray(`"`)}. However the current global version of Yarn is ${chalk.yellow(version)}.`,
280300
);
281301

@@ -501,6 +521,8 @@ export async function main({
501521
});
502522
};
503523

524+
const cwd = command.shouldRunInCurrentCwd ? commander.cwd : findProjectRoot(commander.cwd);
525+
504526
const folderOptionKeys = ['linkFolder', 'globalFolder', 'preferredCacheFolder', 'cacheFolder', 'modulesFolder'];
505527

506528
// Resolve all folder options relative to cwd
@@ -588,6 +610,8 @@ export async function main({
588610

589611
if (err instanceof MessageError) {
590612
reporter.error(err.message);
613+
} else {
614+
reporter.error(err.stack);
591615
}
592616

593617
if (command.getDocsInfo) {

0 commit comments

Comments
 (0)