Skip to content

Commit 4414a06

Browse files
authored
Fix default configured project crash with disableReferecedProjectLoad (microsoft#2639)
1 parent 269986e commit 4414a06

2 files changed

Lines changed: 130 additions & 1 deletion

File tree

internal/project/projectcollection.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func (c *ProjectCollection) findDefaultConfiguredProjectWorker(path tspath.Path,
185185
if project.CommandLine == nil {
186186
return nil
187187
}
188-
return core.Map(project.CommandLine.ResolvedProjectReferencePaths(), func(configFileName string) *Project {
188+
// A referenced project may not be loaded if `disableReferencedProjectLoad` is true.
189+
return core.MapNonNil(project.CommandLine.ResolvedProjectReferencePaths(), func(configFileName string) *Project {
189190
return c.configuredProjects[c.toPath(configFileName)]
190191
})
191192
},
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package project_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/microsoft/typescript-go/internal/bundled"
8+
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
9+
"github.com/microsoft/typescript-go/internal/testutil/projecttestutil"
10+
)
11+
12+
func TestProjectCollectionDefaultProject(t *testing.T) {
13+
t.Parallel()
14+
15+
if !bundled.Embedded {
16+
t.Skip("bundled files are not embedded")
17+
}
18+
19+
// Project 1 references project 2, which does not have open files.
20+
// File project1/dist/index.d.ts does not belong to any tsconfig.json, but is included in programs for
21+
// projects 3 and 4 via project 1's output.
22+
// When looking for a default project for project1/dist/index.d.ts,
23+
// we should not try to unconditionally access project 2,
24+
// which isn't loaded because of `disableReferencedProjectLoad`.
25+
files := map[string]any{
26+
"/project1/tsconfig.json": `{
27+
"extends": "../tsconfig.json",
28+
"files": [],
29+
"include": ["src/**/*"],
30+
"references": [
31+
{
32+
"path": "../project2"
33+
}
34+
],
35+
"compilerOptions": {
36+
"composite": true,
37+
"outDir": "./dist",
38+
"rootDir": "./src",
39+
}
40+
}`,
41+
"/project1/src/index.ts": `export const foo = 42;
42+
export type Bar = { a: string };`,
43+
"/project1/dist/index.d.ts": `export declare const foo = 42;
44+
export type Bar = {
45+
a: string;
46+
};`,
47+
"/project2/tsconfig.json": `{
48+
"extends": "../tsconfig.json",
49+
"files": [],
50+
"include": ["src/**/*"],
51+
"compilerOptions": {
52+
"composite": true,
53+
"outDir": "./dist",
54+
"rootDir": "./src"
55+
}
56+
}`,
57+
"/project3/tsconfig.json": `{
58+
"extends": "../tsconfig.json",
59+
"files": [],
60+
"include": ["src/**/*"],
61+
"references": [
62+
{
63+
"path": "../project1"
64+
}
65+
],
66+
"compilerOptions": {
67+
"composite": true,
68+
"outDir": "./dist",
69+
"rootDir": "./src",
70+
}
71+
}`,
72+
"/project3/src/index.ts": `import { Bar } from "../../project1/dist/index.js";
73+
declare const b: Bar;
74+
const x: string = b.a;`,
75+
"/project4/tsconfig.json": `{
76+
"extends": "../tsconfig.json",
77+
"files": [],
78+
"include": ["src/**/*"],
79+
"references": [
80+
{
81+
"path": "../project1"
82+
}
83+
],
84+
"compilerOptions": {
85+
"composite": true,
86+
"outDir": "./dist",
87+
"rootDir": "./src",
88+
}
89+
}`,
90+
"/project4/src/index.ts": `import { Bar } from "../../project1/dist/index.js";
91+
declare const b: Bar;
92+
const x: string = b.a;`,
93+
"/tsconfig.json": `{
94+
"compilerOptions": {
95+
"disableReferencedProjectLoad": true,
96+
"disableSolutionSearching": true,
97+
"disableSourceOfProjectReferenceRedirect": true
98+
},
99+
"files": [],
100+
"references": [
101+
{
102+
"path": "./project1"
103+
},
104+
{
105+
"path": "./project2"
106+
},
107+
{
108+
"path": "./project3"
109+
},
110+
{
111+
"path": "./project4"
112+
}
113+
]
114+
}`,
115+
}
116+
uris := []lsproto.DocumentUri{
117+
"file:///project1/dist/index.d.ts",
118+
"file:///project1/src/index.ts",
119+
"file:///project3/src/index.ts",
120+
"file:///project4/src/index.ts",
121+
}
122+
session, _ := projecttestutil.Setup(files)
123+
// Should not crash.
124+
for _, uri := range uris {
125+
content := files[string(uri)[7:]].(string)
126+
session.DidOpenFile(context.Background(), uri, 1, content, lsproto.LanguageKindTypeScript)
127+
}
128+
}

0 commit comments

Comments
 (0)