Repository navigation
fix: resolve the caller frame in CJS error stacks - #1532
Open
aashish254 wants to merge 2 commits into
Open
aashish254 wants to merge 2 commits into
aashish254 wants to merge 2 commits into
Conversation
`Fail.getCallerLocationFromString()` anchored the caller on either a `Proxy.set` frame or a `file://` URL. A CJS stack has neither, so `lines[-1]` was read as the fallback index and the whole raw stack became the `at` line of every `ProcessError` message. Skip the frames raised from zx's own module and node internals instead, and keep the first frame left.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Suggests an improvement — no open issue covers it. #629 ("bun support: Error.stack format is different", closed 2024-03) is the sibling case for a different runtime's stacks; the CJS shape is still unhandled.
What
Fail.getCallerLocationFromString()resolved the caller frame by either finding aProxy.setmarker frame or taking the first frame containingfile://. A CJSstack provides neither: the frames carry plain paths and Node never names a
Proxy.setframe for the$proxy in the CJS build. With no marker found thefunction computed
offset = -1and readlines[-1], which isundefined, so itfell through to returning the entire raw stack as the
atline.Impact
Every
ProcessErrorraised from arequire('zx')script dumps ~10 frames of zxand Node internals into its message, and the first line is nonsense (the stack
header gets prefixed with
at):After the change, the same call (
await $({nothrow: true})\exit 3`` from CJS):12 lines become 1. ESM is untouched: its stacks always reach the existing
file://branch first, and I verified that the message for the same script as.mjsis byte-identical before and after.How
The new branch keeps the
Proxy.setrule when it applies. When it does not, thecaller is the first frame that is neither from the module that raised the error
(zx's own
core.cjs, read offlines[1], since the error is created insidegetCallerLocation) nor anode:internal — which also covers the deeper.sync/ preset chains, where zx frames andAsyncLocalStorage.runinterleave.Only stacks that previously returned a raw stack dump can take the new branch, so
no currently-correct output changes shape.
Verification
Run on
origin/main(65fc542) + this commit, Node 24,TERM=dumb FORCE_COLOR=3:npm test(build →test:size→fmt:check→test:unit→test:types→test:license): exit 0 — 270 tests, 269 pass, 0 fail, 1 skipped;tsd1/1; prettier clean; all five size budgets green.npm run test:coverage:src/error.tsat 100% statements / branches / functions / lines; all files 98.6% lines, 91.06% branches (thresholds 98/90).npm run test:smoke:cjsandnpm run test:smoke:mjs: ok. The CJS smoke suite gains a case that asserts the message ends withat <this file>:<line>:<col>immediately followed byexit code: 3, and that nocore.cjsframe appears in it.test/error.test.tsuse stacks captured from a real CJS run (async,.sync/preset chain with interleaved internals, and an all-internals stack).src/error.tsto itsHEAD~1content fails exactlygetCallerLocationFromString-CJSandgetCallerLocationFromString-CJS sync(267/270 pass); reverting onlybuild/core.cjsfails the new smoke case attest/smoke/node.test.cjs:32. So both the unit and the smoke witnesses are load-bearing.Size budget
The fix adds ~160 B to
build/core.cjs, which trips threesize-limitbudgets(
zx-lite129.46 → measured 129.62,js parts881.85 → 882.02,all944.60 →944.73). I raised the three limits to the measured values, the same way #1423 and
#1464 did. The regenerated bundle is committed in this change.
Note for whoever merges: #1526 also edits
.size-limit.json(js parts→ 883.03,all→ 945.89), so the two conflict on those two numbers. The resolution istake-the-larger for those two lines and keep this PR's
129.64 kBforzx-lite.src/error.ts,test/*andbuild/core.cjsmerge cleanly against #1526, #1315,#1527 and #1531 (checked with
git merge-tree --write-treein both orders).npm buildbefore committing and verified the bundle updates correctly.run testand confirmed all tests succeed. Added tests to cover my changes if needed.