Skip to content

Commit

Permalink
fix #2462: convert \ to / in windows paths
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 13, 2022
1 parent e6fa739 commit 1f7273c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Fix Yarn PnP resolution failures due to backslashes in paths on Windows ([#2462](https://github.com/evanw/esbuild/issues/2462))

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.

## 0.15.2

* 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))
Expand Down
4 changes: 4 additions & 0 deletions internal/resolver/yarnpnp.go
Expand Up @@ -277,6 +277,10 @@ func (r resolverQuery) findLocator(manifest *pnpData, moduleUrl string) (pnpIden
relativeUrl, ok := r.fs.Rel(manifest.absDirPath, moduleUrl)
if !ok {
return pnpIdentAndReference{}, false
} else {
// Relative URLs on Windows will use \ instead of /, which will break
// everything we do below. Use normal slashes to keep things working.
relativeUrl = strings.ReplaceAll(relativeUrl, "\\", "/")
}

// The relative path must not start with ./; trim it if needed
Expand Down
78 changes: 78 additions & 0 deletions scripts/js-api-tests.js
Expand Up @@ -3171,6 +3171,84 @@ require("/assets/file.png");
// scripts/.js-api-tests/yarnPnP_indexJs/entry.js
foo_default();
})();
`)
},

async yarnPnP_depOfVirtual({ esbuild, testDir }) {
const entry = path.join(testDir, 'entry.js')
const pkg = path.join(testDir, '.yarn', 'cache', 'pkg', 'index.js')
const dep = path.join(testDir, '.yarn', 'cache', 'dep', 'index.js')
const manifest = path.join(testDir, '.pnp.data.json')

await writeFileAsync(entry, `import 'pkg'`)

await mkdirAsync(path.dirname(pkg), { recursive: true })
await writeFileAsync(pkg, `import 'dep'`)

await mkdirAsync(path.dirname(dep), { recursive: true })
await writeFileAsync(dep, `success()`)

await writeFileAsync(manifest, `{
"packageRegistryData": [
[null, [
[null, {
"packageLocation": "./",
"packageDependencies": [
["pkg", "virtual:some-path"]
],
"linkType": "SOFT"
}]
]],
["demo", [
["workspace:.", {
"packageLocation": "./",
"packageDependencies": [
["demo", "workspace:."],
["pkg", "virtual:some-path"]
],
"linkType": "SOFT"
}]
]],
["pkg", [
["npm:1.0.0", {
"packageLocation": "./.yarn/cache/pkg/",
"packageDependencies": [
["pkg", "npm:1.0.0"]
],
"linkType": "SOFT"
}],
["virtual:some-path", {
"packageLocation": "./.yarn/__virtual__/pkg-virtual/0/cache/pkg/",
"packageDependencies": [
["pkg", "virtual:some-path"],
["dep", "npm:1.0.0"]
],
"linkType": "HARD"
}]
]],
["dep", [
["npm:1.0.0", {
"packageLocation": "./.yarn/cache/dep/",
"packageDependencies": [
["dep", "npm:1.0.0"]
],
"linkType": "HARD"
}]
]]
]
}`)

const value = await esbuild.build({
entryPoints: [entry],
bundle: true,
write: false,
})

assert.strictEqual(value.outputFiles.length, 1)
assert.strictEqual(value.outputFiles[0].text, `(() => {
// scripts/.js-api-tests/yarnPnP_depOfVirtual/.yarn/cache/dep/index.js
success();
})();
`)
},
}
Expand Down

0 comments on commit 1f7273c

Please sign in to comment.