-
Notifications
You must be signed in to change notification settings - Fork 680
Fix panic runtime error on ast.KindTupleTypes #1525
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
base: main
Are you sure you want to change the base?
Conversation
Do you have a test which reproduces this for the PR? It seems like you know the conditions, so laying out a compiler test for it would be helpful. |
I tried super hard to find a test that failing and many combinations, still no luck of a compiler test that would fail. |
We typically don't do that, but we could. What I'm not understanding is why this particular fix is needed; the original code says: if (isTupleTypeNode(input) && (getLineAndCharacterOfPosition(currentSourceFile, input.pos).line === getLineAndCharacterOfPosition(currentSourceFile, input.end).line)) {
setEmitFlags(input, EmitFlags.SingleLine);
} Which is equivalent to the original code, and that sort of tells me the problem is elsewhere, e.g. maybe we have forgotten to set the text on the intermediate SourceFile? |
Even the
|
I understand that they are different, but they would be different in the old TS codebase too. It's odd that a 1:1 port is not working here. Calling |
I tried asking AI to look for this case, failing and passing and it has trouble finding where the react types are being inlined when there is no explicit React import, if you have any ideas on where it happen, I can take further look |
I have a test in this pr that reproduces the bug |
Turned into a compiler test, that might work, but we usually do not add this sort of unit test since it's not clear that the unit tests is actually setting up things how the actual compiler does. |
73be8e8
to
1d12515
Compare
@jakebailey GPT-5 found this a fix that actually works, please let me know if it makes sense, also I am not able to write a compiler test that fails Queue late-painted statements only from the active file: TS sets currentSourceFile at file entry for both single-file and bundle flows and assumes the late-painted queue is for the same file; it then uses currentSourceFile for position math during declaration emit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can understand how this would fix the problem - this kind of check is pervasive in the node builder - but since strada doesn't have this check here (and doesn't seem to need to?) we'd really want a test case demonstrating the crash, so we can see where, exactly, the divergence in behavior is. I'd much prefer to fix the difference in behavior at the root than at some symptom.
Tried many test cases on compiler tests and couldn't do the panic |
Sure, but what I'm getting at is that the issue isn't that these paths don't check for containing file consistency, but rather that the built/marked nodes shouldn't be getting their TL;DR: This is the wrong fix - it's masking a symptom, not fixing the cause. |
Finally could create a reproduction do |
I followed those steps and it did not panic. |
Are you on this branch on that repo? |
Yes, I now see the crash. #1408 also reproduces this with probably less stuff in the repo, so it might be a better candidate for mimimization. Though neither of these are minimal. |
Surprisingly, this issue does seem to require a tsbuildinfo be present to work; same happens in #1408's. |
Hm, no, it doesn't. |
Here is a minimal repro: // @strict: true
// @module: preserve
// @declaration: true
// @filename: node_modules/knex/index.d.ts
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
// A bunch of random text to move the positions forward
type ShouldJustBeAny = [any][0];
declare namespace knex {
export { Knex };
}
declare namespace Knex {
interface Interface {
method(): ShouldJustBeAny;
}
}
export = knex;
// @filename: index.ts
import "knex";
declare module "knex" {
namespace Knex {
function newFunc(): Knex.Interface;
}
} |
Debugging, what I'm seeing is that in |
Yeah, so the code that we have now is the same as Strada and behaves the same. We really do attempt to ask for position information out of the wrong file. This only succeeds because I think the fix is either to make |
1d12515
to
04b3562
Compare
Updated based on your suggestions, let me know if the actual fix is something else |
@microsoft-github-policy-service agree |
I would suspect that you need to do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a panic runtime error that occurs when processing AST nodes of type KindTupleTypes
during declaration emit. The root issue was that the declaration transformer was using the wrong source file context when calculating position information for nodes that originated from dependency files.
- Uses the node's original source file instead of the current source file for position calculations
- Adds proper source file resolution using
MostOriginal()
andGetSourceFileOfNode()
- Includes a comprehensive test case that reproduces the cross-file node processing scenario
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
internal/transformers/declarations/transform.go |
Fixed source file context resolution for TupleType nodes by using original node's source file for position calculations |
testdata/tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts |
Added test case with module augmentation scenario to reproduce the bug |
testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.* |
Generated baseline files showing expected compiler output for the test case |
Fixes #1501
Fixes #1408
Root cause from my debugging?
The declaration transformer is processing AST nodes that originated from
@types/react/index.d.ts
(the large file), but it's trying to use those node positions with a completely different source file (types.ts) as the context.This is what I suspected - cross-file node processing during incremental compilation.
The incremental build system mixes nodes from different files, but the transformer was always using
tx.state.currentSourceFile
for position calculations instead of the node's actual source file.What's the fix?
When handling TupleType nodes, we now compute line/character information against the node’s original parse location by using original := EmitContext().MostOriginal(input) and then the originalSourceFile derived from that node. If the original source file is available and the tuple spans a single line there, we set EFSingleLine. This ensures formatting and position calculations are based on the correct file, regardless of whether the node originated in the current file or a dependency, eliminating mis-formatting and position errors during declaration emit.