Skip to content

Commit 6e32417

Browse files
committed
Filter out more paths that should not be watched irrespective of the root
1 parent 2ba4a12 commit 6e32417

File tree

505 files changed

+2843
-8107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

505 files changed

+2843
-8107
lines changed

src/compiler/resolutionCache.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,30 @@ export function removeIgnoredPath(path: Path): Path | undefined {
206206
path;
207207
}
208208

209+
function perceivedOsRootLengthForWatching(pathComponents: Readonly<PathPathComponents>) {
210+
// Ignore "/", "c:/"
211+
if (pathComponents.length <= 1) return 1;
212+
let userCheckIndex = 1;
213+
let isDosStyle = pathComponents[0].search(/[a-zA-Z]:/) === 0;
214+
if (pathComponents[0] !== directorySeparator &&
215+
!isDosStyle && // Non dos style paths
216+
pathComponents[1].search(/[a-zA-Z]\$$/) === 0) { // Dos style nextPart
217+
// ignore "//vda1cs4850/c$/folderAtRoot"
218+
if (pathComponents.length === 2) return 2;
219+
userCheckIndex = 2;
220+
isDosStyle = true;
221+
}
222+
223+
if (isDosStyle &&
224+
!pathComponents[userCheckIndex].match(/^users$/i)) {
225+
// Paths like c:/notUsers
226+
return userCheckIndex;
227+
}
228+
229+
// Paths like: c:/users/username or /home/username
230+
return userCheckIndex + 2;
231+
}
232+
209233
/**
210234
* Filter out paths like
211235
* "/", "/user", "/user/username", "/user/username/folderAtRoot",
@@ -218,29 +242,14 @@ export function canWatchDirectoryOrFile(pathComponents: Readonly<PathPathCompone
218242
// Ignore "/", "c:/"
219243
// ignore "/user", "c:/users" or "c:/folderAtRoot"
220244
if (pathComponents.length <= 2) return false;
221-
let userCheckIndex = 1;
222-
const isNonDirectorySeparatorRoot = pathComponents[0] !== "/";
223-
if (isNonDirectorySeparatorRoot &&
224-
pathComponents[0].search(/[a-zA-Z]:/) !== 0 && // Non dos style paths
225-
pathComponents[1].search(/[a-zA-Z]\$$/) === 0) { // Dos style nextPart
226-
// ignore "//vda1cs4850/c$/folderAtRoot"
227-
if (pathComponents.length === 3) return false;
228-
userCheckIndex = 2;
229-
}
230-
231-
if (isNonDirectorySeparatorRoot &&
232-
!pathComponents[userCheckIndex].match(/^users$/i)) {
233-
// Paths like c:/folderAtRoot/subFolder are allowed
234-
return true;
235-
}
236-
237-
return pathComponents.length > userCheckIndex + 3;
245+
const perceivedOsRootLength = perceivedOsRootLengthForWatching(pathComponents);
246+
return pathComponents.length > perceivedOsRootLength + 1;
238247
}
239248

240249
/** @internal */
241250
export function canWatchAtTypes(atTypes: Path, rootPathComponents: Readonly<PathPathComponents>) {
242251
// Otherwise can watch directory only if we can watch the parent directory of node_modules/@types
243-
return isInRootPathOrCanWatchDirectoryOrFile(getDirectoryPath(atTypes), rootPathComponents);
252+
return canWatchAffectedPackageJsonOrNodeModulesOfAtTypes(getDirectoryPath(atTypes), rootPathComponents);
244253
}
245254

246255
function isInDirectoryPath(dirComponents: Readonly<PathPathComponents>, fileOrDirComponents: Readonly<PathPathComponents>) {
@@ -251,15 +260,19 @@ function isInDirectoryPath(dirComponents: Readonly<PathPathComponents>, fileOrDi
251260
return true;
252261
}
253262

254-
function isInRootPathOrCanWatchDirectoryOrFile(fileOrDirPath: Path, rootPathComponents: Readonly<PathPathComponents>) {
263+
function canWatchAffectedPackageJsonOrNodeModulesOfAtTypes(fileOrDirPath: Path, rootPathComponents: Readonly<PathPathComponents>) {
255264
const fileOrDirPathComponents = getPathComponents(fileOrDirPath);
256-
return isInDirectoryPath(rootPathComponents, fileOrDirPathComponents) ||
257-
canWatchDirectoryOrFile(fileOrDirPathComponents);
265+
const perceivedOsRootLength = perceivedOsRootLengthForWatching(fileOrDirPathComponents);
266+
if (perceivedOsRootLength >= fileOrDirPathComponents.length - 1) return false;
267+
if (isInDirectoryPath(rootPathComponents, fileOrDirPathComponents)) {
268+
return true;
269+
}
270+
return fileOrDirPathComponents.length > perceivedOsRootLength + 1;
258271
}
259272

260273
/** @internal */
261274
export function canWatchAffectingLocation(filePath: Path, rootPathComponents: Readonly<PathPathComponents>) {
262-
return isInRootPathOrCanWatchDirectoryOrFile(filePath, rootPathComponents);
275+
return canWatchAffectedPackageJsonOrNodeModulesOfAtTypes(filePath, rootPathComponents);
263276
}
264277

265278
/** @internal */
@@ -351,6 +364,7 @@ export function getDirectoryToWatchFailedLookupLocationFromTypeRoot(
351364
): Path | undefined {
352365
const typeRootPathComponents = getPathComponents(typeRootPath);
353366
if (isInDirectoryPath(rootPathComponents, typeRootPathComponents)) {
367+
// Because this is called when we are watching typeRoot, we dont need additional check whether typeRoot is not say c:/users/node_modules/@types when root is c:/
354368
return rootPath;
355369
}
356370
typeRoot = isRootedDiskPath(typeRoot) ? normalizePath(typeRoot) : getNormalizedAbsolutePath(typeRoot, getCurrentDirectory());

src/testRunner/unittests/tscWatch/resolutionCache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,25 @@ describe("unittests:: tsc-watch:: resolutionCache:: tsc-watch module resolution
227227
verifyTscWatch({
228228
scenario,
229229
subScenario: "works when module resolution changes to ambient module",
230-
commandLineArgs: ["-w", "/a/b/foo.ts"],
230+
commandLineArgs: ["-w", "/users/username/projects/project/foo.ts"],
231231
sys: () => createWatchedSystem([{
232-
path: "/a/b/foo.ts",
232+
path: "/users/username/projects/project/foo.ts",
233233
content: `import * as fs from "fs";`
234-
}, libFile], { currentDirectory: "/a/b" }),
234+
}, libFile], { currentDirectory: "/users/username/projects/project" }),
235235
edits: [
236236
{
237237
caption: "npm install node types",
238238
edit: sys => {
239239
sys.ensureFileOrFolder({
240-
path: "/a/b/node_modules/@types/node/package.json",
240+
path: "/users/username/projects/project/node_modules/@types/node/package.json",
241241
content: `
242242
{
243243
"main": ""
244244
}
245245
`
246246
});
247247
sys.ensureFileOrFolder({
248-
path: "/a/b/node_modules/@types/node/index.d.ts",
248+
path: "/users/username/projects/project/node_modules/@types/node/index.d.ts",
249249
content: `
250250
declare module "fs" {
251251
export interface Stats {

src/testRunner/unittests/tsserver/resolutionCache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ describe("unittests:: tsserver:: resolutionCache:: tsserverProjectSystem extra r
4848
describe("unittests:: tsserver:: resolutionCache:: tsserverProjectSystem watching @types", () => {
4949
it("works correctly when typings are added or removed", () => {
5050
const f1 = {
51-
path: "/a/b/app.ts",
51+
path: "/users/username/projects/project/app.ts",
5252
content: "let x = 1;"
5353
};
5454
const t1 = {
55-
path: "/a/b/node_modules/@types/lib1/index.d.ts",
55+
path: "/users/username/projects/project/node_modules/@types/lib1/index.d.ts",
5656
content: "export let a: number"
5757
};
5858
const t2 = {
59-
path: "/a/b/node_modules/@types/lib2/index.d.ts",
59+
path: "/users/username/projects/project/node_modules/@types/lib2/index.d.ts",
6060
content: "export let b: number"
6161
};
6262
const tsconfig = {
63-
path: "/a/b/tsconfig.json",
63+
path: "/users/username/projects/project/tsconfig.json",
6464
content: JSON.stringify({
6565
compilerOptions: {},
6666
exclude: ["node_modules"]

tests/baselines/reference/canWatch/canWatchAffectingLocationDos.baseline.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Determines if package.json that was found during module resolution and change in
88

99
| File | canWatchAffectingLocation |
1010
| ----------------------------------------------------------------------------------- | ------------------------- |
11-
| c:/package.json | true |
11+
| c:/package.json | false |
1212
| c:/folderAtRoot/package.json | true |
1313
| c:/folderAtRoot/folder1/package.json | true |
1414
| c:/folderAtRoot/folder1/folder2/package.json | true |
1515
| c:/folderAtRoot/folder1/folder2/folder3/package.json | true |
1616
| c:/folderAtRoot/folder1/folder2/folder3/folder4/package.json | true |
1717
| c:/folderAtRoot/folder1/folder2/folder3/folder4/folder5/package.json | true |
18-
| c:/users/package.json | true |
19-
| c:/users/username/package.json | true |
18+
| c:/users/package.json | false |
19+
| c:/users/username/package.json | false |
2020
| c:/users/username/folderAtRoot/package.json | true |
2121
| c:/users/username/folderAtRoot/folder1/package.json | true |
2222
| c:/users/username/folderAtRoot/folder1/folder2/package.json | true |
@@ -323,8 +323,8 @@ Determines if package.json that was found during module resolution and change in
323323
| c:/folderAtRoot/folder1/folder2/folder3/package.json | true |
324324
| c:/folderAtRoot/folder1/folder2/folder3/folder4/package.json | true |
325325
| c:/folderAtRoot/folder1/folder2/folder3/folder4/folder5/package.json | true |
326-
| c:/users/package.json | true |
327-
| c:/users/username/package.json | true |
326+
| c:/users/package.json | false |
327+
| c:/users/username/package.json | false |
328328
| c:/users/username/folderAtRoot/package.json | true |
329329
| c:/users/username/folderAtRoot/folder1/package.json | true |
330330
| c:/users/username/folderAtRoot/folder1/folder2/package.json | true |
@@ -368,7 +368,7 @@ Determines if package.json that was found during module resolution and change in
368368
| c:/folderAtRoot/folder1/folder2/folder3/folder4/package.json | true |
369369
| c:/folderAtRoot/folder1/folder2/folder3/folder4/folder5/package.json | true |
370370
| c:/users/package.json | false |
371-
| c:/users/username/package.json | true |
371+
| c:/users/username/package.json | false |
372372
| c:/users/username/folderAtRoot/package.json | true |
373373
| c:/users/username/folderAtRoot/folder1/package.json | true |
374374
| c:/users/username/folderAtRoot/folder1/folder2/package.json | true |

0 commit comments

Comments
 (0)