Skip to content

Commit e84a95f

Browse files
authored
Fix find-all-refs crashing in some project references scenarios (microsoft#42025)
* Add failing test * Fix test * Accept baseline
1 parent 49136f7 commit e84a95f

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed

src/harness/client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,11 @@ namespace ts.server {
343343
}));
344344
}
345345

346-
findReferences(_fileName: string, _position: number): ReferencedSymbol[] {
347-
// Not yet implemented.
348-
return [];
346+
findReferences(fileName: string, position: number): ReferencedSymbol[] {
347+
const args = this.createFileLocationRequestArgs(fileName, position);
348+
const request = this.processRequest<protocol.ReferencesRequest>(CommandNames.ReferencesFull, args);
349+
const response = this.processResponse(request);
350+
return response.body;
349351
}
350352

351353
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[] {

src/harness/fourslashImpl.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,9 @@ namespace FourSlash {
11171117
}
11181118

11191119
public verifyBaselineFindAllReferences(...markerNames: string[]) {
1120+
ts.Debug.assert(markerNames.length > 0, "Must pass at least one marker name to `baselineFindAllReferences()`");
11201121
const baseline = markerNames.map(markerName => {
1122+
this.goToMarker(markerName);
11211123
const marker = this.getMarkerByName(markerName);
11221124
const references = this.languageService.findReferences(marker.fileName, marker.position);
11231125
const refsByFile = references
@@ -1200,7 +1202,7 @@ namespace FourSlash {
12001202
}
12011203
}
12021204

1203-
// Necessary to have this function since `findReferences` isn't implemented in `client.ts`
1205+
/** @deprecated - use `verify.baselineFindAllReferences()` instead. */
12041206
public verifyGetReferencesForServerTest(expected: readonly ts.ReferenceEntry[]): void {
12051207
const refs = this.getReferencesAtCaret();
12061208
assert.deepEqual<readonly ts.ReferenceEntry[] | undefined>(refs, expected);

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ namespace ts.server {
568568
}
569569

570570
function addToTodo<TLocation extends DocumentPosition | undefined>(project: Project, location: TLocation, toDo: Push<ProjectAndLocation<TLocation>>, seenProjects: Set<string>): void {
571-
if (addToSeen(seenProjects, project)) toDo.push({ project, location });
571+
if (!project.isOrphan() && addToSeen(seenProjects, project)) toDo.push({ project, location });
572572
}
573573

574574
function addToSeen(seenProjects: Set<string>, project: Project) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// === /b/b.ts ===
2+
// /// <reference path="../a/index.d.ts" />
3+
// new [|A|]/*FIND ALL REFS*/();
4+
5+
[
6+
{
7+
"definition": {
8+
"containerKind": "",
9+
"containerName": "",
10+
"fileName": "/a/index.ts",
11+
"kind": "class",
12+
"name": "class A",
13+
"textSpan": {
14+
"start": 10,
15+
"length": 1
16+
},
17+
"displayParts": [
18+
{
19+
"text": "class",
20+
"kind": "keyword"
21+
},
22+
{
23+
"text": " ",
24+
"kind": "space"
25+
},
26+
{
27+
"text": "A",
28+
"kind": "className"
29+
}
30+
],
31+
"contextSpan": {
32+
"start": 0,
33+
"length": 10
34+
}
35+
},
36+
"references": [
37+
{
38+
"textSpan": {
39+
"start": 45,
40+
"length": 1
41+
},
42+
"fileName": "/b/b.ts",
43+
"isWriteAccess": false,
44+
"isDefinition": false
45+
}
46+
]
47+
}
48+
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /a/package.json
4+
//// {}
5+
6+
// @Filename: /a/tsconfig.json
7+
//// {}
8+
9+
// @Filename: /a/index.ts
10+
//// class A {}
11+
12+
// @Filename: /a/index.d.ts
13+
//// declare class A {
14+
//// }
15+
//// //# sourceMappingURL=index.d.ts.map
16+
17+
// @Filename: /a/index.d.ts.map
18+
//// {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,CAAC;CAAG"}
19+
20+
// @Filename: /b/tsconfig.json
21+
//// {
22+
//// "compilerOptions": { "disableSourceOfProjectReferenceRedirect": true },
23+
//// "references": [{ "path": "../a" }]
24+
//// }
25+
26+
// @Filename: /b/b.ts
27+
//// /// <reference path="../a/index.d.ts" />
28+
//// new A/**/();
29+
30+
// @Filename: /c/package.json
31+
//// { "dependencies": { "a": "*" } }
32+
33+
// @Filename: /c/tsconfig.json
34+
//// { "references" [{ "path": "../a" }] }
35+
36+
// @Filename: /c/index.ts
37+
//// export {};
38+
39+
// @link: /a -> /c/node_modules/a
40+
41+
// Test asserts lack of crash
42+
goTo.file("/c/index.ts"); // Create AutoImportProviderProject that has /a/index.d.ts in it
43+
verify.baselineFindAllReferences("");

0 commit comments

Comments
 (0)