diff --git a/.changeset/fix-pnpm-browserslist-env-var-leak.md b/.changeset/fix-pnpm-browserslist-env-var-leak.md new file mode 100644 index 000000000..f99d1c2a2 --- /dev/null +++ b/.changeset/fix-pnpm-browserslist-env-var-leak.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/cli": patch +--- + +fix: clear BROWSERSLIST env var during generator execution to prevent pnpm shell context interference diff --git a/src/utils/environment.ts b/src/utils/environment.ts new file mode 100644 index 000000000..26b342211 --- /dev/null +++ b/src/utils/environment.ts @@ -0,0 +1,32 @@ +/** + * Executes a given function with the BROWSERSLIST environment variable temporarily cleared. + * This prevents the AsyncAPI generator and its dependencies (like the HTML template, + * which may use PostCSS/Autoprefixer and Browserslist) from encountering + * malformed browser queries due to an incorrectly set BROWSERSLIST environment variable. + * + * The original BROWSERSLIST value (if any) is restored after the function completes. + * + * @param fn The function to execute within the controlled environment. + * @returns The result of the executed function. + */ +export function withCleanBrowserslistEnv(fn: () => T): T { + const originalBrowserslistEnv = process.env.BROWSERSLIST; + try { + // Clear the BROWSERSLIST environment variable to prevent it from interfering + // with internal template dependencies that might use browserslist, + // especially when an invalid value (like a shell command string) is present. + delete process.env.BROWSERSLIST; + + // Optionally, if a specific default browserslist is always desired, one could set it here: + // process.env.BROWSERSLIST = 'last 2 versions, not dead, > 0.2%'; + + return fn(); + } finally { + // Restore the original BROWSERSLIST environment variable or ensure it remains deleted + if (originalBrowserslistEnv !== undefined) { + process.env.BROWSERSLIST = originalBrowserslistEnv; + } else { + delete process.env.BROWSERSLIST; + } + } +}