Skip to content

Commit

Permalink
fix #2913: crash in ProbeResolvePackageAsRelative
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 13, 2023
1 parent 5e0b1cd commit 29d5a9b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Fix a crash with path resolution error generation ([#2913](https://github.com/evanw/esbuild/issues/2913))

In certain situations, a module containing an invalid import path could previously cause esbuild to crash when it attempts to generate a more helpful error message. This crash has been fixed.

## 0.17.8

* Fix a minification bug with non-ASCII identifiers ([#2910](https://github.com/evanw/esbuild/issues/2910))
Expand Down
4 changes: 2 additions & 2 deletions internal/bundler/bundler.go
Expand Up @@ -570,7 +570,7 @@ func ResolveFailureErrorTextSuggestionNotes(
hint += " You can also add \".catch()\" here to handle this failure at run-time instead of bundle-time."
}
if pluginName == "" && !fs.IsAbs(path) {
if query := res.ProbeResolvePackageAsRelative(absResolveDir, path, kind); query != nil {
if query, _ := res.ProbeResolvePackageAsRelative(absResolveDir, path, kind); query != nil {
hint = fmt.Sprintf("Use the relative path %q to reference the file %q. "+
"Without the leading \"./\", the path %q is being interpreted as a package path instead.",
"./"+path, resolver.PrettyPath(fs, query.PathPair.Primary), path)
Expand Down Expand Up @@ -1654,7 +1654,7 @@ func (s *scanner) addEntryPoints(entryPoints []EntryPoint) []graph.EntryPoint {
"This syntax is typically handled by your shell, and isn't handled by esbuild itself. " +
"You must expand glob syntax first before passing your paths to esbuild.",
})
} else if query := s.res.ProbeResolvePackageAsRelative(entryPointAbsResolveDir, entryPoint.InputPath, ast.ImportEntryPoint); query != nil {
} else if query, _ := s.res.ProbeResolvePackageAsRelative(entryPointAbsResolveDir, entryPoint.InputPath, ast.ImportEntryPoint); query != nil {
notes = append(notes, logger.MsgData{
Text: fmt.Sprintf("Use the relative path %q to reference the file %q. "+
"Without the leading \"./\", the path %q is being interpreted as a package path instead.",
Expand Down
21 changes: 21 additions & 0 deletions internal/bundler_tests/bundler_default_test.go
Expand Up @@ -8089,3 +8089,24 @@ func TestCommentPreservationPreserveJSX(t *testing.T) {
},
})
}

func TestErrorMessageCrashStdinIssue2913(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/project/node_modules/fflate/package.json": `{ "main": "main.js" }`,
"/project/node_modules/fflate/main.js": ``,
},
options: config.Options{
Stdin: &config.StdinInfo{
Contents: `import "node_modules/fflate"`,
AbsResolveDir: "/project",
},
Mode: config.ModeBundle,
Platform: config.PlatformNeutral,
AbsOutputDir: "/out",
},
expectedScanLog: `<stdin>: ERROR: Could not resolve "node_modules/fflate"
NOTE: You can mark the path "node_modules/fflate" as external to exclude it from the bundle, which will remove this error.
`,
})
}
28 changes: 7 additions & 21 deletions internal/resolver/resolver.go
Expand Up @@ -574,28 +574,14 @@ func (r resolverQuery) isExternal(matchers config.ExternalMatchers, path string,
return false
}

func (res *Resolver) ResolveAbs(absPath string) *ResolveResult {
r := resolverQuery{Resolver: res}
if r.log.Level <= logger.LevelDebug {
r.debugLogs = &debugLogs{what: fmt.Sprintf("Getting metadata for absolute path %s", absPath)}
}

r.mutex.Lock()
defer r.mutex.Unlock()

// Just decorate the absolute path with information from parent directories
result := &ResolveResult{PathPair: PathPair{Primary: logger.Path{Text: absPath, Namespace: "file"}}}
r.finalizeResolve(result)
r.flushDebugLogs(flushDueToSuccess)
return result
}

// This tries to run "Resolve" on a package path as a relative path. If
// successful, the user just forgot a leading "./" in front of the path.
func (res *Resolver) ProbeResolvePackageAsRelative(sourceDir string, importPath string, kind ast.ImportKind) *ResolveResult {
func (res *Resolver) ProbeResolvePackageAsRelative(sourceDir string, importPath string, kind ast.ImportKind) (*ResolveResult, DebugMeta) {
var debugMeta DebugMeta
r := resolverQuery{
Resolver: res,
kind: kind,
Resolver: res,
debugMeta: &debugMeta,
kind: kind,
}
absPath := r.fs.Join(sourceDir, importPath)

Expand All @@ -607,10 +593,10 @@ func (res *Resolver) ProbeResolvePackageAsRelative(sourceDir string, importPath
result := &ResolveResult{PathPair: pair, DifferentCase: diffCase}
r.finalizeResolve(result)
r.flushDebugLogs(flushDueToSuccess)
return result
return result, debugMeta
}

return nil
return nil, debugMeta
}

type debugLogs struct {
Expand Down

0 comments on commit 29d5a9b

Please sign in to comment.