bun
bun copied to clipboard
TypeScript modules are resolved last, not first
Version
0.1.7
Platform
Darwin Nicks-MacBook-Pro.local 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64
What steps will reproduce the bug?
Clone: https://github.com/Nick-Mazuk/bun-resolution-reproduction
bun run index.ts
This was created by running bun init -y
, updating index.ts, and adding hello.txt and hello.txt.ts.
How often does it reproduce? Is there a required condition?
If there's a file a
, and another a.ts
, bun will try to first import a
. This differs from TypeScript's resolution where a.ts
would actually be imported.
What is the expected behavior?
TypeScript first tries to resolve the module to a TypeScript file, then to other types of files if none is found.
As such, the expected output is "Hello via Bun!" logged to the console.
What do you see instead?
1 | import { hello } from "./hello.txt";
2 |
3 | console.log(hello);
^
ReferenceError: Can't find variable: hello
at /Users/nick/Documents/GitHub/bun-resolution-reproduction/index.ts:3:12
Additional information
No response
Matching the full path first, and then matching the ".ts" file, which I think is good and prevents a lot of filename collisions. You can also try importing "./hello.txt.ts".
import { hello } from "./hello.txt.ts";
is working but I'm getting this warning An import path cannot end with a '.ts' extension. Consider importing './hello.txt.js' instead.
In TypeScript, it's actually a best practice to not include the extension. The TypeScript compiler itself doesn't actually modify the imported filename extensions when compiling TypeScript code. So when you include the extension, it results in invalid JavaScript (since JavaScript cannot import .ts
files).
Matching the full path first, and then matching the ".ts" file, which I think is good and prevents a lot of filename collisions.
Regardless, I'd argue that Bun should copy the TypeScript spec as much as possible. Otherwise we'd gradually develop two distinct languages called "TypeScript".
Is there any progress on this or do there exist any workarounds?
The reproduction provided breaks in both Node and Bun, so Bun is breaking here because it follows same convention as Node.
If the file extension is added in the import, then Bun, Node and tsx (node + esbuild) can run the example. Node of course only if all files are .js. It is possible with bun to import hello.txt.js
even if the file is hello.txt.ts
- this would i.e. break with tsx
This appear to be working as intended, with Bun being able to execute this in more cases than to Node and tsx.