Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use import.meta.resolve to support relative and bare specifiers #177

Merged
merged 1 commit into from
Dec 26, 2023

Conversation

davidchambers
Copy link
Owner

This pull request fixes our handling of import declarations.

Currently only absolute specifiers such as import util from 'node:util' are interpreted correctly:

  • import foo from './foo.js' attempts to import [[doctest]]/lib/foo.js; and
  • import bar from 'bar' attempts to load the main entry point of [[doctest]]/node_modules/bar.

This bug does not just affect import declarations within doctests: top-level import declarations are also affected.

The fix involves using import.meta.resolve to resolve the module specifier. We provide an argument for the non-standard parent parameter Node.js supports when the --experimental-import-meta-resolve flag is specified. This makes module resolution relative to the module in which the import declaration appears, as expected.

The parent parameter is available in Node.js v16.0.0, the oldest version we currently support, but needs a string argument. Node.js v16.2.0 added support for providing a URL object instead. As this is more convenient, and since we already have a breaking change on main (#176), I decided to increase our Node.js version requirement.

@davidchambers davidchambers merged commit b785745 into main Dec 26, 2023
6 checks passed
@davidchambers davidchambers deleted the import-meta-resolve branch December 26, 2023 15:23
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