-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Broken resolved paths in Windows inside a monorepo #11357
Description
Bug Report
- I would like to work on a fix!
Current Behavior
I'm using rollup to bundle a package inside a yarn monorepo. As soon as @babel/plugin-transform-runtime is used, the built package breaks because of broken resolved paths to runtime helpers such as _typeof. This results in warnings like this:
(!) Unresolved dependencies
C:UsersjohanneszProjectsMyproject
untime/helpers/typeof (imported by ..\..\..\node_modules\regenerator-runtime\runtime.js, C:UsersjohanneszProjectsMyproject
untime/helpers/typeof?commonjs-external
As you can see, the Backslashes are missing from the paths, causing a broken build.
See rollup/plugins#290 and yarnpkg/yarn#8031
Input Code
Here is a repro: https://github.com/johannes-z/rollup-yarn-path-error
Expected behavior/code
The paths should be correctly resolved on Windows
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
- Filename:
babel.config.js
module.exports = {
presets: [
[
'@vue/cli-plugin-babel/preset',
{
corejs: 3,
useBuiltIns: 'usage',
},
],
],
}Environment
System:
OS: Windows 10 10.0.18363
Binaries:
Node: 11.13.0 - C:\Users\johannesz\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
Yarn: 1.22.0 - C:\Users\johannesz\scoop\apps\yarn\current\Yarn\bin\yarn.CMD
npm: 6.7.0 - C:\Users\johannesz\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD
Monorepos:
Yarn Workspaces: 1.22.0
npmPackages:
eslint: ^5.16.0 => 5.16.0
rollup-plugin-babel: ^4.4.0 => 4.4.0
- How you are using Babel: loader/rollup-plugin-babel
Possible Solution
I was able to fix this problem by fixing the backslashes:
https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-runtime/src/index.js#L237
This is the line where the error ultimately occurs (I don't know where it comes from though).
Changing this line to fixed it for me:
return this.addDefaultImport(
`${modulePath.replace(/\\/g, '\\\\')}/${helpersDir}/${name}`,
name,
blockHoist
);Of course, this is not a permanent fix; especially since it seems to work on UNIX systems. I think resolveAbsoluteRuntime should consider Windows as an environment and generate a correct path.
Changing it to the following would probably be a better solution to fix this issue:
`${modulePath.replace(/\\/g, '/')}/${helpersDir}/${name}`EDIT: I investigated a little further. In fact the resolveAbsoluteRuntime function causes inconsistent paths between OS: https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-runtime/src/index.js#L13-L15
path.dirname creates forward slashes for unix systems, but on windows it only creates single backslashes: C:\Users\Johannes\.... Backslashes returned from dirname should be replaced with forward slashes - I haven't found a node.js way of doing this automatically.