Skip to content

Commit dad3cbe

Browse files
committed
When getting notification for any file from node_modules package, add anything in the package as invalidated
Fixes #41735
1 parent 7f0bfb1 commit dad3cbe

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ namespace ts {
11501150
}
11511151
const resolvedFromFile = loadModuleFromFile(extensions, candidate, onlyRecordFailures, state);
11521152
if (resolvedFromFile) {
1153-
const packageDirectory = considerPackageJson ? parseNodeModuleFromPath(resolvedFromFile) : undefined;
1153+
const packageDirectory = considerPackageJson ? parseNodeModuleFromPath(resolvedFromFile.path) : undefined;
11541154
const packageInfo = packageDirectory ? getPackageJsonInfo(packageDirectory, /*onlyRecordFailures*/ false, state) : undefined;
11551155
return withPackageId(packageInfo, resolvedFromFile);
11561156
}
@@ -1184,8 +1184,9 @@ namespace ts {
11841184
* For `/node_modules/@types/foo/bar/index.d.ts` this is packageDirectory: "@types/foo"
11851185
* For `/node_modules/foo/bar/index.d.ts` this is packageDirectory: "foo"
11861186
*/
1187-
function parseNodeModuleFromPath(resolved: PathAndExtension): string | undefined {
1188-
const path = normalizePath(resolved.path);
1187+
/* @internal */
1188+
export function parseNodeModuleFromPath(resolved: string): string | undefined {
1189+
const path = normalizePath(resolved);
11891190
const idx = path.lastIndexOf(nodeModulesPathPart);
11901191
if (idx === -1) {
11911192
return undefined;

src/compiler/resolutionCache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,10 @@ namespace ts {
798798
}
799799
// Resolution need to be invalidated if failed lookup location is same as the file or directory getting created
800800
failedLookupChecks.push(fileOrDirectoryPath);
801+
802+
// If this is file from package, invalidate package itself since we might not get notification for all files from the package
803+
const packagePath = parseNodeModuleFromPath(fileOrDirectoryPath);
804+
if (packagePath) startsWithPathChecks.push(packagePath as Path);
801805
}
802806
}
803807
resolutionHost.scheduleInvalidateResolutionsOfFailedLookupLocations();

src/testRunner/unittests/tscWatch/resolutionCache.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,10 @@ declare namespace NodeJS {
520520
sys.ensureFileOrFolder(nodeAtTypes36Base, /*ignoreWatchInvokedWithTriggerAsFileCreate*/ true);
521521
sys.ensureFileOrFolder(nodeAtTypesGlobals, /*ignoreWatchInvokedWithTriggerAsFileCreate*/ true);
522522
},
523-
timeouts: runQueuedTimeoutCallbacks
523+
timeouts: sys => {
524+
sys.runQueuedTimeoutCallbacks(); // update failed lookups
525+
sys.runQueuedTimeoutCallbacks(); // actual program update
526+
},
524527
},
525528
]
526529
});

tests/baselines/reference/tscWatch/resolutionCache/works-when-installing-something-in-node_modules-or-@types-when-there-is-no-notification-from-fs-for-index-file.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,18 +450,19 @@ Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myprojec
450450
DirectoryWatcher:: Triggered with /user/username/projects/myproject/node_modules/@types/node/ts3.6 :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory
451451
Project: /user/username/projects/myproject/tsconfig.json Detected file add/remove of non supported extension: /user/username/projects/myproject/node_modules/@types/node/ts3.6
452452
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/node_modules/@types/node/ts3.6 :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory
453+
Scheduling update
453454
[12:01:15 AM] File change detected. Starting incremental compilation...
454455

455456
Reloading new file names and options
456457
Synchronizing program
457458
CreatingProgramWith::
458459
roots: ["/user/username/projects/myproject/worker.ts"]
459460
options: {"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
460-
[91merror[0m[90m TS2688: [0mCannot find type definition file for 'node'.
461-
The file is in the program because:
462-
Entry point for implicit type library 'node'
463-
464-
[[90m12:01:16 AM[0m] Found 1 error. Watching for file changes.
461+
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types/node/index.d.ts 250 undefined Source file
462+
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types/node/base.d.ts 250 undefined Source file
463+
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types/node/ts3.6/base.d.ts 250 undefined Source file
464+
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types/node/globals.d.ts 250 undefined Source file
465+
[[90m12:01:19 AM[0m] Found 0 errors. Watching for file changes.
465466

466467

467468

@@ -472,8 +473,19 @@ Program files::
472473
/a/lib/lib.d.ts
473474
/user/username/projects/myproject/worker.ts
474475
/user/username/projects/myproject/node_modules/@types/mocha/index.d.ts
476+
/user/username/projects/myproject/node_modules/@types/node/globals.d.ts
477+
/user/username/projects/myproject/node_modules/@types/node/ts3.6/base.d.ts
478+
/user/username/projects/myproject/node_modules/@types/node/base.d.ts
479+
/user/username/projects/myproject/node_modules/@types/node/index.d.ts
475480

476481
Semantic diagnostics in builder refreshed for::
482+
/a/lib/lib.d.ts
483+
/user/username/projects/myproject/worker.ts
484+
/user/username/projects/myproject/node_modules/@types/mocha/index.d.ts
485+
/user/username/projects/myproject/node_modules/@types/node/globals.d.ts
486+
/user/username/projects/myproject/node_modules/@types/node/ts3.6/base.d.ts
487+
/user/username/projects/myproject/node_modules/@types/node/base.d.ts
488+
/user/username/projects/myproject/node_modules/@types/node/index.d.ts
477489

478490
WatchedFiles::
479491
/user/username/projects/myproject/tsconfig.json:
@@ -484,6 +496,14 @@ WatchedFiles::
484496
{"fileName":"/a/lib/lib.d.ts","pollingInterval":250}
485497
/user/username/projects/myproject/node_modules/@types/mocha/index.d.ts:
486498
{"fileName":"/user/username/projects/myproject/node_modules/@types/mocha/index.d.ts","pollingInterval":250}
499+
/user/username/projects/myproject/node_modules/@types/node/index.d.ts:
500+
{"fileName":"/user/username/projects/myproject/node_modules/@types/node/index.d.ts","pollingInterval":250}
501+
/user/username/projects/myproject/node_modules/@types/node/base.d.ts:
502+
{"fileName":"/user/username/projects/myproject/node_modules/@types/node/base.d.ts","pollingInterval":250}
503+
/user/username/projects/myproject/node_modules/@types/node/ts3.6/base.d.ts:
504+
{"fileName":"/user/username/projects/myproject/node_modules/@types/node/ts3.6/base.d.ts","pollingInterval":250}
505+
/user/username/projects/myproject/node_modules/@types/node/globals.d.ts:
506+
{"fileName":"/user/username/projects/myproject/node_modules/@types/node/globals.d.ts","pollingInterval":250}
487507

488508
FsWatches::
489509

@@ -497,3 +517,4 @@ FsWatchesRecursive::
497517

498518
exitCode:: ExitStatus.undefined
499519

520+
//// [/user/username/projects/myproject/worker.js] file written with same contents

0 commit comments

Comments
 (0)