facebook / facebook/hermes

[Debugger] Incorrect scope variable information for variable initialized in for-loop

Open
#1,355 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
11.3k
Forks
859
Avg merge
1h 30m
Merged PRs (30d)
3

Description

## Bug Description

I'm running into some issues with a CDP Debugger (vscode-js-debug) connected to Hermes. Namely, when a variable is initialized in a for-loop, like the following:
```
for (let i=0; i <10; i++) {
{... some code}
}
```

The variables in the for-loop initializer don't get updated in the debugger. I've narrowed down the root cause to Hermes sending two copies of the variable when `Runtime.getProperties` is called on the local scope and two copies of the variable are returned. One for the initial value, and one for the current value:
```
{"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":2}},
{"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":0}},
```
Digging through the code I think it may be how Hermes is transforming a for block, creating two copies of the variable initialized in the initializer `const/let x = init;` and `const/let x = temp_x;`:
https://github.com/facebook/hermes/blob/55ccdf198078be78ac8c562b0087f561591aa950/lib/AST/BlockScopingTransformations.cpp#L227-L245

Moving the variable initialization outside of the for-loop fixes the issue.

Do you think you guys can help with this issue? Thanks!

Hermes git revision (if applicable): 7d9baea7221a1f69828764938fb5de0737419f6b
React Native version: n/a
OS: Android
Platform: arm64-v8a

## Steps To Reproduce

1. Single step through the following code:
```
for (let i=0; i <10; i++) {
{... some code}
}
```
2. Each time Runtime.getProperties is called on the local scope object, it will return two copies of the variable `i`:
```
{"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":2}},
{"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":0}},
```

## The Expected Behavior

Expected behaviour is for there to just be one copy of the for loop's initializer variable to be sent in the debugger info.

Contributor guide

Open the contributing guide

Research direction

Start with the for-loop transformation in lib/AST/BlockScopingTransformations.cpp at lines 227-245, then reproduce the example while calling Runtime.getProperties on the local scope. Compare the returned entries for i with the expected single current value; done means the debugger receives one copy of the initializer variable.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, javascript
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.