Skip to content

Commit 94f09ea

Browse files
committed
fix #3790: warn about incorrect onResolve plugin
1 parent ab9b002 commit 94f09ea

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
3333
The ECMAScript 2024 specification was just approved, so it has been added to esbuild as a possible compilation target. You can read more about the features that it adds here: [https://2ality.com/2024/06/ecmascript-2024.html](https://2ality.com/2024/06/ecmascript-2024.html). The only addition that's relevant for esbuild is the regular expression `/v` flag. With `--target=es2024`, regular expressions that use the `/v` flag will now be passed through untransformed instead of being transformed into a call to `new RegExp`.
3434
35+
* Warn about `onResolve` plugins not setting a path ([#3790](https://github.com/evanw/esbuild/issues/3790))
36+
37+
Plugins that return values from `onResolve` without resolving the path (i.e. without setting either `path` or `external: true`) will now cause a warning. This is because esbuild only uses return values from `onResolve` if it successfully resolves the path, and it's not good for invalid input to be silently ignored.
38+
3539
## 0.21.5
3640
3741
* Fix `Symbol.metadata` on classes without a class decorator ([#3781](https://github.com/evanw/esbuild/issues/3781))

pkg/api/api_impl.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,33 @@ func (impl *pluginImpl) onResolve(options OnResolveOptions, callback func(OnReso
19271927

19281928
// Convert log messages
19291929
result.Msgs = convertErrorsAndWarningsToInternal(response.Errors, response.Warnings)
1930+
1931+
// Warn if the plugin returned things without resolving the path
1932+
if response.Path == "" && !response.External {
1933+
var what string
1934+
if response.Namespace != "" {
1935+
what = "namespace"
1936+
} else if response.Suffix != "" {
1937+
what = "suffix"
1938+
} else if response.PluginData != nil {
1939+
what = "pluginData"
1940+
} else if response.WatchFiles != nil {
1941+
what = "watchFiles"
1942+
} else if response.WatchDirs != nil {
1943+
what = "watchDirs"
1944+
}
1945+
if what != "" {
1946+
path := "path"
1947+
if logger.API == logger.GoAPI {
1948+
what = strings.Title(what)
1949+
path = strings.Title(path)
1950+
}
1951+
result.Msgs = append(result.Msgs, logger.Msg{
1952+
Kind: logger.Warning,
1953+
Data: logger.MsgData{Text: fmt.Sprintf("Returning %q doesn't do anything when %q is empty", what, path)},
1954+
})
1955+
}
1956+
}
19301957
return
19311958
},
19321959
})

scripts/plugin-tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,25 @@ let pluginTests = {
21482148
assert.deepStrictEqual({ ...esbuildFromBuild }, { ...esbuild })
21492149
},
21502150

2151+
async onResolveSuffixWithoutPath({ esbuild, testDir }) {
2152+
const input = path.join(testDir, 'in.js')
2153+
await writeFileAsync(input, `works()`)
2154+
const result = await esbuild.build({
2155+
entryPoints: [input],
2156+
logLevel: 'silent',
2157+
write: false,
2158+
plugins: [{
2159+
name: 'the-plugin',
2160+
setup(build) {
2161+
build.onResolve({ filter: /.*/ }, () => ({ suffix: '?just suffix without path' }))
2162+
},
2163+
}],
2164+
})
2165+
assert.strictEqual(result.warnings.length, 1)
2166+
assert.strictEqual(result.warnings[0].text, `Returning "suffix" doesn't do anything when "path" is empty`)
2167+
assert.strictEqual(result.warnings[0].pluginName, 'the-plugin')
2168+
},
2169+
21512170
async onResolveInvalidPathSuffix({ esbuild }) {
21522171
try {
21532172
await esbuild.build({

0 commit comments

Comments
 (0)