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

lib: fix inspector links for stack traces #35725

Open
wants to merge 7 commits into
base: master
from

Conversation

@Dragiyski
Copy link

@Dragiyski Dragiyski commented Oct 20, 2020

When the chrome inspector (chrome://inspect) display stack traces, it
makes the filename clickable, and developer can easily locate the
relevant source with a single click. This only works when filenames are
valid URLs. The CJS loader seems to compile sources with absolute path
instead of URL, breaking the inspector functionality. This is especially
apparent when node_modules contain "at" symbol (@). The loader already
presents module to the inspector using file URL, the same URL should be
used for compiling the source.

For example the current stack trace looks like this:

Current Stack Trace

TypeError: (...)
    at Object.<anonymous> (/home/dragiyski/(...)/@dragiyski/(...)/test.js:19:5)

and as shown the link covers only the part after the first slash after the @ directory. The link leads to URL starting with slash (/), which opens an empty (about:blank) tab within the browser. This is an expected behavior, because in URLs @ represents user@host delimiter and the path is not valid absolute URL as it does not have protocol. A properly fixed URL looks like this:

Fixed Stack Trace

TypeError: (...)
    at Object.<anonymous> (file:///home/dragiyski/(...)/@dragiyski/(...)/test.js:19:5)

and clicking on the link provided in inspector opens the actual location of the source for all call sites, including node_modules containing @ symbol in their path. For the console, the path is printed with file:// in front and clickable in some terminals.

Note: (...) replaces a valid path omitted for security reasons.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
When the chrome inspector (chrome://inspect) display stack traces, it
makes the filename clickable, and developer can easily locate the
relevant source with a single click. This only works when filenames are
valid URLs. The CJS loader seems to compile sources with absolute path
instead of URL, breaking the inspector functionality. This is especially
apparent when node_modules contain "at" symbol (@). The loader already
presents module to the inspector using file URL, the same URL should be
used for compiling the source.
@nodejs-github-bot
Copy link

@nodejs-github-bot nodejs-github-bot commented Oct 20, 2020

Review requested:

Dragiyski added 3 commits Oct 21, 2020
It seems getErrMessage() function uses filename from a CallSite to
open a file and extract source code location of the statement. The
CallSite now returns file URL, so it must be converted back to path.
Source maps seems to use CallSite from the prepareStackTrace to the path
to the original sources on the stack. Finding the source map should
convert file URL to path and attached information should be file URL
instead of path, so the inspector can show a proper link. Additionally,
it seems URL to path conversion was made by stripping "file://" prefix,
which might cause problem on windows. The url module has function to
deal with that.
Various tests explore the stack trace or CallSite objects' filename.
They are modified now to match file URL instead.
@Trott Trott added the request-ci label Oct 21, 2020
@github-actions github-actions bot removed the request-ci label Oct 21, 2020
@Dragiyski Dragiyski changed the title fix: inspector links for stack traces lib: fix inspector links for stack traces Oct 21, 2020
Dragiyski added 2 commits Oct 21, 2020
Based on suggestion of @ljharb
Based on suggestion of @ljharb
@codecov-io
Copy link

@codecov-io codecov-io commented Oct 21, 2020

Codecov Report

Merging #35725 into master will decrease coverage by 0.00%.
The diff coverage is 89.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #35725      +/-   ##
==========================================
- Coverage   96.40%   96.40%   -0.01%     
==========================================
  Files         222      223       +1     
  Lines       73682    73713      +31     
==========================================
+ Hits        71034    71061      +27     
- Misses       2648     2652       +4     
Impacted Files Coverage Δ
lib/internal/source_map/source_map_cache.js 84.66% <76.47%> (-0.68%) ⬇️
lib/assert.js 98.11% <100.00%> (+0.01%) ⬆️
lib/internal/modules/cjs/loader.js 94.41% <100.00%> (+0.01%) ⬆️
lib/internal/source_map/prepare_stack_trace.js 95.08% <100.00%> (+0.12%) ⬆️
lib/util/types.js 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6b6bbfe...f976a8b. Read the comment docs.

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

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.
X Tutup