Skip to content
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

Can't debug code supplied to eval(). #171

Closed
jsdevel opened this issue Jul 18, 2013 · 6 comments
Closed

Can't debug code supplied to eval(). #171

jsdevel opened this issue Jul 18, 2013 · 6 comments

Comments

@jsdevel
Copy link

jsdevel commented Jul 18, 2013

I'm reading in a file for a test framework, and at the top of one of my tests I have the debugger; keyword. Node inspector stops on the eval(code) line, but stepping into the function fails to show the code being evaluated.

@bajtos
Copy link
Member

bajtos commented Jul 18, 2013

@jsdevel: Could you please attach a (minimal) test-case that I can run myself to reproduce the problem?

@jsdevel
Copy link
Author

jsdevel commented Jul 19, 2013

Absolutely!

I'm able to reproduce with the following:

In a directory, save the following to a file named test-eval.js:

var fs   = require("fs");
var code = fs.readFileSync("./codeToEval.js", "utf-8");

eval(code);

As a sibling in the same directory, save the following to a file named codeToEval.js:

debugger;

function iAmProblematic(a){
   debugger;
   return "say it isn't so "+a;
}

iAmProblematic("already");

Follow these steps:

  1. node --debug-brk test-eval.js in one terminal window
  2. node-inspector & in another terminal window
  3. View the specified url in chrome.
  4. Step through the code until you reach the eval statement.
  5. Now attempt to step into the eval(code) call.

The interesting part, is that the debugger; statements are reached in the evalled code, as the console pauses on each step when clicking run; however, I'm unable to view the source of the evalled code.

I did find something from the chrome dev tools documentation that seemed promising:
"Note: Notice the "//@ sourceURL=dynamicScript.js" line at the end of dynamicScript.js file. This technique gives a name to a script created with eval, and will be discussed in more detail in the Source Maps section. Breakpoints can be set in dynamic JavaScript only if it has a user supplied name."source

This doesn't seem to be too helpful however, if the evalled code is created dynamically which is often the case.

@jsdevel
Copy link
Author

jsdevel commented Jul 19, 2013

Wait, Freaking Ureeka!! Doing exactly that fixed the issue to some degree.

codeToEval.js now looks like this:

debugger;

function iAmProblematic(a){
   debugger;
   return "say it isn't so "+a;
}

iAmProblematic("already");

//@ sourceURL=dynamicScript.js

However, there are still issues. The evalled code does appear as a new script tab in the window, however, it's fairly sketchy and I can't step through it.

@jsdevel
Copy link
Author

jsdevel commented Jul 19, 2013

Made a bit of progress, but seeing the following error in the window where I started node-inspector as soon as I get to the evalled code:

Internal error: TypeError: Cannot read property 'line' of null

@jsdevel
Copy link
Author

jsdevel commented Jul 19, 2013

OK. Sorry for all the comments. I got it to work reasonably well!!

The trick appears to be to just wait for a while. I've attached a screenshot with a successful session where I was able to step through the evalled code appropriately by stepping into the eval statement once, and then waiting for about 10 seconds or so.
2013-07-18-231114_1280x900_scrot

@jsdevel jsdevel closed this as completed Jul 19, 2013
@bajtos
Copy link
Member

bajtos commented Jul 23, 2013

Hi @jsdevel, thank you for investigating the issue and describing a solution.

I like the idea of using source maps to let V8 know what file you are debugging, it's very elegant.

Still, you should be able to debug eval-ed code out of the box, even without source maps.

In the screenshot below, you can see how my node-inspector looks like after stepping into eval(code). Notice the yellow background - it's a sign that the code has no file associated with it and it lives in V8 VM only.

image

In both cases (with and without sourceMap), the debugger and inspector remained very responsive, I didn't have to wait at all.

What version of node are you running? Is there anything else in your setup/environment that could explain the difference?

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

No branches or pull requests

2 participants