Skip to content

Commit 80717ee

Browse files
Only use npm_package_json when we know it's correct
Fixes #960
1 parent fe2a9c1 commit 80717ee

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/cli-options.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@ import {QuietCiLogger, QuietLogger} from './logging/quiet-logger.js';
1717
import {DefaultLogger} from './logging/default-logger.js';
1818

1919
export const packageDir = await (async (): Promise<string | undefined> => {
20-
// Recent versions of npm set this environment variable that tells us the
21-
// package.
22-
const packageJsonPath = process.env.npm_package_json;
23-
if (packageJsonPath) {
24-
return pathlib.dirname(packageJsonPath);
20+
// Recent versions of npm and yarn set the `npm_package_json` environment
21+
// variable.
22+
//
23+
// `yarn` will only find `wireit` with the `-T` flag, which also sets
24+
// `npm_package_json` to the package.json at the project root, even if we're
25+
// in another package.
26+
//
27+
// Therefore, trust `npm_package_json` when in npm, but introspect the
28+
// filesystem otherwise.
29+
30+
const agent = getNpmUserAgent();
31+
32+
if (agent === 'npm') {
33+
const packageJsonPath = process.env.npm_package_json;
34+
if (packageJsonPath) {
35+
return pathlib.dirname(packageJsonPath);
36+
}
2537
}
26-
// Older versions of npm, as well as yarn and pnpm, don't set this variable,
27-
// so we have to find the nearest package.json by walking up the filesystem.
38+
39+
// Find the nearest package.json by walking up the filesystem.
2840
let maybePackageDir = process.cwd();
2941
while (true) {
3042
try {

0 commit comments

Comments
 (0)