Skip to content

[POC] Better debugging stack traces #3586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

sophiebits
Copy link
Collaborator

I had an idea: what if our stack traces looked more like this so you don't just have 30 frames inside React and then a top-level render call, which is the typical situation nowadays?

image

Turns out we can do this with a small hack. Each time we create an element, we store away a function that just calls its argument:

var foo = React.createElementDebug(function element_Foo(x) { x(); }, Foo, ...);

then when we go to mount the component, we call this function with the mount code as argument so that it appears on the stack.

This is a little crazy and probably slow, but could make development nicer. You can't inspect the props (e.g., a, b, c here) but if we had the function capture those vars I think you could.

I had an idea: what if our stack traces looked more like this so you don't just have 30 frames inside React and then a top-level render call, which is the typical situation nowadays?

![image](https://cloud.githubusercontent.com/assets/6820/6995020/aacf7f9e-dae2-11e4-9959-d4914cae5123.png)

Turns out we can do this with a small hack. Each time we create an element, we store away a function that just calls its argument:

```js
var foo = React.createElementDebug(function element_Foo(x) { x(); }, Foo, ...);
```

then when we go to mount the component, we call this function with the mount code as argument so that it appears on the stack.

This is a little crazy and probably slow, but could make development nicer. You can't inspect the props (e.g., `a`, `b`, `c` here) but if we had the function capture those vars I think you could.
@sophiebits sophiebits changed the title [POC] Better debugging stack traces [RFC] Better debugging stack traces Apr 4, 2015
@sophiebits sophiebits changed the title [RFC] Better debugging stack traces [POC] Better debugging stack traces Apr 4, 2015
@sebmarkbage
Copy link
Collaborator

This is actually a really neat insight! __DEV__ mode is already super slow anyway (which is also a problem but a separate issue). I don't think this is necessarily making it much worse.

Another thing we were considering was using Promises so that the async debugging tools in Chrome would work.

Lets keep iterating on something like this.

@sebmarkbage
Copy link
Collaborator

@spicyj Here's a kind of related thought:

If you have a properly inferred name on the render function. E.g. ES6 classes would have a name like "MyComponent.render".

What if we in createElement did something like: new Error().stack and stored away the stack where it was created. That way you get not just where it was created, but also the indirect stack that got you there, including file/line which can be used by the devtools.

You could even use this to infer the "owner" name for use by debug outputs. That could allow us to avoid tracking owner even in debug mode.

@sophiebits
Copy link
Collaborator Author

Yes, we could. I assumed that would be much slower than this and not much more useful.

@sebmarkbage
Copy link
Collaborator

The benefits are that A) it works with unchanged syntax and any caller, not just JSX. B) it shows indirections. The downside is that you don't get the breakpoint debugger which is really neat.

@sophiebits
Copy link
Collaborator Author

cc @jaredly

@gaearon
Copy link
Collaborator

gaearon commented Mar 26, 2016

This is interesting but I’m not sure how feasible this would be perf-wise, or whether it would actually improve the situation rather than introduce more confusion.

Deep and seemingly unrelated stack traces are a real problem so let’s continue tracking it in #5460. I will close this PR, but we can revisit it later.

@gaearon gaearon closed this Mar 26, 2016
@jaredly
Copy link
Contributor

jaredly commented Mar 26, 2016

Yeah I remember we had lots of discussions about this last year, but didn't come up with anything sufficiently performant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants