Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

fix: improve panic message for unspecified ScriptKind in Parser::initializeState - #1556

Merged
Jake Bailey (jakebailey) merged 2 commits into
microsoft:mainfrom
camc314:c/more-helpful-panic
Dec 15, 2025
Merged

Jake Bailey (jakebailey) merged 2 commits into
microsoft:mainfrom
camc314:c/more-helpful-panic

Conversation

@camc314

Copy link
Copy Markdown
Contributor

Currently this message give no diagnostic information, and no information about how to fix the issue.

ScriptKindUnknown, probably comes from here when the extension failed to be fetched, by including the filename in the diagnostic, it should allow devs/users to more easily track down the source of the panic

func GetScriptKindFromFileName(fileName string) ScriptKind {
dotPos := strings.LastIndex(fileName, ".")
if dotPos >= 0 {
switch strings.ToLower(fileName[dotPos:]) {
case tspath.ExtensionJs, tspath.ExtensionCjs, tspath.ExtensionMjs:
return ScriptKindJS
case tspath.ExtensionJsx:
return ScriptKindJSX
case tspath.ExtensionTs, tspath.ExtensionCts, tspath.ExtensionMts:
return ScriptKindTS
case tspath.ExtensionTsx:
return ScriptKindTSX
case tspath.ExtensionJson:
return ScriptKindJSON
}
}
return ScriptKindUnknown
}

Ref: oxc-project/oxc#12950

@jakebailey

Copy link
Copy Markdown
Member

I think we avoid including user-specified data in panics/throws just because that means we can't collect it for telemetry on errors. But maybe the old code already broke this rule?

@camc314

Copy link
Copy Markdown
Contributor Author

Hmm that's a fair point, feel free to close. However i do feel that the value in extra debugging info is valuable.

A large number of the panics do have additional data:

Total panics: (ignoring _test.go files)

$ rg -t go --count-matches --no-filename '\bpanic\s*\('  -g '!**/*_test.go' | awk '{s+=$1} END{print s}'
538

Panics with either string concatenation, or string formatting:

$ rg -t go --count-matches --no-filename -g '!**/*_test.go'   -e 'panic\(\s*fmt\.\w+\s*\('   -e 'panic\(\s*(?:"(?:[^"\\]|\\.)*"|`[^`]*`)\s*\+' | awk '{s+=$1} END{print s}'
161

For telemetry, couldn't just the first part of the string be matched?

@jakebailey

Copy link
Copy Markdown
Member

Searching for just prints in panics isn't really enough, given many of those aren't printing out filenames or file contents.

We'll have to think about this, but definitely be aware that we don't even have an API at the moment, so oxc's usage really comes without warranty (they're probably "holding it wrong", since we haven't exactly optimized for anyone using it outside our own). 😅

@jakebailey

Copy link
Copy Markdown
Member

Honestly, I think we can just take this; NewSourceFile already panics with a filename. I would just merge from main, however.

Comment thread internal/parser/parser.go Resolved
Copilot AI review requested due to automatic review settings December 15, 2025 14:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves the diagnostic information in a panic message when ScriptKind is not specified during source file parsing. The change adds the filename to the error message, making it easier for developers to identify which file is causing the issue.

Key Changes

  • Enhanced panic message to include the filename from opts.FileName

Comment thread internal/parser/parser.go Resolved
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants