diff --git a/src/cli-options.ts b/src/cli-options.ts index b12645f4b..97d8fb46f 100644 --- a/src/cli-options.ts +++ b/src/cli-options.ts @@ -276,9 +276,15 @@ function getArgvOptions( // // npm 8.11.0 // - Like npm 6, except there is no "npm_config_argv" environment variable. + // + // npm 12 (https://github.com/npm/cli/pull/8071) + // - "npm run build --watch" is rejected as an unrecognized npm argument + // instead of being silently turned into "npm_config_watch", so we also + // accept WIREIT_WATCH=1 as an escape hatch (see #1346). return { watch: - process.env['npm_config_watch'] !== undefined + process.env['npm_config_watch'] !== undefined || + process.env['WIREIT_WATCH'] !== undefined ? readWatchConfigFromEnv() : false, extraArgs: process.argv.slice(2), @@ -446,6 +452,11 @@ function parseRemainingArgs( `e.g. "npm run build -- --extra".`, ); } + // Also accept WIREIT_WATCH as an escape hatch (matches the npm branch behaviour, + // useful for yarn/pnpm/node --run wrappers that scrub --watch from argv). + if (!watch && process.env['WIREIT_WATCH'] !== undefined) { + watch = readWatchConfigFromEnv(); + } return { watch, extraArgs, diff --git a/src/test/cli-options.test.ts b/src/test/cli-options.test.ts index 48520ad4e..60278d96a 100644 --- a/src/test/cli-options.test.ts +++ b/src/test/cli-options.test.ts @@ -247,6 +247,30 @@ for (const {agent, runCmd, testCmd, startCmd, needsExtraDashes} of commands) { }), ); + // https://github.com/google/wireit/issues/1346 + // npm 12 no longer forwards `--watch` as `npm_config_watch`, so we accept + // WIREIT_WATCH=1 as an alternative trigger. Should work on every agent. + void test( + `${agent} WIREIT_WATCH=1 run`, + rigTest(async ({rig}) => { + await assertOptions( + rig, + `${runCmd} main`, + { + agent, + script: { + packageDir: rig.temp, + name: 'main', + }, + watch: {strategy: 'event'}, + }, + { + WIREIT_WATCH: '1', + }, + ); + }), + ); + // Doesn't work with yarn 1.x due to // https://github.com/yarnpkg/yarn/issues/8905. Anything before a "--" is not // included on argv, and the npm_config_argv variable does not let us