evanw / evanw/esbuild

`require-resolve` resolution hook is not called for esm

Open
#2,986 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Currently, for esm target, require-resolve resolution kind is not called.

to reproduce
```js
async resolveKindRequireResolve({ esbuild }) {
let resolveKind = ''
try {
await esbuild.build({
entryPoints: ['entry'],
bundle: true,
write: false,
format: "esm", // -- added this line.
logLevel: 'silent',
platform: 'node',
plugins: [{
name: 'plugin',
setup(build) {
build.onResolve({ filter: /.*/ }, args => {
if (args.importer === '') return { path: args.path, namespace: 'ns' }
else resolveKind = args.kind
})
build.onLoad({ filter: /.*/, namespace: 'ns' }, () => {
return { contents: `require.resolve('test')` }
})
},
}],
})
} catch (e) {
}
assert.strictEqual(resolveKind, 'require-resolve')
},
```
Problem is this flow:
https://github.com/evanw/esbuild/blob/5ad10c614eb074b1ca0c6968137124d5c186cf02/internal/js_parser/js_parser.go#L1647
`ShouldCallRuntimeRequire` is false for not cjs.
`id.Ref == p.requireRef` is false
https://github.com/evanw/esbuild/blob/5ad10c614eb074b1ca0c6968137124d5c186cf02/internal/js_parser/js_parser.go#L13794

Require kind is called for esm, so probably require-resolve should be also called.
Can provide a pr if you tell me what is best fix.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in internal/js_parser/js_parser.go at the linked ShouldCallRuntimeRequire and requireRef handling locations. Run the provided ESM build reproduction and inspect why the plugin's onResolve callback does not receive a require-resolve kind. Done means the callback receives require-resolve for the shown ESM case without regressing the existing require behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.