Skip to content

fix: ES module compatibility for migrations URL resolution#3373

Merged
mondoreale merged 6 commits intomainfrom
fix-undefined-dirname-in-browser
Jan 28, 2026
Merged

fix: ES module compatibility for migrations URL resolution#3373
mondoreale merged 6 commits intomainfrom
fix-undefined-dirname-in-browser

Conversation

@mondoreale
Copy link
Copy Markdown
Contributor

@mondoreale mondoreale commented Jan 28, 2026

Summary

Fix undefined __dirname error in browser environments by using import.meta.url for migration URL resolution in PersistenceManager. Rollup handles the conversion to environment-specific resolution for non-ESM builds.

Changes

  • Use import.meta.url instead of __dirname in PersistenceManager.ts for resolving migration paths
  • Add custom Jest transformer that wraps ts-jest to replace import.meta.url with file://${__filename} before transformation, enabling tests to run in Jest's CommonJS environment
  • Configure Jest to use the custom transformer for TypeScript files

Note

Ensures migrations path resolution works in ESM and Jest environments.

  • Replace __dirname with import.meta.url for migrationsUrl in PersistenceManager.ts (with webpackIgnore)
  • Add test/test-utils/importMetaTransformer.mjs to rewrite import.meta.url to file://${__filename} for ts-jest
  • Update jest.config.ts to use the custom transformer for TypeScript files

Written by Cursor Bugbot for commit b5e61d2. This will update automatically on new commits. Configure here.

This gets converted into env-specific resolution outside of esm (rollup does it).
Create `importMetaTransformer.js` that wraps `ts-jest` and replaces
`import.meta.url` with `file://${__filename}` before transformation,
enabling ESM-style URL references to work in Jest's CommonJS environment.
@github-actions github-actions Bot added the sdk label Jan 28, 2026
@mondoreale mondoreale requested a review from Copilot January 28, 2026 11:43
@mondoreale mondoreale marked this pull request as ready for review January 28, 2026 11:43
@mondoreale mondoreale changed the title fix: Fix import.meta.url usage for browser compatibility in PersistenceManager fix: Fix undefined __dirname in browser by using import.meta.url Jan 28, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR modernizes the PersistenceManager to use ES module syntax (import.meta.url) instead of CommonJS-specific globals (__dirname). The change improves compatibility with ES module environments and standardizes the codebase on ES module patterns. A custom Jest transformer was added to handle the transformation of import.meta.url to a CommonJS-compatible equivalent during test execution.

Changes:

  • Replaced file://${__dirname}/ with import.meta.url in PersistenceManager for constructing the migrations URL
  • Added a custom Jest transformer that converts import.meta.url to a template string using __filename for test compatibility
  • Updated Jest configuration to use the custom transformer for TypeScript files

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
packages/sdk/src/PersistenceManager.ts Updated migrations URL construction to use import.meta.url instead of __dirname
packages/sdk/test/test-utils/importMetaTransformer.js Added custom Jest transformer to handle import.meta.url in test environment
packages/sdk/jest.config.ts Configured Jest to use the custom transformer for TypeScript files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/sdk/test/test-utils/importMetaTransformer.mjs
Comment thread packages/sdk/test/test-utils/importMetaTransformer.mjs
Comment thread packages/sdk/src/PersistenceManager.ts Outdated
ownerId: await this.identity.getUserId(),
namespaces: Object.values(NAMESPACES),
migrationsUrl: new URL('./encryption/migrations', `file://${__dirname}/`),
migrationsUrl: new URL('./encryption/migrations', import.meta.url),
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

The PR title mentions "browser compatibility" but PersistenceManager imports from '@/Persistence' which resolves to the Node.js-specific implementation. This code doesn't actually run in browsers - the browser version uses IndexedDB and doesn't use migrations. Consider updating the PR title/description to clarify that this change is about ES module compatibility or module format compatibility, not browser compatibility.

Copilot uses AI. Check for mistakes.
Comment thread packages/sdk/test/test-utils/importMetaTransformer.mjs
Comment thread packages/sdk/test/test-utils/importMetaTransformer.mjs
@mondoreale mondoreale changed the title fix: Fix undefined __dirname in browser by using import.meta.url fix: ES module compatibility for migrations URL resolution Jan 28, 2026
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread packages/sdk/test/test-utils/importMetaTransformer.mjs Outdated
@mondoreale mondoreale merged commit 7039eea into main Jan 28, 2026
24 checks passed
@mondoreale mondoreale deleted the fix-undefined-dirname-in-browser branch January 28, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants