fix: prevent browserslist config resolution error when installed via pnpm#2076
fix: prevent browserslist config resolution error when installed via pnpm#2076DeekRoumy wants to merge 1 commit intoasyncapi:masterfrom
Conversation
…pnpm (asyncapi#1781) When asyncapi CLI is installed globally via pnpm, the global bin/ directory contains a shell-script shim also named 'browserslist'. The browserslist package walks up the directory tree looking for config files and mistakenly tries to parse that shim as a browser query, producing: Unknown browser query 'basedir=$(dirname "$(echo "$0" | sed -e 's'")' Fix: set the BROWSERSLIST_ROOT_PATH environment variable to the output directory before invoking the generator. This restricts browserslist's config-file search scope to a known-safe directory, preventing the upward traversal that would reach pnpm's global bin directory. The variable is always restored in a finally block — including on error — so there are no side effects on subsequent calls or pre-existing env values. BROWSERSLIST_ROOT_PATH is preferred over BROWSERSLIST=defaults because it only limits where browserslist searches for config files, without changing the default browser targets themselves. Fixes asyncapi#1781
|
There was a problem hiding this comment.
Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
|



Summary
Fixes #1781 — Generator fails with `Unknown browser query 'basedir=$(dirname ...`` when CLI is installed globally via pnpm.
Root Cause
When installed via pnpm, the global
bin/directory contains a shell-script shim also namedbrowserslist. Thebrowserslistpackage walks up the directory tree looking for config files and mistakenly tries to parse this shim as a browser query, causing:Fix
Set the
BROWSERSLIST_ROOT_PATHenvironment variable to the output directory before invoking the generator. This restricts browserslist's config-file search scope to a known-safe path, preventing the upward traversal that would reach pnpm's globalbin/directory.The variable is always restored in a
finallyblock — including on error — so there are no side effects on subsequent calls or pre-existing environment values.Why
BROWSERSLIST_ROOT_PATHinstead ofBROWSERSLIST=defaults?BROWSERSLIST_ROOT_PATHonly limits where browserslist searches for config files — it does not change the default browser targets themselves.BROWSERSLIST=defaultsoverrides the actual query used for transpilation, which could silently change the output of templates that have their ownbrowserslistconfig.BROWSERSLIST_ROOT_PATHis the minimal, surgical fix.Changes
src/domains/services/generator.service.ts— set/restoreBROWSERSLIST_ROOT_PATHaround thegenerateFromStringcall.test/unit/services/generator.service.test.ts— new unit tests covering all env-var management scenarios (set, restore on success, restore on failure, no-override of pre-existing value, no leak across calls).Test Results
All 52 existing unit tests pass. 6 new tests added, all passing:
Test Plan
pnpm add -g @asyncapi/cli)asyncapi generate fromTemplate <spec.yaml> @asyncapi/html-template -o docsnpm run unit:test)