Skip to content

fix(parser): give ESM entries an explicit module scope - #201

Closed
pyramation wants to merge 1 commit into
mainfrom
fix/esm-module-scope
Closed

pyramation wants to merge 1 commit into
mainfrom
fix/esm-module-scope

Conversation

@pyramation

@pyramation pyramation commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

@pgsql/parser's ESM entries currently rely on Node's syntax detection; with detection disabled, import { Parser } from '@pgsql/parser' fails with SyntaxError: Named export 'Parser' not found. This gives the ESM entries an explicit module scope without .mjs files.

  • parser/scripts/prepare.js writes wasm/esm/package.json ({"type":"module"}) and puts the ESM entries inside it; the WASM loader and bundled types stay outside as CommonJS.
  • parser/package.json points every existing ESM export at wasm/esm/… and lists types conditions first. CJS paths and versions unchanged.
  • parser/templates/index.js.template imports the scoped entries.
  • parser/test/esm.test.mjs adds a root + v18 regression test run with syntax detection disabled (subprocess errors surfaced, not discarded).

Fresh redo of #182 on current main. Left out vs #182: .mjs outputs (folder-scoped type: module instead) and the duplicated per-version (PG13–17) regression tests — the shared export map covers those versions and PG 18 is the focus.

Tested locally: PG18 WASM built from source, versions/18 tests 150/150, parser suite 34/34 on Node 20; packed tarball imports root and all six subpaths in ESM and CJS with detection disabled and type-checks under NodeNext.


libpg-query and parser opened this PR for Dan
Implemented in session Redo explicit ESM module scope in libpg-query-node
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • Fixed ESM package entry points so the default and version-specific imports resolve correctly while CommonJS require paths remain available.
  • Tests
    • Added checks that the root and v18 ESM entry points can parse a query.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@luxor-code-review

luxor-code-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Failed load permanently poisons _loadPromise and ready · index.js.template:28–45 · unchanged line

// parser/templates/index.js.template
28	    if (!this._loadPromise) {
29	      this._loadPromise = this._doLoad();
30	    }
31	    
32	    return this._loadPromise;
33	  }
34	  
35	  async _doLoad() {
36	    const module = await import(`./v${this.version}.js`);
37	    this.parser = module;
38	    
39	    if (this.parser.loadModule) {

In the ESM Parser, loadParser caches this._loadPromise = this._doLoad() and never clears it on rejection, and ready is resolved only at the end of _doLoad (parser/templates/index.js.template:28-45). If await import(\./v${this.version}.js`)orloadModule()fails once, every laterparse()call re-awaits the same cached rejected promise and fails with the stale first error, and any caller awaitingparser.ready` hangs forever.

A transient failure that would succeed on retry becomes permanent, which matters precisely for the newly relocated wasm/esm/ shims whose absence this load path would surface. The identical pattern exists in parser/templates/index.cjs.template at line 29.


This PR introduces dual ESM/CJS entry points for the parser package: the prepare script now writes an esm/ directory of module shims alongside the existing CJS ones, and parser/package.json gains corresponding exports subpaths. A new parser/test/esm.test.mjs smoke-tests the ESM entry through --input-type=module -e.

Files Change
parser/scripts/prepare.js Generates wasm/esm/vN.js / index.js shims with rewritten relative imports and writes an esm/package.json marked "type": "module".
parser/templates/index.js.template ESM entry template that lazy-loads the per-version shim via dynamic import() and caches the load promise.
parser/templates/index.cjs.template Adjusted CJS template to match the new load/ready wiring.
parser/package.json Adds ESM exports conditions pointing at the generated esm/ shims.
parser/test/esm.test.mjs New smoke test exercising the ESM entry point.

Reviewed commit: 129194d

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The package now exposes ESM entry points from wasm/esm. The build script generates flat version modules, and Parser._doLoad() loads those modules. A subprocess test checks parsing through the root and v18 entry points.

Changes: ESM package exports and loading

Layer / File(s) Summary
ESM exports and generated layout
parser/package.json, parser/scripts/prepare.js
Package exports point to ESM files under wasm/esm while retaining CommonJS paths for require. The preparation script creates the ESM package marker and generates the ESM entry point and flat version files.
Version loading and ESM verification
parser/templates/index.js.template, parser/test/esm.test.mjs
Parser._doLoad() loads version modules from flat paths. A subprocess test checks that the root and v18 entry points parse SELECT 1.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 12919

This change gives the ESM entry points an explicit module scope so imports work when Node syntax detection is off. CommonJS paths are unchanged. No merge-blocking risk was identified.

Architecture risk: Low · up to 12919

The change affects 1 system.

Changed systems: parser

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — parser (service) was modified; 4 changed files map to changed impact.

Before / after behavior

  • observed — Modified behavior in parser/package.json: The default module and type paths now target wasm/esm; the default export uses the ESM entry for import and retains its CommonJS entry for require.
  • observed — Modified behavior in parser/package.json: The v13–v18 export entries now target wasm/esm files for import, while retaining their CommonJS files for require; their declaration paths remain version-specific.
  • observed — Modified behavior in parser/scripts/prepare.js: The script now creates wasm/esm recursively and writes a package.json declaring that directory an ES module package; it no longer conditionally creates the WASM root directory.
  • observed — Modified behavior in parser/scripts/prepare.js: Generated ESM namespace exports now target flat ./vN.js files rather than ./vN/index.js.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(parser): give ESM entries an explicit module scope" matches the main change. The PR adds wasm/esm/package.json with {"type":"module"}, places the ESM entries in that scope, and points t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(parser): give ESM entries an explicit module scope" matches the main change. The PR adds wasm/esm/package.json with {"type":"module"}, places the ESM entries in that scope, and points the export map and template to the scoped entries. The title names the parser package and the ESM scope change, so it is specific and conveys the primary change.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4d675e0 and 129194d.

Files selected for processing: 4

  • parser/package.json
  • parser/scripts/prepare.js
  • parser/templates/index.js.template
  • parser/test/esm.test.mjs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant