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

Extensionless require() for subpath in package with subpath folder mappings in its exports map fails to resolve #865

Closed
tbroyer opened this issue Apr 21, 2021 · 1 comment

Comments

@tbroyer
Copy link

tbroyer commented Apr 21, 2021

  • Rollup Plugin Name: node-resolve, commonjs
  • Rollup Plugin Version: node-resolve=11.2.1, commonjs=18.0.0
  • Rollup Version: 2.45.2
  • Operating System (or Browser): Arch Linux
  • Node Version: v15.14.0
  • Link to reproduction (⚠️ read below): https://replit.com/@tbroyer/rollup-plugin-repro-issue-865

Expected Behavior

Extensionless subpath require()s, from a package with a subpath folder mappings for that subpath, resolves the file by appending the extension.

Actual Behavior

Error: Could not load /home/runner/rollup-plugin-repro/node_modules/extract-files/public/extractFiles (imported by input.js): ENOENT: no such file or directory, open '/home/runner/rollup-plugin-repro/node_modules/extract-files/public/extractFiles'

Additional Information

Copied from #684 (comment):

Following https://nodejs.org/api/modules.html#modules_all_together, require("extract-files/public/extractFiles") will end up calling

PACKAGE_EXPORTS_RESOLVE("file:///…/node_modules/extract-files", "./public/extractFiles", { …, "./public/": "./public/", … }, ["node", "require"])

Following https://nodejs.org/api/esm.html#esm_resolver_algorithm_specification, this will call

PACKAGE_IMPORTS_EXPORTS_RESOLVE("./public/extractFiles", { …, "./public/": "./public/", … }, "file:///…/node_modules/extract-files", false, ["node", "require"])

which will then call (from 3.2.3)

PACKAGE_TARGET_RESOLVE("file:///…/node_modules/extract-files", "./public/", "extractFiles", false, false, ["node, "require"])

This will return (from 1.8) file:///…/node_modules/extract-files/public/extractFiles. PACKAGE_EXPORTS_RESOLVE will thus return { resolved: "file:///…/node_modules/extract-files/public/extractFiles", exact: false }.
Back to https://nodejs.org/api/modules.html#modules_all_together, we'll end up in

RESOLVE_ESM_MATCH({ resolved: "file:///…/node_modules/extract-files/public/extractFiles", exact: false })

and because of exact: false, Node would correctly add the .js extension and find file:///…/node_modules/extract-files/public/extractFiles.js.

Webpack 5 had the same issue, and changed behavior last October: webpack/enhanced-resolve#256

So, this is a combination of using node-resolve, commonjs, extensionless requires, and a package using the deprecated subpath folder mappings. Had the extract-files package used subpath patterns, it would have ended up in RESOLVE_ESM_MATCH with exact: true, making the file extension mandatory.

@stale
Copy link

stale bot commented Jun 21, 2021

Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it.

@stale stale bot closed this as completed Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant