Skip to content

Commit 69b0d53

Browse files
Fix file rename crashes when dealing with solution configuration files (#4067)
1 parent 78694bb commit 69b0d53

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGetEditsForFileRenameWithSolutionConfigFile(t *testing.T) {
11+
t.Parallel()
12+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
13+
// The parent-directory solution tsconfig only references the composite child
14+
// project, so when the child file is opened the solution is created as an
15+
// ancestor project without ever building its program (it stays nil). Renaming
16+
// a file in the child project must not crash when iterating that nil-program
17+
// solution project.
18+
const content = `
19+
// @Filename: /tsconfig.json
20+
{
21+
"files": [],
22+
"references": [
23+
{ "path": "./src/tsconfig.json" }
24+
]
25+
}
26+
27+
// @Filename: /src/tsconfig.json
28+
{
29+
"compilerOptions": {
30+
"composite": true
31+
},
32+
"files": ["./a.ts", "./b.ts"]
33+
}
34+
35+
// @Filename: /src/a.ts
36+
import { b } from "./b";
37+
b;
38+
39+
// @Filename: /src/b.ts
40+
export const b = 0;`
41+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
42+
defer done()
43+
f.VerifyWillRenameFilesEdits(t, "/src/b.ts", "/src/c.ts", map[string]string{
44+
"/src/a.ts": `import { b } from "./c";
45+
b;
46+
`,
47+
}, nil /*preferences*/)
48+
}

internal/project/session.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,12 @@ func (s *Session) GetLanguageServicesForDocuments(ctx context.Context, uris []ls
10251025
projects := snapshot.ProjectCollection.Projects()
10261026
services := make([]*ls.LanguageService, 0, len(projects))
10271027
for _, project := range projects {
1028-
services = append(services, ls.NewLanguageService(project.configFilePath, project.GetProgram(), snapshot, activeFile))
1028+
program := project.GetProgram()
1029+
if program == nil {
1030+
continue
1031+
}
1032+
1033+
services = append(services, ls.NewLanguageService(project.configFilePath, program, snapshot, activeFile))
10291034
}
10301035
return services
10311036
}

0 commit comments

Comments
 (0)