Skip to content

Commit 12ca63c

Browse files
committed
Do not watch failed lookup locations in persistResolutions if resolved to a file
This ensure --watch and editor behaviour matches with what happens without watch and there is no confusion as well as we are not watching unnecessary things
1 parent 933df64 commit 12ca63c

10 files changed

+546
-416
lines changed

src/compiler/resolutionCache.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace ts {
3333
refCount?: number;
3434
// Files that have this resolution using
3535
files?: Path[];
36+
skipWatchingFailedLookup?: true;
3637
}
3738

3839
interface ResolutionWithResolvedFileName {
@@ -600,18 +601,24 @@ namespace ts {
600601
) {
601602
if (resolution.refCount) {
602603
resolution.refCount++;
603-
Debug.assertDefined(resolution.files);
604+
Debug.checkDefined(resolution.files);
604605
}
605606
else {
606607
resolution.refCount = 1;
607608
Debug.assert(length(resolution.files) === 0); // This resolution shouldnt be referenced by any file yet
608-
if (isExternalModuleNameRelative(name)) {
609-
watchFailedLookupLocationOfResolution(resolution);
609+
const resolved = getResolutionWithResolvedFileName(resolution);
610+
// Watch resolution only if not persisting resolutions or if its not resolved to a file
611+
if (!resolutionHost.getCompilationSettings().persistResolutions || !resolved?.resolvedFileName) {
612+
if (isExternalModuleNameRelative(name)) {
613+
watchFailedLookupLocationOfResolution(resolution);
614+
}
615+
else {
616+
nonRelativeExternalModuleResolutions.add(name, resolution);
617+
}
610618
}
611619
else {
612-
nonRelativeExternalModuleResolutions.add(name, resolution);
620+
resolution.skipWatchingFailedLookup = true;
613621
}
614-
const resolved = getResolutionWithResolvedFileName(resolution);
615622
if (resolved && resolved.resolvedFileName) {
616623
resolvedFileToResolution.add(resolutionHost.toPath(resolved.resolvedFileName), resolution);
617624
}
@@ -677,7 +684,7 @@ namespace ts {
677684
filePath: Path,
678685
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
679686
) {
680-
unorderedRemoveItem(Debug.assertDefined(resolution.files), filePath);
687+
unorderedRemoveItem(Debug.checkDefined(resolution.files), filePath);
681688
resolution.refCount!--;
682689
if (resolution.refCount) {
683690
return;
@@ -687,6 +694,7 @@ namespace ts {
687694
resolvedFileToResolution.remove(resolutionHost.toPath(resolved.resolvedFileName), resolution);
688695
}
689696

697+
if (resolution.skipWatchingFailedLookup) return;
690698
if (!unorderedRemoveItem(resolutionsWithFailedLookups, resolution)) {
691699
// If not watching failed lookups, it wont be there in resolutionsWithFailedLookups
692700
return;
@@ -779,7 +787,7 @@ namespace ts {
779787
for (const resolution of resolutions) {
780788
if (resolution.isInvalidated || !canInvalidate(resolution)) continue;
781789
resolution.isInvalidated = invalidated = true;
782-
for (const containingFilePath of Debug.assertDefined(resolution.files)) {
790+
for (const containingFilePath of Debug.checkDefined(resolution.files)) {
783791
(filesWithInvalidatedResolutions || (filesWithInvalidatedResolutions = new Set())).add(containingFilePath);
784792
// When its a file with inferred types resolution, invalidate type reference directive resolution
785793
hasChangedAutomaticTypeDirectiveNames = hasChangedAutomaticTypeDirectiveNames || endsWith(containingFilePath, inferredTypesContainingFile);

tests/baselines/reference/tscWatch/persistResolutions/can-build-after-resolutions-are-cleaned-with-outFile.js

+87-60
Large diffs are not rendered by default.

tests/baselines/reference/tscWatch/persistResolutions/can-build-after-resolutions-are-cleaned.js

+72-61
Large diffs are not rendered by default.

tests/baselines/reference/tscWatch/persistResolutions/can-build-after-resolutions-have-been-saved-in-tsbuildinfo-file-with-outFile.js

+87-59
Large diffs are not rendered by default.

tests/baselines/reference/tscWatch/persistResolutions/can-build-after-resolutions-have-been-saved-in-tsbuildinfo-file.js

+72-60
Large diffs are not rendered by default.

tests/baselines/reference/tscWatch/persistResolutions/saves-resolution-and-uses-it-for-new-program-with-outFile.js

+87-60
Large diffs are not rendered by default.

tests/baselines/reference/tscWatch/persistResolutions/saves-resolution-and-uses-it-for-new-program.js

+72-61
Large diffs are not rendered by default.

tests/baselines/reference/tsserver/persistResolutions/creates-new-resolutions-for-program-if-tsbuildinfo-is-not-present.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -1411,27 +1411,24 @@ DirectoryWatcher:: Triggered with /user/username/projects/myproject/src/external
14111411
Scheduled: /user/username/projects/myproject/tsconfig.jsonFailedLookupInvalidation
14121412
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/src/externalThing.ts :: WatchInfo: /user/username/projects/myproject/src 1 undefined Project: /user/username/projects/myproject/tsconfig.json WatchType: Failed Lookup Locations
14131413
Running: /user/username/projects/myproject/tsconfig.json
1414-
Scheduled: *ensureProjectForOpenFiles*, Cancelled earlier one
14151414
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/externalThing.ts 500 undefined WatchType: Closed Script info
14161415
Starting updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json
14171416
Reusing resolution of module './filePresent' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'.
1418-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
1419-
======== Resolving module 'externalThing' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts'. ========
1420-
Module resolution kind is not specified, using 'Classic'.
1421-
File '/user/username/projects/myproject/src/externalThing.ts' exist - use it as a name resolution result.
1422-
======== Module name 'externalThing' was successfully resolved to '/user/username/projects/myproject/src/externalThing.ts'. ========
1417+
Reusing resolution of module 'externalThing' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThing.d.ts'.
14231418
Reusing resolution of module 'externalThingNotPresent' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThingNotPresent.ts'.
1419+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
14241420
Reusing resolution of module './newFile' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/newFile.ts'.
14251421
Reusing resolution of module './filePresent' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'.
1426-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
1427-
Reusing resolution of module 'externalThing' from '/user/username/projects/myproject/src/main.ts' found in cache from location '/user/username/projects/myproject/src', it was successfully resolved to '/user/username/projects/myproject/src/externalThing.ts'.
1422+
Reusing resolution of module './filePresent' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'.
1423+
Reusing resolution of module 'externalThing' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThing.d.ts'.
14281424
Reusing resolution of module 'externalThingNotPresent' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThingNotPresent.ts'.
1425+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
14291426
Finishing updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json Version: 10 structureChanged: true structureIsReused:: SafeModules Elapsed:: *ms
14301427
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1431-
Files (14)
1428+
Files (15)
14321429
/a/lib/lib.d.ts
14331430
/user/username/projects/myproject/src/filePresent.ts
1434-
/user/username/projects/myproject/src/externalThing.ts
1431+
/user/username/projects/myproject/src/externalThing.d.ts
14351432
/user/username/projects/myproject/src/externalThingNotPresent.ts
14361433
/user/username/projects/myproject/src/anotherFileReusingResolution.ts
14371434
/user/username/projects/myproject/src/types.ts
@@ -1443,6 +1440,7 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14431440
/user/username/projects/myproject/src/globalMain.ts
14441441
/user/username/projects/myproject/src/newFile.ts
14451442
/user/username/projects/myproject/src/main.ts
1443+
/user/username/projects/myproject/src/externalThing.ts
14461444

14471445

14481446
../../../../a/lib/lib.d.ts
@@ -1452,10 +1450,9 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14521450
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14531451
Imported via "./filePresent" from file 'src/main.ts'
14541452
Imported via "./filePresent" from file 'src/main.ts'
1455-
src/externalThing.ts
1453+
src/externalThing.d.ts
14561454
Imported via "externalThing" from file 'src/anotherFileReusingResolution.ts'
14571455
Imported via "externalThing" from file 'src/main.ts'
1458-
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14591456
src/externalThingNotPresent.ts
14601457
Imported via "externalThingNotPresent" from file 'src/anotherFileReusingResolution.ts'
14611458
Imported via "externalThingNotPresent" from file 'src/main.ts'
@@ -1487,12 +1484,14 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14871484
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14881485
src/main.ts
14891486
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
1487+
src/externalThing.ts
1488+
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14901489

14911490
-----------------------------------------------
14921491
Running: *ensureProjectForOpenFiles*
14931492
Before ensureProjectForOpenFiles:
14941493
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1495-
Files (14)
1494+
Files (15)
14961495

14971496
-----------------------------------------------
14981497
Open files:
@@ -1502,7 +1501,7 @@ Open files:
15021501
Projects: /user/username/projects/myproject/tsconfig.json
15031502
After ensureProjectForOpenFiles:
15041503
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1505-
Files (14)
1504+
Files (15)
15061505

15071506
-----------------------------------------------
15081507
Open files:
@@ -1528,8 +1527,8 @@ interface Array<T> { length: number; [n: number]: T; }
15281527
{"fileName":"/user/username/projects/myproject/src/filePresent.ts","version":"11598859296-export function something() { return 10; }"}
15291528
export function something() { return 10; }
15301529

1531-
{"fileName":"/user/username/projects/myproject/src/externalThing.ts","version":"5618215488-export function externalThing1() { return 10; }"}
1532-
export function externalThing1() { return 10; }
1530+
{"fileName":"/user/username/projects/myproject/src/externalThing.d.ts","version":"5686005290-export function externalThing1(): number;"}
1531+
export function externalThing1(): number;
15331532

15341533
{"fileName":"/user/username/projects/myproject/src/externalThingNotPresent.ts","version":"5318862050-export function externalThing2() { return 20; }"}
15351534
export function externalThing2() { return 20; }
@@ -1582,3 +1581,6 @@ something();
15821581
import { externalThing1 } from "externalThing";
15831582
import { externalThing2 } from "externalThingNotPresent";
15841583

1584+
{"fileName":"/user/username/projects/myproject/src/externalThing.ts","version":"5618215488-export function externalThing1() { return 10; }"}
1585+
export function externalThing1() { return 10; }
1586+

tests/baselines/reference/tsserver/persistResolutions/creates-new-resolutions-for-program-if-tsbuildinfo-is-present-but-program-is-not-persisted.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -1411,27 +1411,24 @@ DirectoryWatcher:: Triggered with /user/username/projects/myproject/src/external
14111411
Scheduled: /user/username/projects/myproject/tsconfig.jsonFailedLookupInvalidation
14121412
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/src/externalThing.ts :: WatchInfo: /user/username/projects/myproject/src 1 undefined Project: /user/username/projects/myproject/tsconfig.json WatchType: Failed Lookup Locations
14131413
Running: /user/username/projects/myproject/tsconfig.json
1414-
Scheduled: *ensureProjectForOpenFiles*, Cancelled earlier one
14151414
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/externalThing.ts 500 undefined WatchType: Closed Script info
14161415
Starting updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json
14171416
Reusing resolution of module './filePresent' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'.
1418-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
1419-
======== Resolving module 'externalThing' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts'. ========
1420-
Module resolution kind is not specified, using 'Classic'.
1421-
File '/user/username/projects/myproject/src/externalThing.ts' exist - use it as a name resolution result.
1422-
======== Module name 'externalThing' was successfully resolved to '/user/username/projects/myproject/src/externalThing.ts'. ========
1417+
Reusing resolution of module 'externalThing' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThing.d.ts'.
14231418
Reusing resolution of module 'externalThingNotPresent' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThingNotPresent.ts'.
1419+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
14241420
Reusing resolution of module './newFile' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/newFile.ts'.
14251421
Reusing resolution of module './filePresent' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'.
1426-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
1427-
Reusing resolution of module 'externalThing' from '/user/username/projects/myproject/src/main.ts' found in cache from location '/user/username/projects/myproject/src', it was successfully resolved to '/user/username/projects/myproject/src/externalThing.ts'.
1422+
Reusing resolution of module './filePresent' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'.
1423+
Reusing resolution of module 'externalThing' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThing.d.ts'.
14281424
Reusing resolution of module 'externalThingNotPresent' from '/user/username/projects/myproject/src/main.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/externalThingNotPresent.ts'.
1425+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
14291426
Finishing updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json Version: 10 structureChanged: true structureIsReused:: SafeModules Elapsed:: *ms
14301427
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1431-
Files (14)
1428+
Files (15)
14321429
/a/lib/lib.d.ts
14331430
/user/username/projects/myproject/src/filePresent.ts
1434-
/user/username/projects/myproject/src/externalThing.ts
1431+
/user/username/projects/myproject/src/externalThing.d.ts
14351432
/user/username/projects/myproject/src/externalThingNotPresent.ts
14361433
/user/username/projects/myproject/src/anotherFileReusingResolution.ts
14371434
/user/username/projects/myproject/src/types.ts
@@ -1443,6 +1440,7 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14431440
/user/username/projects/myproject/src/globalMain.ts
14441441
/user/username/projects/myproject/src/newFile.ts
14451442
/user/username/projects/myproject/src/main.ts
1443+
/user/username/projects/myproject/src/externalThing.ts
14461444

14471445

14481446
../../../../a/lib/lib.d.ts
@@ -1452,10 +1450,9 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14521450
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14531451
Imported via "./filePresent" from file 'src/main.ts'
14541452
Imported via "./filePresent" from file 'src/main.ts'
1455-
src/externalThing.ts
1453+
src/externalThing.d.ts
14561454
Imported via "externalThing" from file 'src/anotherFileReusingResolution.ts'
14571455
Imported via "externalThing" from file 'src/main.ts'
1458-
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14591456
src/externalThingNotPresent.ts
14601457
Imported via "externalThingNotPresent" from file 'src/anotherFileReusingResolution.ts'
14611458
Imported via "externalThingNotPresent" from file 'src/main.ts'
@@ -1487,12 +1484,14 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14871484
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14881485
src/main.ts
14891486
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
1487+
src/externalThing.ts
1488+
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14901489

14911490
-----------------------------------------------
14921491
Running: *ensureProjectForOpenFiles*
14931492
Before ensureProjectForOpenFiles:
14941493
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1495-
Files (14)
1494+
Files (15)
14961495

14971496
-----------------------------------------------
14981497
Open files:
@@ -1502,7 +1501,7 @@ Open files:
15021501
Projects: /user/username/projects/myproject/tsconfig.json
15031502
After ensureProjectForOpenFiles:
15041503
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1505-
Files (14)
1504+
Files (15)
15061505

15071506
-----------------------------------------------
15081507
Open files:
@@ -1528,8 +1527,8 @@ interface Array<T> { length: number; [n: number]: T; }
15281527
{"fileName":"/user/username/projects/myproject/src/filePresent.ts","version":"11598859296-export function something() { return 10; }"}
15291528
export function something() { return 10; }
15301529

1531-
{"fileName":"/user/username/projects/myproject/src/externalThing.ts","version":"5618215488-export function externalThing1() { return 10; }"}
1532-
export function externalThing1() { return 10; }
1530+
{"fileName":"/user/username/projects/myproject/src/externalThing.d.ts","version":"5686005290-export function externalThing1(): number;"}
1531+
export function externalThing1(): number;
15331532

15341533
{"fileName":"/user/username/projects/myproject/src/externalThingNotPresent.ts","version":"5318862050-export function externalThing2() { return 20; }"}
15351534
export function externalThing2() { return 20; }
@@ -1582,3 +1581,6 @@ something();
15821581
import { externalThing1 } from "externalThing";
15831582
import { externalThing2 } from "externalThingNotPresent";
15841583

1584+
{"fileName":"/user/username/projects/myproject/src/externalThing.ts","version":"5618215488-export function externalThing1() { return 10; }"}
1585+
export function externalThing1() { return 10; }
1586+

0 commit comments

Comments
 (0)