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

fix(commonjs)!: Correctly infer module name for any separator #924

Merged
merged 2 commits into from Jul 30, 2021
Merged

fix(commonjs)!: Correctly infer module name for any separator #924

merged 2 commits into from Jul 30, 2021

Conversation

pastelmind
Copy link
Contributor

@pastelmind pastelmind commented Jul 3, 2021

Rollup Plugin Name: commonjs

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers: #923

Description

When a module whose file name is index.js is resolved to a module ID containing a directory separator that is different from path.sep of the current platform, the commonjs plugin incorrectly infers the base name of the module. This causes the absolute path of the module to be embedded into the bundle, and prevents generating reproducible builds across different machines.

For example, with a module ID of C:/Users/Phil/Documents/GitHub/test-vite-app/node_modules/react/index.js on Windows, the plugin generates code like this:

var C__Users_Phil_Documents_GitHub_testViteApp_node_modules_react = { exports: {} };
var react_production_min = {};

This PR fixes the problem by using path.basename() to extract the final path component instead of splitting the string with path.sep.

With the fix, the plugin generates code like this:

var react = { exports: {} };
var react_production_min = {};

Closes #923

pastelmind added a commit to Loathing-Associates-Scripting-Society/philter that referenced this pull request Jul 3, 2021
Vite currently has a bug that causes it to generate different bundles on
Windows and Linux. This is caused by a fault in @rollup/plugin-commonjs
which is bundled into Vite.

We directly fix the fault in our `node_modules/` using patch-package.
Let's hope the Rollup devs accept my PR and the fix is integrated into
Vite.

More links:

- Bug reports:
    - vitejs/vite#2623
    - rollup/plugins#923
- Related pull requests:
    - rollup/plugins#924
pastelmind added a commit to Loathing-Associates-Scripting-Society/philter that referenced this pull request Jul 4, 2021
Vite currently has a bug that causes it to generate different bundles on
Windows and Linux. This is caused by a fault in @rollup/plugin-commonjs
which is bundled into Vite.

We directly fix the fault in our `node_modules/` using patch-package.
Let's hope the Rollup devs accept my PR and the fix is integrated into
Vite.

More links:

- Bug reports:
    - vitejs/vite#2623
    - rollup/plugins#923
- Related pull requests:
    - rollup/plugins#924
@pastelmind
Copy link
Contributor Author

I added a test. Unfortunately, it works only on Windows, where replacing backslashes with slashes is perfectly fine. On Linux, the backslash is treated as a regular character, which means that we'd get an error if we replaced slashes with backslashes.

@pastelmind
Copy link
Contributor Author

@danielgindi I apologize for pinging you, but this PR hasn't even got a chance to run workflows yet. Could you please take a look?

@pastelmind
Copy link
Contributor Author

@danielgindi Thanks for letting the CI workflow happen, but it failed and I'm not sure why. The log is at https://github.com/rollup/plugins/runs/3150391648, here's an excerpt:

Run pnpm run ci:test --filter ...[36a461e]
pnpm run ci:test --filter ...[36a461e]
shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
 ERROR  Filtering by changed packages failed. fatal: bad object 36a461e
Error: Process completed with exit code 1.

I rebased the commit onto the current master and force-pushed, but now the workflow needs approval again. I'm not familiar with the CI process--could you please help me troubleshoot this?

Add test for module IDs that use a path separator that is different from
`path.sep`.

Previously, when a module whose file name is `index.js` was resolved to
a module ID containing different path separators, the commonjs plugin
generated a variable name containing the module's absolute path.
BREAKING CHANGES:
Correctly infer the module name from the module ID, regardless of the
path separator used in the module ID and the value of `path.sep`.

This generates variable names like `react` instead of
`C__testViteApp_node_modules_react` when a module ID of
`C:/test-vite-app/node_modules/react/index.js` is given on Windows.
@shellscape
Copy link
Collaborator

@pastelmind looks like your tests all pass now. I've requested review from a few folks to get eyes on this for you.

@danielgindi
Copy link
Contributor

To me this looks much more resilient than the current code. Using core path functions handles this stuff for us.

@shellscape shellscape merged commit f408497 into rollup:master Jul 30, 2021
@shellscape
Copy link
Collaborator

thanks!

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.

commonjs: Module ID containing forward slash is improperly normalized on Windows
4 participants