X Tutup
The Wayback Machine - https://web.archive.org/web/20201025235218/https://github.com/nodejs/node/issues/26780
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

VM: options.columnOffset has no effect on column number in stack trace output #26780

Open
Eric24 opened this issue Mar 19, 2019 · 8 comments
Open

Comments

@Eric24
Copy link

@Eric24 Eric24 commented Mar 19, 2019

  • Version: v10.3.0 and v10.15.2
  • Platform: Linux ip-172-30-3-105 4.14.42-52.37.amzn1.x86_64
  • Subsystem: vm

In vm.runInNewContext (and maybe other versions of "run", too), the options.columnOffset has no effect. I've tried a couple of intentionally-caused errors with various columnOffset values (ranging from -5 to 5), but the character position reported in the stack trace (via e.stack) is always the same. Note that lineOffset works as expected.

@devsnek
Copy link
Member

@devsnek devsnek commented Mar 19, 2019

I can't reproduce this in 10.15.3

> vm.runInNewContext('throw new Error();', {}, { columnOffset: 20 })
evalmachine.<anonymous>:1
throw new Error();
^

Error
    at evalmachine.<anonymous>:1:27
@Eric24
Copy link
Author

@Eric24 Eric24 commented Mar 19, 2019

@devsnek : Yes, I get the same results with your example, but it appears to only work when the VM code is a single line. I've created a stripped-down version of my code that shows the one change that makes it work or not:

In this example, columnOffset is ignored:

let code = `
throw new Error();
`;
vm.runInNewContext(code, {}, {columnOffset: 20});

// RESULT (truncated):
evalmachine.<anonymous>:2
throw new Error();
^

Error
    at evalmachine.<anonymous>:2:7

But in this example, it works:

let code = `throw new Error();`;
vm.runInNewContext(code, {}, {columnOffset: 20});

// RESULT (truncated):
evalmachine.<anonymous>:1
throw new Error();
^

Error
    at evalmachine.<anonymous>:1:27

Note that the only difference is whether code has an extra newline. In my real code, the source JS is loaded from a file, and typically contains many newlines, but this example (using either a template as show or a simple string with \n characters) shows the difference. That's why it was never working for me (I hadn't tried a simple one-line code test).

PS - The "line" difference between the two examples is expected, and this is easily adjusted using lineOffiset, which works fine in both cases.

@devsnek
Copy link
Member

@devsnek devsnek commented Mar 19, 2019

The column offset only affects the first line. The reason column offset exists is for script tags in html:

<body>
  <script>maybe code
code
code
code
  </script>
</body>

Every line that isn't the first script tag line has a correct column no matter what, because they start fromt the 1st column. But the source of the js in the first line is offset by 10 (" <script>"), so in order to display the correctly attribute an error to the location of the code within an html document, the offset is used here.

@Eric24
Copy link
Author

@Eric24 Eric24 commented Mar 19, 2019

Hmmm. OK. I guess that explains what I'm seeing. If it's only supposed to affect the first line, then maybe this is just a documentation update.

@jasnell
Copy link
Member

@jasnell jasnell commented Jun 19, 2020

Marking this as a good first issue doc update.

@jasnell jasnell added the help wanted label Jun 26, 2020
@harshilparmar
Copy link

@harshilparmar harshilparmar commented Aug 4, 2020

@jasnell Can I do this?

@jasnell
Copy link
Member

@jasnell jasnell commented Aug 4, 2020

Please feel free!

@harshilparmar
Copy link

@harshilparmar harshilparmar commented Aug 5, 2020

@jasnell I am adding Note beside columnOffset definition
here https://nodejs.org/api/vm.html#vm_vm_runinnewcontext_code_contextobject_options
Is it correct? Please correct me if I am wrong!!

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

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.
X Tutup