evanw / evanw/esbuild

esbuild does not use typescript source for self-references or sub path imports

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

Description

### Describe the bug

Subpath and self-reference imports are resolved to their literal values rather than the TypeScript source file.

```
// package.json
{
"name": "subpath-exports",
"exports": {
"./sub": "./dist/sub.js"
},
"imports": {
"#sub": "./dist/sub.js"
}
}
```

```
// src/index.ts
import "./sub.js"
import "subpath-exports/sub"
import "#sub"
```

`esbuild` will resolve `#sub` and `subpath-exports/sub` to `./dist/sub.js`, not `./src/sub.ts`. This can cause module not found errors if dist has not been produced, or unexpected behavior if the dist/ is out of date and doesn't match the other source.

### Reproduction

```
echo 'import("#sub"); import("subpath-exports/sub")' | npx esbuild --bundle
✘ [ERROR] Could not resolve "#sub"

:1:7:
1 │ import("#sub"); import("subpath-exports/sub")
╵ ~~~~~~

The module "./dist/sub.js" was not found on the file system:

package.json:9:12:
9 │ "#sub": "./dist/sub.js"
╵ ~~~~~~~~~~~~~~~

You can mark the path "#sub" as external to exclude it from the bundle, which will remove this
error. You can also add ".catch()" here to handle this failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "subpath-exports/sub"

:1:23:
1 │ import("#sub"); import("subpath-exports/sub")
╵ ~~~~~~~~~~~~~~~~~~~~~

The module "./dist/sub.js" was not found on the file system:

package.json:6:13:
6 │ "./sub": "./dist/sub.js"
╵ ~~~~~~~~~~~~~~~

You can mark the path "subpath-exports/sub" as external to exclude it from the bundle, which will
remove this error. You can also add ".catch()" here to handle this failure at run-time instead of
bundle-time.

2 errors
```

https://github.com/everett1992/subpath-exports

Contributor guide

No contributing guide indexed for this repository

Research direction

The reproduction uses package.json and src/index.ts; first run the npx esbuild --bundle command to confirm how the self-reference and subpath imports resolve. Trace esbuild's package export and import resolution from that reproduction, then verify that #sub and subpath-exports/sub resolve to the TypeScript source rather than the dist paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript, typescript
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.