oven-sh / bun

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

`ReferenceError: Cannot access uninitialized variable.` is lacking proper location info from JSC #6824

Open morpig opened 1 year ago

morpig commented 1 year ago

What version of Bun is running?

1.0.7+b0393fba6200d8573f3433fb0af258a0e33ac157

What platform is your computer?

Darwin 22.5.0 arm64 arm

What steps can reproduce the bug?

async function pushToLog(sampling, requestId, message, data, duration) {
    console.log('a')
    console.log(Math.random())
}

What is the expected behavior?

no errors

What do you see instead?

a
0.4203247358417366
20 |     return c.json({ message: 'ok' }, 200);
21 | });
22 | 
23 | async function pushToLog(sampling, requestId, message, data, duration) {
24 |     console.log('a')
25 |     console.log(Math.random())
                                ^
ReferenceError: Cannot access uninitialized variable.
      at /Users/morpig/Documents/code/nel-collector/index.js:25:29
      at pushToLog (/Users/morpig/Documents/code/nel-collector/index.js:23:25)
      at /Users/morpig/Documents/code/nel-collector/index.js:15:8
      at find (:1:20)
      at /Users/morpig/Documents/code/nel-collector/index.js:20:4

Additional information

No response

morpig commented 1 year ago

well i'm stupid. there are business logic following, one of which is

const fetch = await fetch(``).....

but bun seems to be blaming the above lines. why?

Electroid commented 1 year ago

Could you share as much code as you can so we can try to make the error message better?

morpig commented 1 year ago
async function a(sampling, random, math) {
    console.log(Math.random())

    const fetch = await fetch('https://google.com')
}

a()

error shown:

0.9973894943346983
1 | async function a(sampling, random, math) {
2 |     console.log(Math.random())
        ^
ReferenceError: Cannot access uninitialized variable.
scsmash3r commented 1 year ago
async function a(sampling, random, math) {
    console.log(Math.random())

    const fetch = await fetch('https://google.com')
}

a()

error shown:

0.9973894943346983
1 | async function a(sampling, random, math) {
2 |     console.log(Math.random())
        ^
ReferenceError: Cannot access uninitialized variable.

This is not a bun issue. The problem in your code is that you are trying to redeclare fetch function as a constant. Thus saying, you can not reassign fetch to a fetch. Just use another constant name, like:

const fetchSomePage = await fetch('https://google.com')
morpig commented 1 year ago

As I said above, its my fault for not checking the fetch function.

Why? because Bun is blaming the console.log instead. Line tracing also points at the consolelog line.

scsmash3r commented 1 year ago

As I said above, its my fault for not checking the fetch function.

Why? because Bun is blaming the console.log instead. Line tracing also points at the consolelog line.

Aha. I see your point now. Tried same code in nodejs, and it gives much cleaner error message:

(node:3148) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'fetch' before initialization

pointing our directly to the problematic part.

Jarred-Sumner commented 5 months ago

There were two issues here: 1) Bun is pointing to the wrong source line. ~This was fixed in #11581~. Turns out it's a JavaScriptCore bug. 2) JavaScriptCore's ReferenceError lacks the variable name, which was fixed for Bun in https://github.com/oven-sh/WebKit/pull/56

This should be noticably better in Bun 1.1.13!