Skip to content

fix: prevent browserslist config resolution error when installed via pnpm#2076

Closed
DeekRoumy wants to merge 1 commit intoasyncapi:masterfrom
DeekRoumy:fix/pnpm-1781
Closed

fix: prevent browserslist config resolution error when installed via pnpm#2076
DeekRoumy wants to merge 1 commit intoasyncapi:masterfrom
DeekRoumy:fix/pnpm-1781

Conversation

@DeekRoumy
Copy link

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 named browserslist. The browserslist package walks up the directory tree looking for config files and mistakenly tries to parse this shim as a browser query, causing:

Generator Error: Unknown browser query 'basedir=$(dirname "$(echo "$0" | sed -e 's'". Maybe you are using old Browserslist or made typo in query.

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 path, 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 environment values.

Why BROWSERSLIST_ROOT_PATH instead of BROWSERSLIST=defaults?

  • BROWSERSLIST_ROOT_PATH only limits where browserslist searches for config files — it does not change the default browser targets themselves.
  • BROWSERSLIST=defaults overrides the actual query used for transpilation, which could silently change the output of templates that have their own browserslist config.
  • BROWSERSLIST_ROOT_PATH is the minimal, surgical fix.

Changes

  • src/domains/services/generator.service.ts — set/restore BROWSERSLIST_ROOT_PATH around the generateFromString call.
  • test/unit/services/generator.service.test.tsnew 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:

GeneratorService – pnpm browserslist guard (issue #1781)
  ✔ sets BROWSERSLIST_ROOT_PATH to the output directory while generation runs
  ✔ removes BROWSERSLIST_ROOT_PATH after generation succeeds
  ✔ removes BROWSERSLIST_ROOT_PATH even when generation throws
  ✔ preserves a pre-existing BROWSERSLIST_ROOT_PATH and restores it afterward
  ✔ restores a pre-existing BROWSERSLIST_ROOT_PATH even when generation throws
  ✔ does not leak BROWSERSLIST_ROOT_PATH across multiple sequential calls

52 passing (2s)

Test Plan

  • Install asyncapi CLI globally via pnpm (pnpm add -g @asyncapi/cli)
  • Run asyncapi generate fromTemplate <spec.yaml> @asyncapi/html-template -o docs
  • Verify documentation generates without the browserslist error
  • Verify all existing unit tests pass (npm run unit:test)

…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
@changeset-bot
Copy link

changeset-bot bot commented Mar 23, 2026

⚠️ No Changeset found

Latest commit: b120f45

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sonarqubecloud
Copy link

@github-project-automation github-project-automation bot moved this from To Triage to Done in CLI - Kanban Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] Generator error when using pnpm to create documentation

2 participants