Skip to content

Commit 1f7273c

Browse files
committed
fix #2462: convert \ to / in windows paths
1 parent e6fa739 commit 1f7273c

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Fix Yarn PnP resolution failures due to backslashes in paths on Windows ([#2462](https://github.com/evanw/esbuild/issues/2462))
6+
7+
Previously dependencies of a Yarn PnP virtual dependency failed to resolve on Windows. This was because Windows uses `\` instead of `/` as a path separator, and the path manipulation algorithms used for Yarn PnP expected `/`. This release converts `\` into `/` in Windows paths, which fixes this issue.
8+
39
## 0.15.2
410

511
* Fix Yarn PnP issue with packages containing `index.js` ([#2455](https://github.com/evanw/esbuild/issues/2455), [#2461](https://github.com/evanw/esbuild/issues/2461))

internal/resolver/yarnpnp.go

+4
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ func (r resolverQuery) findLocator(manifest *pnpData, moduleUrl string) (pnpIden
277277
relativeUrl, ok := r.fs.Rel(manifest.absDirPath, moduleUrl)
278278
if !ok {
279279
return pnpIdentAndReference{}, false
280+
} else {
281+
// Relative URLs on Windows will use \ instead of /, which will break
282+
// everything we do below. Use normal slashes to keep things working.
283+
relativeUrl = strings.ReplaceAll(relativeUrl, "\\", "/")
280284
}
281285

282286
// The relative path must not start with ./; trim it if needed

scripts/js-api-tests.js

+78
Original file line numberDiff line numberDiff line change
@@ -3171,6 +3171,84 @@ require("/assets/file.png");
31713171
// scripts/.js-api-tests/yarnPnP_indexJs/entry.js
31723172
foo_default();
31733173
})();
3174+
`)
3175+
},
3176+
3177+
async yarnPnP_depOfVirtual({ esbuild, testDir }) {
3178+
const entry = path.join(testDir, 'entry.js')
3179+
const pkg = path.join(testDir, '.yarn', 'cache', 'pkg', 'index.js')
3180+
const dep = path.join(testDir, '.yarn', 'cache', 'dep', 'index.js')
3181+
const manifest = path.join(testDir, '.pnp.data.json')
3182+
3183+
await writeFileAsync(entry, `import 'pkg'`)
3184+
3185+
await mkdirAsync(path.dirname(pkg), { recursive: true })
3186+
await writeFileAsync(pkg, `import 'dep'`)
3187+
3188+
await mkdirAsync(path.dirname(dep), { recursive: true })
3189+
await writeFileAsync(dep, `success()`)
3190+
3191+
await writeFileAsync(manifest, `{
3192+
"packageRegistryData": [
3193+
[null, [
3194+
[null, {
3195+
"packageLocation": "./",
3196+
"packageDependencies": [
3197+
["pkg", "virtual:some-path"]
3198+
],
3199+
"linkType": "SOFT"
3200+
}]
3201+
]],
3202+
["demo", [
3203+
["workspace:.", {
3204+
"packageLocation": "./",
3205+
"packageDependencies": [
3206+
["demo", "workspace:."],
3207+
["pkg", "virtual:some-path"]
3208+
],
3209+
"linkType": "SOFT"
3210+
}]
3211+
]],
3212+
["pkg", [
3213+
["npm:1.0.0", {
3214+
"packageLocation": "./.yarn/cache/pkg/",
3215+
"packageDependencies": [
3216+
["pkg", "npm:1.0.0"]
3217+
],
3218+
"linkType": "SOFT"
3219+
}],
3220+
["virtual:some-path", {
3221+
"packageLocation": "./.yarn/__virtual__/pkg-virtual/0/cache/pkg/",
3222+
"packageDependencies": [
3223+
["pkg", "virtual:some-path"],
3224+
["dep", "npm:1.0.0"]
3225+
],
3226+
"linkType": "HARD"
3227+
}]
3228+
]],
3229+
["dep", [
3230+
["npm:1.0.0", {
3231+
"packageLocation": "./.yarn/cache/dep/",
3232+
"packageDependencies": [
3233+
["dep", "npm:1.0.0"]
3234+
],
3235+
"linkType": "HARD"
3236+
}]
3237+
]]
3238+
]
3239+
}`)
3240+
3241+
const value = await esbuild.build({
3242+
entryPoints: [entry],
3243+
bundle: true,
3244+
write: false,
3245+
})
3246+
3247+
assert.strictEqual(value.outputFiles.length, 1)
3248+
assert.strictEqual(value.outputFiles[0].text, `(() => {
3249+
// scripts/.js-api-tests/yarnPnP_depOfVirtual/.yarn/cache/dep/index.js
3250+
success();
3251+
})();
31743252
`)
31753253
},
31763254
}

0 commit comments

Comments
 (0)