Skip to content

Commit

Permalink
Only use npm_package_json when we know it's correct
Browse files Browse the repository at this point in the history
Fixes #960
  • Loading branch information
appsforartists committed Nov 3, 2023
1 parent fe2a9c1 commit cef14f5
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@ import {QuietCiLogger, QuietLogger} from './logging/quiet-logger.js';
import {DefaultLogger} from './logging/default-logger.js';

export const packageDir = await (async (): Promise<string | undefined> => {
// Recent versions of npm set this environment variable that tells us the
// package.
const packageJsonPath = process.env.npm_package_json;
if (packageJsonPath) {
return pathlib.dirname(packageJsonPath);
// Recent versions of npm and yarn set the `npm_package_json` environment
// variable.
//
// yarn sets it to the package.json at the project root, even if we're in
// another package.
//
// Therefore, trust `npm_package_json` when in npm, but introspect the
// filesystem otherwise.

const agent = getNpmUserAgent();

if (agent === 'npm') {
const packageJsonPath = process.env.npm_package_json;
if (packageJsonPath) {
return pathlib.dirname(packageJsonPath);
}
}
// Older versions of npm, as well as yarn and pnpm, don't set this variable,
// so we have to find the nearest package.json by walking up the filesystem.

// Find the nearest package.json by walking up the filesystem.
let maybePackageDir = process.cwd();
while (true) {
try {
Expand Down

0 comments on commit cef14f5

Please sign in to comment.