bun icon indicating copy to clipboard operation
bun copied to clipboard

TypeScript modules are resolved last, not first

Open Nick-Mazuk opened this issue 2 years ago • 5 comments

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

Nick-Mazuk avatar Aug 08 '22 01:08 Nick-Mazuk

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".

mchao123 avatar Aug 08 '22 07:08 mchao123

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.

kitharvey avatar Aug 08 '22 10:08 kitharvey

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).

Nick-Mazuk avatar Aug 08 '22 16:08 Nick-Mazuk

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".

Nick-Mazuk avatar Aug 08 '22 16:08 Nick-Mazuk

Is there any progress on this or do there exist any workarounds?

kbrimble avatar Dec 13 '22 22:12 kbrimble

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.

birkskyum avatar Aug 26 '23 23:08 birkskyum