Skip to content

Commit e9dc290

Browse files
andrewbranchmprobst
authored andcommitted
Set hasAddedOrRemovedSymlinks when discovering an existing file by its link (microsoft#46569)
* Set hasAddedOrRemovedSymlinks when discovering an existing file by its link * Make it optional
1 parent ea3373f commit e9dc290

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,8 +1674,8 @@ namespace ts {
16741674
const typesReferenceDirectives = map(newSourceFile.typeReferenceDirectives, ref => toFileNameLowerCase(ref.fileName));
16751675
const typeReferenceResolutions = resolveTypeReferenceDirectiveNamesWorker(typesReferenceDirectives, newSourceFile);
16761676
// ensure that types resolutions are still correct
1677-
const typeReferenceEesolutionsChanged = hasChangesInResolutions(typesReferenceDirectives, typeReferenceResolutions, oldSourceFile.resolvedTypeReferenceDirectiveNames, oldSourceFile, typeDirectiveIsEqualTo);
1678-
if (typeReferenceEesolutionsChanged) {
1677+
const typeReferenceResolutionsChanged = hasChangesInResolutions(typesReferenceDirectives, typeReferenceResolutions, oldSourceFile.resolvedTypeReferenceDirectiveNames, oldSourceFile, typeDirectiveIsEqualTo);
1678+
if (typeReferenceResolutionsChanged) {
16791679
structureIsReused = StructureIsReused.SafeModules;
16801680
newSourceFile.resolvedTypeReferenceDirectiveNames = zipToModeAwareCache(newSourceFile, typesReferenceDirectives, typeReferenceResolutions);
16811681
}

src/compiler/resolutionCache.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace ts {
6666
getCurrentProgram(): Program | undefined;
6767
fileIsOpen(filePath: Path): boolean;
6868
getCompilerHost?(): CompilerHost | undefined;
69+
onDiscoveredSymlink?(): void;
6970
}
7071

7172
interface DirectoryWatchesOfFailedLookup {
@@ -431,6 +432,9 @@ namespace ts {
431432
else {
432433
resolution = loader(name, containingFile, compilerOptions, resolutionHost.getCompilerHost?.() || resolutionHost, redirectedReference, containingSourceFile);
433434
perDirectoryResolution.set(name, mode, resolution);
435+
if (resolutionHost.onDiscoveredSymlink && resolutionIsSymlink(resolution)) {
436+
resolutionHost.onDiscoveredSymlink();
437+
}
434438
}
435439
resolutionsInFile.set(name, mode, resolution);
436440
watchFailedLookupLocationsOfExternalModuleResolutions(name, resolution, path, getResolutionWithResolvedFileName);
@@ -965,4 +969,11 @@ namespace ts {
965969
return dirPath === rootPath || canWatchDirectory(dirPath);
966970
}
967971
}
972+
973+
function resolutionIsSymlink(resolution: ResolutionWithFailedLookupLocations) {
974+
return !!(
975+
(resolution as ResolvedModuleWithFailedLookupLocations).resolvedModule?.originalPath ||
976+
(resolution as ResolvedTypeReferenceDirectiveWithFailedLookupLocations).resolvedTypeReferenceDirective?.originalPath
977+
);
978+
}
968979
}

src/server/project.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,11 @@ namespace ts.server {
10311031
}
10321032
}
10331033

1034+
/* @internal */
1035+
onDiscoveredSymlink() {
1036+
this.hasAddedOrRemovedSymlinks = true;
1037+
}
1038+
10341039
/**
10351040
* Updates set of files that contribute to this project
10361041
* @returns: true if set of files in the project stays the same and false - otherwise.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// Notable lack of package.json referencing `mylib` as dependency here.
4+
// This is not accurate to the original repro, but I think that's a separate
5+
// bug, which, if fixed, would prevent the later crash.
6+
7+
// @Filename: /tsconfig.json
8+
//// { "compilerOptions": { "module": "commonjs" } }
9+
10+
// @Filename: /packages/mylib/package.json
11+
//// { "name": "mylib", "version": "1.0.0", "main": "index.js" }
12+
13+
// @Filename: /packages/mylib/index.ts
14+
//// export * from "./mySubDir";
15+
16+
// @Filename: /packages/mylib/mySubDir/index.ts
17+
//// export * from "./myClass";
18+
//// export * from "./myClass2";
19+
20+
// @Filename: /packages/mylib/mySubDir/myClass.ts
21+
//// export class MyClass {}
22+
23+
// @Filename: /packages/mylib/mySubDir/myClass2.ts
24+
//// export class MyClass2 {}
25+
26+
// @link: /packages/mylib -> /node_modules/mylib
27+
28+
// @Filename: /src/index.ts
29+
////
30+
//// const a = new MyClass/*1*/();
31+
//// const b = new MyClass2/*2*/();
32+
33+
goTo.marker("1");
34+
format.setOption("newLineCharacter", "\n");
35+
36+
verify.completions({
37+
marker: "1",
38+
includes: [{
39+
name: "MyClass",
40+
source: "../packages/mylib",
41+
sourceDisplay: "../packages/mylib",
42+
hasAction: true,
43+
sortText: completion.SortText.AutoImportSuggestions,
44+
}],
45+
preferences: {
46+
includeCompletionsForModuleExports: true,
47+
includeInsertTextCompletions: true,
48+
allowIncompleteCompletions: true,
49+
}
50+
});
51+
52+
verify.applyCodeActionFromCompletion("1", {
53+
name: "MyClass",
54+
source: "../packages/mylib",
55+
description: `Import 'MyClass' from module "../packages/mylib"`,
56+
data: {
57+
exportName: "MyClass",
58+
fileName: "/packages/mylib/index.ts",
59+
},
60+
preferences: {
61+
includeCompletionsForModuleExports: true,
62+
includeCompletionsWithInsertText: true,
63+
allowIncompleteCompletions: true,
64+
},
65+
newFileContent: `import { MyClass } from "../packages/mylib";
66+
67+
const a = new MyClass();
68+
const b = new MyClass2();`,
69+
});
70+
71+
edit.replaceLine(0, `import { MyClass } from "mylib";`);
72+
73+
verify.completions({
74+
marker: "2",
75+
includes: [{
76+
name: "MyClass2",
77+
source: "mylib",
78+
sourceDisplay: "mylib",
79+
hasAction: true,
80+
sortText: completion.SortText.AutoImportSuggestions,
81+
}],
82+
preferences: {
83+
includeCompletionsForModuleExports: true,
84+
includeInsertTextCompletions: true,
85+
allowIncompleteCompletions: true,
86+
}
87+
});

0 commit comments

Comments
 (0)