Skip to content

Commit a08f30d

Browse files
committed
fix #3634: crash if resolving with bad source dir
1 parent 2af5ccf commit a08f30d

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Fix a crash when resolving a path from a directory that doesn't exist ([#3634](https://github.com/evanw/esbuild/issues/3634))
6+
7+
This release fixes a regression where esbuild could crash when resolving an absolute path if the source directory for the path resolution operation doesn't exist. While this situation doesn't normally come up, it could come up when running esbuild concurrently with another operation that mutates the file system as esbuild is doing a build (such as using `git` to switch branches). The underlying problem was a regression that was introduced in version 0.18.0.
8+
39
## 0.20.0
410

511
**This release deliberately contains backwards-incompatible changes.** To avoid automatically picking up releases like this, you should either be pinning the exact version of `esbuild` in your `package.json` file (recommended) or be using a version range syntax that only accepts patch upgrades such as `^0.19.0` or `~0.19.0`. See npm's documentation about [semver](https://docs.npmjs.com/cli/v6/using-npm/semver/) for more information.

internal/resolver/resolver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor
504504

505505
r.mutex.Lock()
506506
defer r.mutex.Unlock()
507-
sourceDirInfo := r.dirInfoCached(sourceDir)
508507

509508
// Check for the Yarn PnP manifest if it hasn't already been checked for
510509
if !r.pnpManifestWasChecked {
@@ -533,6 +532,12 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor
533532
}
534533
}
535534

535+
sourceDirInfo := r.dirInfoCached(sourceDir)
536+
if sourceDirInfo == nil {
537+
// Bail if the directory is missing for some reason
538+
return nil, debugMeta
539+
}
540+
536541
result := r.resolveWithoutSymlinks(sourceDir, sourceDirInfo, importPath)
537542
if result == nil {
538543
// If resolution failed, try again with the URL query and/or hash removed
@@ -1006,11 +1011,6 @@ func (r resolverQuery) resolveWithoutSymlinks(sourceDir string, sourceDirInfo *d
10061011
}
10071012

10081013
if checkPackage {
1009-
if sourceDirInfo == nil {
1010-
// Bail if the directory is missing for some reason
1011-
return nil
1012-
}
1013-
10141014
// Support remapping one package path to another via the "browser" field
10151015
if remapped, ok := r.checkBrowserMap(sourceDirInfo, importPath, packagePathKind); ok {
10161016
if remapped == nil {

scripts/plugin-tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,25 @@ var foo_default2 = "🍕";
25222522
console.log(foo_default, foo_default2);
25232523
`)
25242524
},
2525+
2526+
async internalCrashIssue3634({ esbuild }) {
2527+
await esbuild.build({
2528+
entryPoints: [],
2529+
bundle: true,
2530+
plugins: [{
2531+
name: 'abc',
2532+
setup(build) {
2533+
build.onStart(async () => {
2534+
const result = await build.resolve('/foo', {
2535+
kind: 'require-call',
2536+
resolveDir: 'bar',
2537+
})
2538+
assert.strictEqual(result.errors.length, 1)
2539+
})
2540+
}
2541+
}],
2542+
})
2543+
},
25252544
}
25262545

25272546
const makeRebuildUntilPlugin = () => {

0 commit comments

Comments
 (0)