OK, so ParsedOptions has CompilerOptions *CompilerOptions. And ParsedCommandLine has:
type ParsedCommandLine struct {
ParsedConfig *core.ParsedOptions `json:"parsedConfig"`
// ...
}
And CompilerOptions() is:
func (p *ParsedCommandLine) CompilerOptions() *core.CompilerOptions {
if p == nil {
return nil
}
return p.ParsedConfig.CompilerOptions
}
If p is not nil but p.ParsedConfig is nil, then p.ParsedConfig.CompilerOptions would try to access .CompilerOptions on a nil pointer. In Go, this is safe and returns nil.
So the only way to crash is if:
p.opts.Config is nil, CompilerOptions() returns nil, Options() returns nil, and then nil.SingleThreaded somehow panics.
But in Go, nil.SingleThreaded is safe because SingleThreaded is a value field, not a method.
Unless... wait, let me check if SingleThreaded is a field or a method.
Looking at CompilerOptions:
type CompilerOptions struct {
// ...
SingleThreaded Tristate `json:"singleThreaded,omitzero"`
// ...
}
SingleThreaded is a field (of type Tristate), not a method. So accessing it on a nil pointer should be safe.
Let me think about this differently. Maybe the issue is not a nil pointer dereference but something else.
Actually, wait! Let me re-read the issue description:
Fatal crash in (*Program).SingleThreaded on snapshot update
And the stack trace shows the call chain. But the crash could be happening not in the line shown but somewhere else in the call chain.
Actually, let me look at the actual line 416 again:
func (p *Program) SingleThreaded() bool {
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
}
The panic could be at any point in this expression:
p.opts.SingleThreaded.DefaultIfUnknown(...) - safe, SingleThreaded is a value
p.Options().SingleThreaded - could panic if p.Options() returns nil and accessing .SingleThreaded on nil causes a panic
Wait, actually in Go, accessing a field on a nil struct pointer IS safe for value types. But there might be something I'm missing.
Let me actually run a quick test to verify:
Actually, I can't run Go code right now. Let me think about this more carefully.
OK here's my understanding of Go semantics:
- Accessing a field on a nil struct pointer is safe for value types (returns zero value)
- Accessing a field on a nil struct pointer is safe for pointer types (returns nil)
- Calling a method on a nil struct pointer panics UNLESS the method handles the nil case
So nil.SingleThreaded where SingleThreaded is a Tristate (a byte alias) should return 0 (TSUnknown).
But what about nil.DefaultIfUnknown(...)? The DefaultIfUnknown method is:
func (t Tristate) DefaultIfUnknown(value Tristate) Tristate {
if t == TSUnknown {
return value
}
return t
}
This is a method on a value type (Tristate), not a pointer. So it's always called on a value, not a nil pointer. The return value of p.Options().SingleThreaded is a Tristate value, which is then used to call .DefaultIfUnknown(...) on it.
So there should be no nil pointer dereference here.
Unless... wait, let me look at the expression more carefully:
p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
This is evaluated as:
p.opts.SingleThreaded - returns a Tristate value
p.Options().SingleThreaded - this is the argument to DefaultIfUnknown
p.Options() calls p.opts.Config.CompilerOptions()
- If
p.opts.Config is nil, CompilerOptions() returns nil (safe due to nil check)
- If
p.opts.Config.ParsedConfig is nil, p.ParsedConfig.CompilerOptions returns nil
- Then
nil.SingleThreaded returns 0 (TSUnknown) - safe in Go
TristateValue.DefaultIfUnknown(TristateValue) - this is a method call on a value, safe
TristateValue.IsTrue() - method call on a value, safe
So this should NOT panic!
Unless there's a different issue. Maybe the panic is actually happening elsewhere, and the stack trace is misleading.
Actually, let me look at the issue from a different angle. Maybe the issue is that p.opts.Config is not nil, but p.opts.Config.ParsedConfig is nil, and some other code path tries to access ParsedConfig methods or fields.
Actually, let me look at the processAllProgramFiles function again:
func processAllProgramFiles(
opts ProgramOptions,
singleThreaded bool,
) processedFiles {
compilerOptions := opts.Config.CompilerOptions()
rootFiles := opts.Config.FileNames()
...
}
Wait, this is being called AFTER SingleThreaded() returns. So the panic must be happening before this.
OK let me step back and think about this differently. The stack trace says:
internal/compiler.(*Program).SingleThreaded()
internal/compiler/program.go:416
internal/compiler.NewProgram()
internal/compiler/program.go:274
This clearly shows that SingleThreaded() at line 416 is being called from NewProgram() at line 274. The crash happens inside SingleThreaded().
Let me look at what could panic at line 416. The only line in SingleThreaded() is:
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
If p.opts.Config is nil:
p.opts.Config.CompilerOptions() - safe, returns nil (due to nil check in CompilerOptions())
nil.SingleThreaded - safe in Go, returns 0
TSUnknown.DefaultIfUnknown(TSUnknown) - safe, returns TSUnknown
TSUnknown.IsTrue() - safe, returns false
So this should NOT panic.
UNLESS the CompilerOptions() method doesn't properly handle the nil case. Let me re-read it:
func (p *ParsedCommandLine) CompilerOptions() *core.CompilerOptions {
if p == nil {
return nil
}
return p.ParsedConfig.CompilerOptions
}
Wait, this checks if p == nil, but p is a *ParsedCommandLine receiver. If p.opts.Config is nil (i.e., the receiver is nil), then the check p == nil is true, and it returns nil. So this is safe.
Hmm, but what if p.opts.Config is not nil, but p.opts.Config.ParsedConfig is nil? Then p.ParsedConfig.CompilerOptions would try to access .CompilerOptions on a nil *ParsedOptions.
In Go:
type A struct {
B *B
}
type B struct {
C int
}
a := &A{B: nil}
a.B.C // This is safe! Returns 0.
So even if ParsedConfig is nil, accessing .CompilerOptions is safe because CompilerOptions is a pointer type, and accessing a nil pointer field is safe.
Wait, that's not quite right. Let me think again.
ParsedCommandLine.ParsedConfig is *ParsedOptions.
ParsedOptions.CompilerOptions is *CompilerOptions.
So p.ParsedConfig.CompilerOptions is:
- First,
p.ParsedConfig is evaluated (returns *ParsedOptions, could be nil)
- Then
.CompilerOptions is accessed on that
If p.ParsedConfig is not nil, then .CompilerOptions is accessed on a valid pointer, and returns *CompilerOptions.
If p.ParsedConfig is nil, then we're accessing a field on a nil pointer. In Go, this is safe and returns the zero value of the field type. Since CompilerOptions is a pointer type, the zero value is nil.
So this should also be safe!
OK, I'm going in circles. Let me try a different approach. Let me look at what could actually cause a panic.
Wait, I just realized something. Maybe the issue is NOT a nil pointer dereference at all. Maybe it's something else, like an out-of-bounds access or a division by zero.
But the stack trace shows the crash is in SingleThreaded(), and the only code there is:
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
The only thing I can think of is that p.opts itself is somehow corrupted or invalid.
Actually, let me look at the Program struct again:
type Program struct {
opts ProgramOptions
// ...
}
And ProgramOptions:
type ProgramOptions struct {
Host CompilerHost
Config *tsoptions.ParsedCommandLine
// ...
}
So opts is a value, not a pointer. When we do p.opts.SingleThreaded, we're accessing a field on a value struct. This is always safe.
Hmm, let me think about this from yet another angle. Maybe the issue is that the code compiles and runs, but there's a subtle bug somewhere that causes the panic.
Actually, let me look at the stack trace one more time:
internal/compiler.(*Program).SingleThreaded()
internal/compiler/program.go:416
internal/compiler.NewProgram()
internal/compiler/program.go:274
internal/project.(*Project).CreateProgram()
internal/project/project.go:376
internal/project.(*ProjectCollectionBuilder).updateProgram.func2.1()
internal/project/projectcollectionbuilder.go:1055
internal/project/dirty.(*Box[...]).Change()
internal/project/dirty/box.go:41
internal/project.(*ProjectCollectionBuilder).updateProgram.func2()
internal/project/projectcollectionbuilder.go:1052
internal/project/dirty.(*Box[...]).Locked()
internal/project/dirty/box.go:57
internal/project.(*ProjectCollectionBuilder).updateProgram()
internal/project/projectcollectionbuilder.go:1051
internal/project.(*ProjectCollectionBuilder).DidRequestFile()
internal/project/projectcollectionbuilder.go:397
internal/project.(*Snapshot).Clone()
internal/project/snapshot.go:353
internal/project.(*Session).updateSnapshot()
internal/project/session.go:1101
internal/project.(*Session).getSnapshot()
internal/project/session.go:819
internal/project.(*Session).getSnapshotAndDefaultProject()
internal/project/session.go:877
internal/project.(*Session).GetLanguageService()
internal/project/session.go:890
internal/lsp/init.func1.registerLanguageServiceDocumentRequestHandler[...].13()
internal/lsp/server.go:826
internal/lsp.(*Server).handleRequestOrNotification()
internal/lsp/server.go:669
Wait, the crash is happening deep in the LSP server's request handling. The updateProgram function calls project.CreateProgram(), which calls NewProgram(), which calls SingleThreaded().
Let me look at what could be different between a normal call and the snapshot update call.
Actually, I think I need to just look at the issue more pragmatically. The issue says there's a fatal crash. Let me look for any potential nil pointer dereferences in the SingleThreaded() method.
The issue could be that p.opts.Config is nil, and while accessing .CompilerOptions() is safe (due to the nil check), maybe there's something else going on.
Actually, wait. Let me re-read the CompilerOptions() method:
func (p *ParsedCommandLine) CompilerOptions() *core.CompilerOptions {
if p == nil {
return nil
}
return p.ParsedConfig.CompilerOptions
}
This returns p.ParsedConfig.CompilerOptions. If p.ParsedConfig is nil, accessing .CompilerOptions is safe (returns nil). But what if p.ParsedConfig is not nil but p.ParsedConfig.CompilerOptions is also nil?
Then CompilerOptions() returns nil. And then p.Options() returns nil. And then in SingleThreaded():
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
// p.Options() returns nil
// nil.SingleThreaded - in Go, this should return 0 (zero value of Tristate)
Wait, is this actually true? Let me think again.
In Go, if you have:
type Foo struct {
Bar int
}
func main() {
var f *Foo
fmt.Println(f.Bar) // Does this panic?
}
Actually, this DOES panic! No wait, let me check again.
Actually, I think in Go, accessing a field on a nil struct pointer is safe for value types:
type Foo struct {
Bar int
}
func main() {
var f *Foo
fmt.Println(f.Bar) // This prints 0, no panic
}
Let me verify this is correct. Yes, in Go, accessing a field on a nil struct pointer is safe. It returns the zero value of the field type. This applies to all value types (int, bool, struct, etc.) and pointer types (returns nil).
So nil.SingleThreaded should return 0 (TSUnknown), not panic.
OK, I'm really confused now. Let me try a completely different approach. Let me look at what the fix should be based on the issue description.
The issue says "Fatal crash in (*Program).SingleThreaded on snapshot update". The simplest fix would be to add a nil check before accessing p.Options().SingleThreaded.
Let me look at the code one more time:
func (p *Program) SingleThreaded() bool {
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
}
The issue could be that p.Options() returns nil, and then we're trying to call something on nil. Even though in Go, accessing a field on nil is safe, maybe there's something else going on.
Actually, wait. I just realized that SingleThreaded is a field, not a method. But DefaultIfUnknown and IsTrue are methods. Let me trace through:
p.opts.SingleThreaded - this is a value of type Tristate
p.Options().SingleThreaded - this is also a value of type Tristate
value1.DefaultIfUnknown(value2) - method call on value1
result.IsTrue() - method call on result
None of these should panic if p.Options() returns nil, because:
p.opts.SingleThreaded is a value, not a pointer
p.Options().SingleThreaded returns 0 (zero value) if p.Options() returns nil
OK wait, I just realized something. Let me look at the actual expression more carefully:
p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
This is parsed as:
(p.opts.SingleThreaded).DefaultIfUnknown((p.Options()).SingleThreaded).IsTrue()
So:
p.opts.SingleThreaded is evaluated (returns a Tristate value)
p.Options().SingleThreaded is evaluated (returns a Tristate value)
value1.DefaultIfUnknown(value2) is called
result.IsTrue() is called
Step 2 involves calling p.Options(), which is p.opts.Config.CompilerOptions(). If p.opts.Config is nil, then calling a method on a nil receiver...
Actually, in Go, calling a method on a nil receiver is safe IF the method checks for nil and handles it properly. The CompilerOptions() method does check for nil:
func (p *ParsedCommandLine) CompilerOptions() *core.CompilerOptions {
if p == nil {
return nil
}
return p.ParsedConfig.CompilerOptions
}
So if p.opts.Config is nil, CompilerOptions() returns nil.
Then, in step 2, we have nil.SingleThreaded, which should return 0 (TSUnknown).
Then, step 3 calls TSUnknown.DefaultIfUnknown(TSUnknown) (assuming p.opts.SingleThreaded is also TSUnknown), which returns TSUnknown.
Then, step 4 calls TSUnknown.IsTrue(), which returns false.
So the entire expression should evaluate to false without panicking.
Unless... wait, what if p.opts.SingleThreaded is NOT TSUnknown but TSTrue or TSFalse? Let me check:
If p.opts.SingleThreaded is TSTrue:
TSTrue.DefaultIfUnknown(...) - the if t == TSUnknown check is false, so it returns TSTrue
TSTrue.IsTrue() returns true
If p.opts.SingleThreaded is TSFalse:
TSFalse.DefaultIfUnknown(...) - the if t == TSUnknown check is false, so it returns TSFalse
TSFalse.IsTrue() returns false
Either way, this should not panic.
Hmm, I'm really stuck. Let me try a different approach. Let me look at what happens if p.opts.Config is not nil but p.opts.Config.ParsedConfig is nil.
Actually, looking at this again, I think the issue might be that in Go, accessing a field through a nil pointer is NOT always safe. Let me check:
Actually, I was wrong earlier. In Go, accessing a field on a nil struct pointer IS safe. The Go runtime does not panic when you access a field through a nil pointer. The Go spec says:
The expression x.a, after evaluating x, selects the field a, and the result is the contents of the field. If x is a nil pointer, the expression panics.
Wait, that says it panics! Let me check the Go spec more carefully.
Actually, I think the Go spec says:
The primary expression x.a selects the field a of the struct pointed to by x, and the result is the contents of the field. If x is a pointer to a struct, x is not evaluated, and x.a is a compile-time error if x is nil.
No wait, that's not right either. Let me think about this more carefully.
In Go, if you have:
type Foo struct {
Bar int
}
func main() {
var f *Foo
fmt.Println(f.Bar)
}
This DOES panic with "runtime error: invalid memory address or nil pointer dereference".
Oh wait, but that's different from what I thought earlier. Let me verify this.
Actually, I think the Go behavior depends on the type. For struct pointers, accessing a field through a nil pointer DOES panic. But for method receivers, calling a method on a nil pointer is fine IF the method handles it.
Let me verify:
- Accessing a field through a nil struct pointer: PANIC
- Calling a method on a nil receiver: depends on the method (no automatic nil check)
So nil.SingleThreaded where SingleThreaded is a field of CompilerOptions WOULD PANIC!
That's the bug! When p.Options() returns nil (because p.opts.Config.CompilerOptions() returns nil or because p.opts.Config is nil), then nil.SingleThreaded panics.
Wait, but I said earlier that CompilerOptions() has a nil check, so if p.opts.Config is nil, it returns nil. And then nil.SingleThreaded panics.
Actually, let me re-check. In Go:
type Foo struct {
Bar int
}
func main() {
var f *Foo
fmt.Println(f.Bar) // This PANICS
}
Yes, accessing a field through a nil struct pointer PANICS in Go.
So the bug is:
p.opts.Config is nil
p.opts.Config.CompilerOptions() calls CompilerOptions() on a nil receiver
CompilerOptions() checks p == nil and returns nil (safe)
p.Options() returns nil
nil.SingleThreaded PANICS because we're accessing a field on a nil struct pointer
OK, so the fix is to handle the nil case in SingleThreaded():
func (p *Program) SingleThreaded() bool {
if p.opts.Config == nil {
return p.opts.SingleThreaded.IsTrue()
}
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
}
Or alternatively, we can use p.Options() and check if it returns nil:
func (p *Program) SingleThreaded() bool {
options := p.Options()
if options == nil {
return p.opts.SingleThreaded.IsTrue()
}
return p.opts.SingleThreaded.DefaultIfUnknown(options.SingleThreaded).IsTrue()
}
But wait, there's still the issue of what to do if options is nil. The SingleThreaded option from p.opts.SingleThreaded is from the ProgramOptions, not from the CompilerOptions. If p.opts.Config is nil, we should probably just use p.opts.SingleThreaded without a fallback.
Actually, I think the fix should be:
func (p *Program) SingleThreaded() bool {
if p.opts.Config == nil {
return p.opts.SingleThreaded.IsTrue()
}
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
}
This checks if p.opts.Config is nil before calling p.Options(). If it is nil, we just return p.opts.SingleThreaded.IsTrue() without trying to get the default from the options.
Let me verify this is the right fix by looking at the usage of SingleThreaded():
func (p *Program) BindSourceFiles() {
wg := core.NewWorkGroup(p.SingleThreaded())
// ...
}
It's used to determine whether to use parallelism or single-threaded processing. If p.opts.Config is nil, we should probably default to single-threaded mode (return false).
So the fix could be:
func (p *Program) SingleThreaded() bool {
if p.opts.Config == nil {
return true // Default to single-threaded if config is nil
}
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
}
Or just:
func (p *Program) SingleThreaded() bool {
if p.opts.Config == nil {
return p.opts.SingleThreaded.IsTrue()
}
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
}
The second option is better because it preserves the p.opts.SingleThreaded value even when config is nil.
OK, let me implement this fix.
Actually wait, I should double-check by looking at what happens when p.opts.Config is nil in processAllProgramFiles. Because NewProgram calls both processAllProgramFiles(p.opts, p.SingleThreaded()) and then processAllProgramFiles calls opts.Config.CompilerOptions(), which would also panic if opts.Config is nil.
So the fix for SingleThreaded() would only fix the crash in SingleThreaded(), but processAllProgramFiles would still crash.
Let me look at processAllProgramFiles again:
func processAllProgramFiles(
opts ProgramOptions,
singleThreaded bool,
) processedFiles {
compilerOptions := opts.Config.CompilerOptions()
rootFiles := opts.Config.FileNames()
...
}
If opts.Config is nil, both opts.Config.CompilerOptions() and opts.Config.FileNames() would panic.
So the issue is that NewProgram is being called with a ProgramOptions that has a nil Config. And this is causing the crash.
Looking at the call sites:
NewProgram in NewProgram (line 274) - called from CreateProgram():
newProgram = compiler.NewProgram(
compiler.ProgramOptions{
Host: p.host,
Config: commandLine,
UseSourceOfProjectReference: true,
TypingsLocation: typingsLocation,
CreateCheckerPool: createCheckerPool,
},
)
NewProgram in UpdateProgram:
return NewProgram(newOpts), false
For case 2, newOpts is a copy of p.opts, so Config should be the same as the original program's config.
For case 1, commandLine := p.getCommandLineWithTypingsFiles(). If p.CommandLine is nil and there are no typings files, this returns nil.
Actually, looking at the code flow in CreateProgram():
commandLine := p.getCommandLineWithTypingsFiles()
if p.dirtyFilePath != "" && p.Program != nil && p.Program.CommandLine() == commandLine {
// UpdateProgram path
} else {
// NewProgram path
newProgram = compiler.NewProgram(
compiler.ProgramOptions{
Host: p.host,
Config: commandLine,
// ...
},
)
}
So if commandLine is nil and we're in the else branch, we call NewProgram with Config: nil.
When would commandLine be nil? When getCommandLineWithTypingsFiles() returns nil. This happens when:
len(p.typingsFiles) == 0 and p.CommandLine is nil, OR
typeAcquisition == nil || !typeAcquisition.Enable.IsTrue() and p.CommandLine is nil
So p.CommandLine could be nil for some projects.
OK, so the root cause is that NewProgram is being called with Config: nil, which causes the crash. The fix should handle this case.
Now, the question is: should we fix SingleThreaded() to handle nil config, or should we fix NewProgram() to not be called with nil config, or should we fix getCommandLineWithTypingsFiles() to never return nil?
I think the best fix is to make SingleThreaded() handle nil config gracefully. Additionally, we might want to add defensive checks in other places.
But actually, looking at the stack trace again:
internal/compiler.(*Program).SingleThreaded()
internal/compiler/program.go:416
internal/compiler.NewProgram()
internal/compiler/program.go:274
The crash is in SingleThreaded() at line 416. So the immediate fix should be in SingleThreaded().
Let me implement the fix:
func (p *Program) SingleThreaded() bool {
if p.opts.Config == nil {
return p.opts.SingleThreaded.IsTrue()
}
return p.opts.SingleThreaded.DefaultIfUnknown(p.Options().SingleThreaded).IsTrue()
}
But wait, this only fixes SingleThreaded(). After that, processAllProgramFiles would still be called with opts.Config being nil, which would crash.
Actually, looking at NewProgram:
func NewProgram(opts ProgramOptions) *Program {
p := &Program{opts: opts}
if p.opts.Tracing != nil {
defer p.opts.Tracing.Push(tracing.PhaseProgram, "createProgram", map[string]any{"configFilePath": opts.Config.CompilerOptions().ConfigFilePath}, true)()
}
p.processedFiles = processAllProgramFiles(p.opts, p.SingleThreaded())
p.initCheckerPool()
p.verifyCompilerOptions()
return p
}
Line 270-271 has a defer that accesses opts.Config.CompilerOptions().ConfigFilePath. If opts.Config is nil, this would also crash.
So there are multiple places where opts.Config could be nil and cause a crash.
The proper fix would be to either:
- Ensure
NewProgram is never called with nil Config
- Add nil checks everywhere in
NewProgram where opts.Config is accessed
I think option 1 is the right approach. Let me look at where NewProgram is called with nil Config and fix the caller.
Actually, looking at the issue description:
No new interfaces are introduced.
This suggests the fix should be in the existing code, not by adding new interfaces.
Let me look at what could be done:
- Fix
getCommandLineWithTypingsFiles() to not return nil when p.CommandLine is nil
- Fix
NewProgram to handle nil Config
- Fix
CreateProgram to not call NewProgram with nil Config
I think the cleanest fix is to fix SingleThreaded() to handle nil config. This way, even if NewProgram is called with nil config, it won't crash in SingleThreaded(). But we'd also need to fix the other places in NewProgram and processAllProgramFiles that access opts.Config.
Actually, looking at this more carefully, I think the issue might be that p.CommandLine is being set to nil somewhere, or there's a race condition.
Wait, looking at the project kinds again:
KindInferred projects have their CommandLine set in NewInferredProject
KindConfigured projects get their CommandLine set elsewhere
For KindConfigured projects, p.CommandLine is set when the config file is parsed. If the config file can't be parsed, p.CommandLine might be nil.
OK let me just look at what the fix should be. The issue is that (*Program).SingleThreaded() crashes when p.opts.Config is nil. The fix is to add a nil check.
Let me also check if there are other methods that might have the same issue.
Actually, let me look at what happens after the fix. If we fix SingleThreaded() to not crash when config is nil, then processAllProgramFiles would still crash because it accesses opts.Config.CompilerOptions() and opts.Config.FileNames().
So the real fix should be to ensure that NewProgram is never called with nil Config, or to handle nil Config gracefully throughout.
Let me look at the issue from the caller's perspective. In CreateProgram:
commandLine := p.getCommandLineWithTypingsFiles()
if p.dirtyFilePath != "" && p.Program != nil && p.Program.CommandLine() == commandLine {
newProgram, programCloned = p.Program.UpdateProgram(...)
} else {
newProgram = compiler.NewProgram(
compiler.ProgramOptions{
Host: p.host,
Config: commandLine,
...
},
)
}
If commandLine is nil and we're in the else branch, we call NewProgram with nil config.
The fix could be:
- Make
getCommandLineWithTypingsFiles() return a non-nil commandLine even when p.CommandLine is nil
- Add a nil check in
CreateProgram before calling NewProgram
- Handle nil config in
NewProgram
I think the best approach is to fix SingleThreaded() to handle nil config. This is the minimal fix for the specific crash reported. But we should also consider whether processAllProgramFiles should handle nil config.
Actually, wait. Let me re-read the processAllProgramFiles function:
func processAllProgramFiles(
opts ProgramOptions,
singleThreaded bool,
) processedFiles {
compilerOptions := opts.Config.CompilerOptions()
rootFiles := opts.Config.FileNames()
...
}
If opts.Config is nil, this would crash. So even if we fix SingleThreaded(), processAllProgramFiles would still crash.
But wait, if NewProgram crashes in processAllProgramFiles, the stack trace would show:
internal/compiler.NewProgram()
internal/compiler/program.go:274
internal/compiler/processAllProgramFiles()
internal/compiler/fileloader.go:122
But the stack trace shows:
internal/compiler.(*Program).SingleThreaded()
internal/compiler/program.go:416
internal/compiler.NewProgram()
internal/compiler/program.go:274
This means SingleThreaded() crashes before processAllProgramFiles is called. The p.processedFiles = processAllProgramFiles(p.opts, p.SingleThreaded()) line evaluates p.SingleThreaded() first (as an argument to processAllProgramFiles), and that's where the crash happens.
So the fix should be in SingleThreaded(). But we also need to handle the case where processAllProgramFiles is called with nil config.
Actually, I think the issue is that NewProgram shouldn't be called with nil config. Let me look at the call sites and fix them.
Looking at CreateProgram:
commandLine := p.getCommandLineWithTypingsFiles()
if p.dirtyFilePath != "" && p.Program != nil && p.Program.CommandLine() == commandLine {
// UpdateProgram path
} else {
// NewProgram path - crashes if commandLine is nil
newProgram = compiler.NewProgram(...)
}
The UpdateProgram path also checks p.Program.CommandLine() == commandLine, which would also crash if commandLine is nil (because p.Program.CommandLine() might return nil).
Let me look at Program.CommandLine():
<tool_call>
<function=bash_terminal>
<parameter=command>
grep -n "func.*CommandLine" /typescript-go/internal/compiler/program.go | head -10
</tool_call>