-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Handle extensionless root files gracefully instead of panicking #4628
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
Open
UditDewan
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
UditDewan:fix/extensionless-root-file-panic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
29 changes: 29 additions & 0 deletions
29
internal/fourslash/tests/goToSourceDefinitionExtensionlessMappedSource_test.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| // A declaration map can name anything in its `sources`, including a file whose | ||
| // name has no recognized extension. Parsing that file used to hit the parser's | ||
| // "ScriptKind must be specified" assert and crash the request. | ||
| func TestGoToSourceDefinitionExtensionlessMappedSource(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `// @Filename: /lib/helper.d.ts | ||
| export declare function helper(): string; | ||
| //# sourceMappingURL=helper.d.ts.map | ||
| // @Filename: /lib/helper.d.ts.map | ||
| {"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["helper"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,IAAI,MAAM,CAAC"} | ||
| // @Filename: /lib/helper | ||
| export function helper(): string { return ""; } | ||
| // @Filename: /index.ts | ||
| import { /*usage*/helper } from "./lib/helper"; | ||
| helper();` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyBaselineGoToSourceDefinition(t, "usage") | ||
| } |
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
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
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
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
7 changes: 7 additions & 0 deletions
7
...urslash/goToSourceDefinition/goToSourceDefinitionExtensionlessMappedSource.baseline.jsonc
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // === goToSourceDefinition === | ||
| // === /lib/helper === | ||
| // <|export function [|helper|](): string { return ""; }|> | ||
|
|
||
| // === /index.ts === | ||
| // import { /*GOTO SOURCE DEF*/helper } from "./lib/helper"; | ||
| // helper(); |
47 changes: 47 additions & 0 deletions
47
testdata/baselines/reference/tsc/commandLine/extensionless-file-in-tsconfig-exists.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| currentDirectory::/home/src/workspaces/project | ||
| useCaseSensitiveFileNames::true | ||
| Input:: | ||
| //// [/home/src/workspaces/project/src/script] *new* | ||
| const n: number = "s"; | ||
| //// [/home/src/workspaces/project/tsconfig.json] *new* | ||
| { | ||
| "files": ["./src/script"] | ||
| } | ||
|
|
||
| tsgo -p ./tsconfig.json | ||
| ExitStatus:: DiagnosticsPresent_OutputsGenerated | ||
| Output:: | ||
| [91merror[0m[90m TS6231: [0mCould not resolve the path '/home/src/workspaces/project/src/script' with the extensions: '.ts', '.tsx', '.d.ts', '.cts', '.d.cts', '.mts', '.d.mts'. | ||
| The file is in the program because: | ||
| Part of 'files' list in tsconfig.json | ||
| [96mtsconfig.json[0m:[93m2[0m:[93m31[0m - File is matched by 'files' list specified here. | ||
| [7m2[0m "files": ["./src/script"] | ||
| [7m [0m [96m ~~~~~~~~~~~~~~[0m | ||
|
|
||
|
|
||
| Found 1 error. | ||
|
|
||
| //// [/home/src/tslibs/TS/Lib/lib.es2025.full.d.ts] *Lib* | ||
| /// <reference no-default-lib="true"/> | ||
| interface Boolean {} | ||
| interface Function {} | ||
| interface CallableFunction {} | ||
| interface NewableFunction {} | ||
| interface IArguments {} | ||
| interface Number { toExponential: any; } | ||
| interface Object {} | ||
| interface RegExp {} | ||
| interface String { charAt: any; } | ||
| interface Array<T> { length: number; [n: number]: T; } | ||
| interface ReadonlyArray<T> {} | ||
| interface SymbolConstructor { | ||
| (desc?: string | number): symbol; | ||
| for(name: string): symbol; | ||
| readonly toStringTag: symbol; | ||
| } | ||
| declare var Symbol: SymbolConstructor; | ||
| interface Symbol { | ||
| readonly [Symbol.toStringTag]: string; | ||
| } | ||
| declare const console: { log(msg: any): void; }; | ||
|
|
39 changes: 39 additions & 0 deletions
39
testdata/baselines/reference/tsc/commandLine/extensionless-file-on-command-line-exists.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| currentDirectory::/home/src/workspaces/project | ||
| useCaseSensitiveFileNames::true | ||
| Input:: | ||
| //// [/home/src/workspaces/project/script] *new* | ||
| const n: number = "s"; | ||
|
|
||
| tsgo script | ||
| ExitStatus:: DiagnosticsPresent_OutputsGenerated | ||
| Output:: | ||
| [91merror[0m[90m TS6231: [0mCould not resolve the path 'script' with the extensions: '.ts', '.tsx', '.d.ts', '.cts', '.d.cts', '.mts', '.d.mts'. | ||
| The file is in the program because: | ||
| Root file specified for compilation | ||
|
|
||
| Found 1 error. | ||
|
|
||
| //// [/home/src/tslibs/TS/Lib/lib.es2025.full.d.ts] *Lib* | ||
| /// <reference no-default-lib="true"/> | ||
| interface Boolean {} | ||
| interface Function {} | ||
| interface CallableFunction {} | ||
| interface NewableFunction {} | ||
| interface IArguments {} | ||
| interface Number { toExponential: any; } | ||
| interface Object {} | ||
| interface RegExp {} | ||
| interface String { charAt: any; } | ||
| interface Array<T> { length: number; [n: number]: T; } | ||
| interface ReadonlyArray<T> {} | ||
| interface SymbolConstructor { | ||
| (desc?: string | number): symbol; | ||
| for(name: string): symbol; | ||
| readonly toStringTag: symbol; | ||
| } | ||
| declare var Symbol: SymbolConstructor; | ||
| interface Symbol { | ||
| readonly [Symbol.toStringTag]: string; | ||
| } | ||
| declare const console: { log(msg: any): void; }; | ||
|
|
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.
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.
Is this the only place we need to do this?
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.
Nah, it wasn't. I went through the other callers that derive a script kind from a file name and found one more live crash:
getOrParseSourceFileininternal/ls/sourcedefinition.go. A declaration map'ssourcesentries are arbitrary strings, so a.d.ts.mapnaming an existing extensionless file panickedcustom/textDocument/sourceDefinitionwith the same assert:Fixed in the follow-up commit, with a fourslash regression test; it now navigates into the mapped file instead of crashing.
The rest of what I looked at:
internal/execute/tsc.go(fmtMain) has the same pattern, but it's dead code today since its only caller, the-fcase, is commented out. Happy to switch it over too if you want.internal/project/api.godeliberately returns "unsupported file extension" instead of parsing, so I left it alone.GetScriptKindfield on the ATA request insession.gois never read by the installer.harnessutilpanics on purpose in the test harness.