oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.14k stars 2.76k forks source link

Preemptively expanding `await import("node:test")` #8748

Open jcbhmr opened 9 months ago

jcbhmr commented 9 months ago

What version of Bun is running?

1.0.26+c75e768a6

What platform is your computer?

Linux 4.4.0-19041-Microsoft x86_64 x86_64

What steps can reproduce the bug?

I want to conditionally import something. Like this:

const { test } = process.isBun ? await import("bun:test") : await import("node:test")

...however this fails. I think it's due to early resolution of node:test which should NOT be resolved until runtime.

As evidence to support that it's something happening before runtime: this works:

const { test } = process.isBun ? await import("bun:test") : await import(String("node:test"))

You can test this stuff by putting that code 👆 into a file like x.js and running bun test ./x.js

What is the expected behavior?

It should resolve the await import("node:test") at runtime so that it's OK that it doesn't exist.

What do you see instead?

jcbhmr@PIG2016:~/Documents/node4web$ cat z.js
const { test } = process.isBun ? await import("bun:test") : await import("node:test")
jcbhmr@PIG2016:~/Documents/node4web$ bun test ./z.js 
bun test v1.0.26 (c75e768a)

z.js:
1 | const { test } = process.isBun ? await import("bun:test") : await import("node:test")
                                                                             ^
error: Could not resolve: "node:test". Maybe you need to "bun install"?
    at /home/jcbhmr/Documents/node4web/z.js:1:74

 0 pass
 1 fail
Ran 1 tests across 1 files. [32.00ms]
jcbhmr@PIG2016:~/Documents/node4web$ cat z.js
const { test } = process.isBun ? await import("bun:test") : await import(String("node:test"))
jcbhmr@PIG2016:~/Documents/node4web$ bun test ./z.js 
bun test v1.0.26 (c75e768a)

z.js:

 0 pass
 0 fail
Ran 0 tests across 1 files. [19.00ms]

Additional information

No response

paperdave commented 9 months ago

ah, this is only intended to be an error when bundling, but it seems to also happen at runtime.