Skip to content

Commit 8061bfa

Browse files
committed
When installing unrelated package inside scoped packages dont invalidate resolutions from everything in the scoped package
1 parent b771d70 commit 8061bfa

File tree

3 files changed

+15
-67
lines changed

3 files changed

+15
-67
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,24 +1889,24 @@ export function pathContainsNodeModules(path: string): boolean {
18891889
*
18901890
* @internal
18911891
*/
1892-
export function parseNodeModuleFromPath(resolved: string): string | undefined {
1892+
export function parseNodeModuleFromPath(resolved: string, isFolder?: boolean): string | undefined {
18931893
const path = normalizePath(resolved);
18941894
const idx = path.lastIndexOf(nodeModulesPathPart);
18951895
if (idx === -1) {
18961896
return undefined;
18971897
}
18981898

18991899
const indexAfterNodeModules = idx + nodeModulesPathPart.length;
1900-
let indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterNodeModules);
1900+
let indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterNodeModules, isFolder);
19011901
if (path.charCodeAt(indexAfterNodeModules) === CharacterCodes.at) {
1902-
indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterPackageName);
1902+
indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterPackageName, isFolder);
19031903
}
19041904
return path.slice(0, indexAfterPackageName);
19051905
}
19061906

1907-
function moveToNextDirectorySeparatorIfAvailable(path: string, prevSeparatorIndex: number): number {
1907+
function moveToNextDirectorySeparatorIfAvailable(path: string, prevSeparatorIndex: number, isFolder: boolean | undefined): number {
19081908
const nextSeparatorIndex = path.indexOf(directorySeparator, prevSeparatorIndex + 1);
1909-
return nextSeparatorIndex === -1 ? prevSeparatorIndex : nextSeparatorIndex;
1909+
return nextSeparatorIndex === -1 ? isFolder ? path.length : prevSeparatorIndex : nextSeparatorIndex;
19101910
}
19111911

19121912
function loadModuleFromFileNoPackageId(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {

src/compiler/resolutionCache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11121112
}
11131113

11141114
function scheduleInvalidateResolutionOfFailedLookupLocation(fileOrDirectoryPath: Path, isCreatingWatchedDirectory: boolean) {
1115+
debugger;
11151116
if (isCreatingWatchedDirectory) {
11161117
// Watching directory is created
11171118
// Invalidate any resolution has failed lookup in this directory
@@ -1152,7 +1153,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11521153
// If the invalidated file is from a node_modules package, invalidate everything else
11531154
// in the package since we might not get notifications for other files in the package.
11541155
// This hardens our logic against unreliable file watchers.
1155-
const packagePath = parseNodeModuleFromPath(fileOrDirectoryPath);
1156+
const packagePath = parseNodeModuleFromPath(fileOrDirectoryPath, /*isFolder*/ true);
11561157
if (packagePath) (startsWithPathChecks ||= new Set()).add(packagePath as Path);
11571158
}
11581159
}

tests/baselines/reference/tscWatch/resolutionCache/scoped-package-installation.js

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,8 @@ export const myappUnrelated2 = 10;
352352
Before running Timeout callback:: count: 2
353353
18: timerToInvalidateFailedLookupResolutions
354354
19: timerToUpdateProgram
355-
After running Timeout callback:: count: 1
356-
20: timerToUpdateProgram
357-
Before running Timeout callback:: count: 1
358-
20: timerToUpdateProgram
355+
After running Timeout callback:: count: 0
356+
Before running Timeout callback:: count: 0
359357
After running Timeout callback:: count: 0
360358
Output::
361359
DirectoryWatcher:: Triggered with /user/username/projects/myproject/node_modules/@myapp/unrelated2 :: WatchInfo: /user/username/projects/myproject/node_modules 1 undefined Failed Lookup Locations
@@ -370,61 +368,10 @@ Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myprojec
370368
DirectoryWatcher:: Triggered with /user/username/projects/myproject/node_modules/@myapp/unrelated2/index.d.ts :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory
371369
Scheduling update
372370
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/node_modules/@myapp/unrelated2/index.d.ts :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory
373-
Scheduling update
374371
Reloading new file names and options
375372
Synchronizing program
376-
[12:00:48 AM] File change detected. Starting incremental compilation...
377-
378-
CreatingProgramWith::
379-
roots: ["/user/username/projects/myproject/lib/app.ts"]
380-
options: {"watch":true,"project":"/user/username/projects/myproject","traceResolution":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
381-
======== Resolving module '@myapp/ts-types' from '/user/username/projects/myproject/lib/app.ts'. ========
382-
Module resolution kind is not specified, using 'Node10'.
383-
Loading module '@myapp/ts-types' from 'node_modules' folder, target file types: TypeScript, Declaration.
384-
Directory '/user/username/projects/myproject/lib/node_modules' does not exist, skipping all lookups in it.
385-
Scoped package detected, looking in 'myapp__ts-types'
386-
File '/user/username/projects/myproject/node_modules/@myapp/ts-types.ts' does not exist.
387-
File '/user/username/projects/myproject/node_modules/@myapp/ts-types.tsx' does not exist.
388-
File '/user/username/projects/myproject/node_modules/@myapp/ts-types.d.ts' does not exist.
389-
Directory '/user/username/projects/myproject/node_modules/@types' does not exist, skipping all lookups in it.
390-
Scoped package detected, looking in 'myapp__ts-types'
391-
Directory '/user/username/projects/node_modules' does not exist, skipping all lookups in it.
392-
Scoped package detected, looking in 'myapp__ts-types'
393-
Directory '/user/username/node_modules' does not exist, skipping all lookups in it.
394-
Scoped package detected, looking in 'myapp__ts-types'
395-
Directory '/user/node_modules' does not exist, skipping all lookups in it.
396-
Scoped package detected, looking in 'myapp__ts-types'
397-
Directory '/node_modules' does not exist, skipping all lookups in it.
398-
Scoped package detected, looking in 'myapp__ts-types'
399-
Loading module '@myapp/ts-types' from 'node_modules' folder, target file types: JavaScript.
400-
Directory '/user/username/projects/myproject/lib/node_modules' does not exist, skipping all lookups in it.
401-
File '/user/username/projects/myproject/node_modules/@myapp/ts-types.js' does not exist.
402-
File '/user/username/projects/myproject/node_modules/@myapp/ts-types.jsx' does not exist.
403-
Directory '/user/username/projects/node_modules' does not exist, skipping all lookups in it.
404-
Directory '/user/username/node_modules' does not exist, skipping all lookups in it.
405-
Directory '/user/node_modules' does not exist, skipping all lookups in it.
406-
Directory '/node_modules' does not exist, skipping all lookups in it.
407-
======== Module name '@myapp/ts-types' was not resolved. ========
408-
lib/app.ts:1:23 - error TS2307: Cannot find module '@myapp/ts-types' or its corresponding type declarations.
409-
410-
1 import { myapp } from "@myapp/ts-types";
411-
   ~~~~~~~~~~~~~~~~~
412-
413-
[12:00:49 AM] Found 1 error. Watching for file changes.
414-
415373

416374

417-
Program root files: ["/user/username/projects/myproject/lib/app.ts"]
418-
Program options: {"watch":true,"project":"/user/username/projects/myproject","traceResolution":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
419-
Program structureReused: SafeModules
420-
Program files::
421-
/a/lib/lib.d.ts
422-
/user/username/projects/myproject/lib/app.ts
423-
424-
Semantic diagnostics in builder refreshed for::
425-
426-
No shapes updated in the builder::
427-
428375
exitCode:: ExitStatus.undefined
429376

430377

@@ -436,12 +383,12 @@ export const myapp = 10;
436383

437384

438385
Before running Timeout callback:: count: 2
439-
23: timerToInvalidateFailedLookupResolutions
440-
24: timerToUpdateProgram
386+
22: timerToInvalidateFailedLookupResolutions
387+
23: timerToUpdateProgram
441388
After running Timeout callback:: count: 1
442-
25: timerToUpdateProgram
389+
24: timerToUpdateProgram
443390
Before running Timeout callback:: count: 1
444-
25: timerToUpdateProgram
391+
24: timerToUpdateProgram
445392
After running Timeout callback:: count: 0
446393
Output::
447394
DirectoryWatcher:: Triggered with /user/username/projects/myproject/node_modules/@myapp/ts-types :: WatchInfo: /user/username/projects/myproject/node_modules 1 undefined Failed Lookup Locations
@@ -459,7 +406,7 @@ Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myprojec
459406
Scheduling update
460407
Reloading new file names and options
461408
Synchronizing program
462-
[[90m12:00:55 AM[0m] File change detected. Starting incremental compilation...
409+
[[90m12:00:52 AM[0m] File change detected. Starting incremental compilation...
463410

464411
CreatingProgramWith::
465412
roots: ["/user/username/projects/myproject/lib/app.ts"]
@@ -481,7 +428,7 @@ Resolving real path for '/user/username/projects/myproject/node_modules/@myapp/t
481428
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@myapp/ts-types/index.d.ts 250 undefined Source file
482429
DirectoryWatcher:: Close:: WatchInfo: /user/username/projects/node_modules 1 undefined Failed Lookup Locations
483430
Elapsed:: *ms DirectoryWatcher:: Close:: WatchInfo: /user/username/projects/node_modules 1 undefined Failed Lookup Locations
484-
[[90m12:00:59 AM[0m] Found 0 errors. Watching for file changes.
431+
[[90m12:00:56 AM[0m] Found 0 errors. Watching for file changes.
485432

486433

487434

0 commit comments

Comments
 (0)