[nightly] Syntactically invalid declaration files emitted under extremely specific conditions #58807
Labels
Bug
A bug in TypeScript
Domain: Declaration Emit
The issue relates to the emission of d.ts files
Fix Available
A PR has been opened for this issue
Milestone
π Search Terms
declaration syntax invalid comment docstring
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/dev/bug-workbench/?target=7&composite=true&ts=5.6.0-dev.20240608#code/PTAEAEGMHsFsAdoGcCWAXApgLlGgTgK4YBQIES+KkaO+RpY4AJhpADYCGeHaK0AdrUIky4JAAtoAdwCisdA3KTZ8tJiYAxFG2ygOAOib60SYovAAzbRn4dYugyeIoE0PGlABvUACNoADwwmACEA0ABfUAs8OFAAIn1gP0CQgLiAbjMMf0R3UBh+ClAAfVAAXl8AoND-TMUAQQ8UfhZ-UAAOAGZSACoe4lAe0CkeAZ7gM1ErHVt7HGTqgMNjU2zcjxZ2Lgx8gSKF1P8cF3WACgSktIBKfRqAHk8B0GeD49d3c8TkuJv7-gJYD4MHgAHyZcJg55Q6Ew2HQsgAPQAKuIUEhQGiMS1sh1OljcKj0dMSOZibNdMllk41m4NqxOHgdlskOj7kiQV4ns8AG4cNhEHBIzLQgoUQjUNynXn83RIq7g4g0vKbBk7CwEfjUPj8Sr+O7sqV8gWgOU4Nlg4hAA
π» Code
π Actual behavior
a.d.ts
is emitted asπ Expected behavior
a.d.ts
is emitted asAdditional information about the issue
Given the extremely specific conditions under which this bug arises, somehow we hit the lottery by encountering it. Basically what I observed is that there is a point where this code is reached:
TypeScript/src/compiler/emitter.ts
Lines 5715 to 5728 in d8086f1
in the following state:
node
refers toBox
, the one that's part ofimport("./box").Box
that comes just before the syntax error.getSourceFileOfNode(node)
evaluates to theb.d.ts
which is different fromcurrentSourceFile
, which isa.ts
node.pos
andnode.end
to figure out which comment range to get the comment from. But these indexes are relative tob.d.ts
, nota.ts
! So we attempt to look for a comment ina.ts
. Most of the time no comment would be found at these positions ina.ts
, but by sheer luck, there happens to be a comment ina.ts
starting right atnode.end
!Imagine our surprise when my teammate's compilation started failing after touching a completely different part of a different file. They were just unlucky enough to have shifted a token into the exact right spot to surface the bug.
The text was updated successfully, but these errors were encountered: