Skip to content

Commit

Permalink
fix #3634: crash if resolving with bad source dir
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 8, 2024
1 parent 2af5ccf commit a08f30d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Fix a crash when resolving a path from a directory that doesn't exist ([#3634](https://github.com/evanw/esbuild/issues/3634))

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.

## 0.20.0

**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.
Expand Down
12 changes: 6 additions & 6 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,6 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor

r.mutex.Lock()
defer r.mutex.Unlock()
sourceDirInfo := r.dirInfoCached(sourceDir)

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

sourceDirInfo := r.dirInfoCached(sourceDir)
if sourceDirInfo == nil {
// Bail if the directory is missing for some reason
return nil, debugMeta
}

result := r.resolveWithoutSymlinks(sourceDir, sourceDirInfo, importPath)
if result == nil {
// If resolution failed, try again with the URL query and/or hash removed
Expand Down Expand Up @@ -1006,11 +1011,6 @@ func (r resolverQuery) resolveWithoutSymlinks(sourceDir string, sourceDirInfo *d
}

if checkPackage {
if sourceDirInfo == nil {
// Bail if the directory is missing for some reason
return nil
}

// Support remapping one package path to another via the "browser" field
if remapped, ok := r.checkBrowserMap(sourceDirInfo, importPath, packagePathKind); ok {
if remapped == nil {
Expand Down
19 changes: 19 additions & 0 deletions scripts/plugin-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,25 @@ var foo_default2 = "🍕";
console.log(foo_default, foo_default2);
`)
},

async internalCrashIssue3634({ esbuild }) {
await esbuild.build({
entryPoints: [],
bundle: true,
plugins: [{
name: 'abc',
setup(build) {
build.onStart(async () => {
const result = await build.resolve('/foo', {
kind: 'require-call',
resolveDir: 'bar',
})
assert.strictEqual(result.errors.length, 1)
})
}
}],
})
},
}

const makeRebuildUntilPlugin = () => {
Expand Down

0 comments on commit a08f30d

Please sign in to comment.