Skip to content

Commit 8ec6dab

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 688b635 commit 8ec6dab

10 files changed

+546
-416
lines changed

src/compiler/resolutionCache.ts

Lines changed: 15 additions & 7 deletions
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

Lines changed: 87 additions & 60 deletions
Large diffs are not rendered by default.

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

Lines changed: 72 additions & 61 deletions
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

Lines changed: 87 additions & 59 deletions
Large diffs are not rendered by default.

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

Lines changed: 72 additions & 60 deletions
Large diffs are not rendered by default.

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

Lines changed: 87 additions & 60 deletions
Large diffs are not rendered by default.

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

Lines changed: 72 additions & 61 deletions
Large diffs are not rendered by default.

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,27 +1425,24 @@ DirectoryWatcher:: Triggered with /user/username/projects/myproject/src/external
14251425
Scheduled: /user/username/projects/myproject/tsconfig.jsonFailedLookupInvalidation
14261426
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
14271427
Running: /user/username/projects/myproject/tsconfig.json
1428-
Scheduled: *ensureProjectForOpenFiles*, Cancelled earlier one
14291428
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/externalThing.ts 500 undefined WatchType: Closed Script info
14301429
Starting updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json
14311430
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'.
1432-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
1433-
======== Resolving module 'externalThing' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts'. ========
1434-
Module resolution kind is not specified, using 'Classic'.
1435-
File '/user/username/projects/myproject/src/externalThing.ts' exist - use it as a name resolution result.
1436-
======== Module name 'externalThing' was successfully resolved to '/user/username/projects/myproject/src/externalThing.ts'. ========
1431+
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'.
14371432
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'.
1433+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
14381434
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'.
14391435
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'.
1440-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
1441-
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'.
1436+
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'.
1437+
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'.
14421438
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'.
1439+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
14431440
Finishing updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json Version: 10 structureChanged: true structureIsReused:: SafeModules Elapsed:: *ms
14441441
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1445-
Files (14)
1442+
Files (15)
14461443
/a/lib/lib.d.ts
14471444
/user/username/projects/myproject/src/filePresent.ts
1448-
/user/username/projects/myproject/src/externalThing.ts
1445+
/user/username/projects/myproject/src/externalThing.d.ts
14491446
/user/username/projects/myproject/src/externalThingNotPresent.ts
14501447
/user/username/projects/myproject/src/anotherFileReusingResolution.ts
14511448
/user/username/projects/myproject/src/types.ts
@@ -1457,6 +1454,7 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14571454
/user/username/projects/myproject/src/globalMain.ts
14581455
/user/username/projects/myproject/src/newFile.ts
14591456
/user/username/projects/myproject/src/main.ts
1457+
/user/username/projects/myproject/src/externalThing.ts
14601458

14611459

14621460
../../../../a/lib/lib.d.ts
@@ -1466,10 +1464,9 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14661464
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14671465
Imported via "./filePresent" from file 'src/main.ts'
14681466
Imported via "./filePresent" from file 'src/main.ts'
1469-
src/externalThing.ts
1467+
src/externalThing.d.ts
14701468
Imported via "externalThing" from file 'src/anotherFileReusingResolution.ts'
14711469
Imported via "externalThing" from file 'src/main.ts'
1472-
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14731470
src/externalThingNotPresent.ts
14741471
Imported via "externalThingNotPresent" from file 'src/anotherFileReusingResolution.ts'
14751472
Imported via "externalThingNotPresent" from file 'src/main.ts'
@@ -1501,12 +1498,14 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
15011498
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
15021499
src/main.ts
15031500
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
1501+
src/externalThing.ts
1502+
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
15041503

15051504
-----------------------------------------------
15061505
Running: *ensureProjectForOpenFiles*
15071506
Before ensureProjectForOpenFiles:
15081507
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1509-
Files (14)
1508+
Files (15)
15101509

15111510
-----------------------------------------------
15121511
Open files:
@@ -1516,7 +1515,7 @@ Open files:
15161515
Projects: /user/username/projects/myproject/tsconfig.json
15171516
After ensureProjectForOpenFiles:
15181517
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1519-
Files (14)
1518+
Files (15)
15201519

15211520
-----------------------------------------------
15221521
Open files:
@@ -1542,8 +1541,8 @@ interface Array<T> { length: number; [n: number]: T; }
15421541
{"fileName":"/user/username/projects/myproject/src/filePresent.ts","version":"11598859296-export function something() { return 10; }"}
15431542
export function something() { return 10; }
15441543

1545-
{"fileName":"/user/username/projects/myproject/src/externalThing.ts","version":"5618215488-export function externalThing1() { return 10; }"}
1546-
export function externalThing1() { return 10; }
1544+
{"fileName":"/user/username/projects/myproject/src/externalThing.d.ts","version":"5686005290-export function externalThing1(): number;"}
1545+
export function externalThing1(): number;
15471546

15481547
{"fileName":"/user/username/projects/myproject/src/externalThingNotPresent.ts","version":"5318862050-export function externalThing2() { return 20; }"}
15491548
export function externalThing2() { return 20; }
@@ -1596,3 +1595,6 @@ something();
15961595
import { externalThing1 } from "externalThing";
15971596
import { externalThing2 } from "externalThingNotPresent";
15981597

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

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,27 +1425,24 @@ DirectoryWatcher:: Triggered with /user/username/projects/myproject/src/external
14251425
Scheduled: /user/username/projects/myproject/tsconfig.jsonFailedLookupInvalidation
14261426
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
14271427
Running: /user/username/projects/myproject/tsconfig.json
1428-
Scheduled: *ensureProjectForOpenFiles*, Cancelled earlier one
14291428
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/externalThing.ts 500 undefined WatchType: Closed Script info
14301429
Starting updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json
14311430
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'.
1432-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
1433-
======== Resolving module 'externalThing' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts'. ========
1434-
Module resolution kind is not specified, using 'Classic'.
1435-
File '/user/username/projects/myproject/src/externalThing.ts' exist - use it as a name resolution result.
1436-
======== Module name 'externalThing' was successfully resolved to '/user/username/projects/myproject/src/externalThing.ts'. ========
1431+
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'.
14371432
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'.
1433+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts' of old program, it was not resolved.
14381434
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'.
14391435
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'.
1440-
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
1441-
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'.
1436+
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'.
1437+
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'.
14421438
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'.
1439+
Reusing resolution of module './fileNotFound' from '/user/username/projects/myproject/src/main.ts' of old program, it was not resolved.
14431440
Finishing updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json Version: 10 structureChanged: true structureIsReused:: SafeModules Elapsed:: *ms
14441441
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1445-
Files (14)
1442+
Files (15)
14461443
/a/lib/lib.d.ts
14471444
/user/username/projects/myproject/src/filePresent.ts
1448-
/user/username/projects/myproject/src/externalThing.ts
1445+
/user/username/projects/myproject/src/externalThing.d.ts
14491446
/user/username/projects/myproject/src/externalThingNotPresent.ts
14501447
/user/username/projects/myproject/src/anotherFileReusingResolution.ts
14511448
/user/username/projects/myproject/src/types.ts
@@ -1457,6 +1454,7 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14571454
/user/username/projects/myproject/src/globalMain.ts
14581455
/user/username/projects/myproject/src/newFile.ts
14591456
/user/username/projects/myproject/src/main.ts
1457+
/user/username/projects/myproject/src/externalThing.ts
14601458

14611459

14621460
../../../../a/lib/lib.d.ts
@@ -1466,10 +1464,9 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
14661464
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14671465
Imported via "./filePresent" from file 'src/main.ts'
14681466
Imported via "./filePresent" from file 'src/main.ts'
1469-
src/externalThing.ts
1467+
src/externalThing.d.ts
14701468
Imported via "externalThing" from file 'src/anotherFileReusingResolution.ts'
14711469
Imported via "externalThing" from file 'src/main.ts'
1472-
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
14731470
src/externalThingNotPresent.ts
14741471
Imported via "externalThingNotPresent" from file 'src/anotherFileReusingResolution.ts'
14751472
Imported via "externalThingNotPresent" from file 'src/main.ts'
@@ -1501,12 +1498,14 @@ Project '/user/username/projects/myproject/tsconfig.json' (Configured)
15011498
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
15021499
src/main.ts
15031500
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
1501+
src/externalThing.ts
1502+
Matched by include pattern 'src/**/*.ts' in 'tsconfig.json'
15041503

15051504
-----------------------------------------------
15061505
Running: *ensureProjectForOpenFiles*
15071506
Before ensureProjectForOpenFiles:
15081507
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1509-
Files (14)
1508+
Files (15)
15101509

15111510
-----------------------------------------------
15121511
Open files:
@@ -1516,7 +1515,7 @@ Open files:
15161515
Projects: /user/username/projects/myproject/tsconfig.json
15171516
After ensureProjectForOpenFiles:
15181517
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
1519-
Files (14)
1518+
Files (15)
15201519

15211520
-----------------------------------------------
15221521
Open files:
@@ -1542,8 +1541,8 @@ interface Array<T> { length: number; [n: number]: T; }
15421541
{"fileName":"/user/username/projects/myproject/src/filePresent.ts","version":"11598859296-export function something() { return 10; }"}
15431542
export function something() { return 10; }
15441543

1545-
{"fileName":"/user/username/projects/myproject/src/externalThing.ts","version":"5618215488-export function externalThing1() { return 10; }"}
1546-
export function externalThing1() { return 10; }
1544+
{"fileName":"/user/username/projects/myproject/src/externalThing.d.ts","version":"5686005290-export function externalThing1(): number;"}
1545+
export function externalThing1(): number;
15471546

15481547
{"fileName":"/user/username/projects/myproject/src/externalThingNotPresent.ts","version":"5318862050-export function externalThing2() { return 20; }"}
15491548
export function externalThing2() { return 20; }
@@ -1596,3 +1595,6 @@ something();
15961595
import { externalThing1 } from "externalThing";
15971596
import { externalThing2 } from "externalThingNotPresent";
15981597

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

0 commit comments

Comments
 (0)