Skip to content

Commit 16e0117

Browse files
committed
Do not watch folders like "c:/users/username", "c:/users/username/folderAtRoot"
Fixes microsoft/vscode#51139
1 parent 83c58a4 commit 16e0117

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

src/compiler/resolutionCache.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,32 @@ namespace ts {
349349
return endsWith(dirPath, "/node_modules/@types");
350350
}
351351

352-
function isDirectoryAtleastAtLevelFromFSRoot(dirPath: Path, minLevels: number) {
353-
for (let searchIndex = getRootLength(dirPath); minLevels > 0; minLevels--) {
352+
/**
353+
* Filter out paths like
354+
* "/", "/user", "/user/username", "/user/username/folderAtRoot",
355+
* "c:/", "c:/users", "c:/users/username", "c:/users/username/folderAtRoot", "c:/folderAtRoot"
356+
* @param dirPath
357+
*/
358+
function canWatchDirectory(dirPath: Path) {
359+
const rootLength = getRootLength(dirPath);
360+
if (dirPath.length === rootLength) {
361+
// Ignore "/", "c:/"
362+
return false;
363+
}
364+
365+
const nextDirectorySeparator = dirPath.indexOf(directorySeparator, rootLength);
366+
if (nextDirectorySeparator === -1) {
367+
// ignore "/user", "c:/users" or "c:/folderAtRoot"
368+
return false;
369+
}
370+
371+
if (dirPath.charCodeAt(0) !== CharacterCodes.slash &&
372+
dirPath.substr(rootLength, nextDirectorySeparator).search(/users/i) === -1) {
373+
// Paths like c:/folderAtRoot/subFolder are allowed
374+
return true;
375+
}
376+
377+
for (let searchIndex = nextDirectorySeparator + 1, searchLevels = 2; searchLevels > 0; searchLevels--) {
354378
searchIndex = dirPath.indexOf(directorySeparator, searchIndex) + 1;
355379
if (searchIndex === 0) {
356380
// Folder isnt at expected minimun levels
@@ -360,15 +384,6 @@ namespace ts {
360384
return true;
361385
}
362386

363-
function canWatchDirectory(dirPath: Path) {
364-
return isDirectoryAtleastAtLevelFromFSRoot(dirPath,
365-
// When root is "/" do not watch directories like:
366-
// "/", "/user", "/user/username", "/user/username/folderAtRoot"
367-
// When root is "c:/" do not watch directories like:
368-
// "c:/", "c:/folderAtRoot"
369-
dirPath.charCodeAt(0) === CharacterCodes.slash ? 3 : 1);
370-
}
371-
372387
function filterFSRootDirectoriesToWatch(watchPath: DirectoryOfFailedLookupWatch, dirPath: Path): DirectoryOfFailedLookupWatch {
373388
if (!canWatchDirectory(dirPath)) {
374389
watchPath.ignore = true;

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7501,8 +7501,8 @@ namespace ts.projectSystem {
75017501
});
75027502

75037503
describe("tsserverProjectSystem Watched recursive directories with windows style file system", () => {
7504-
function verifyWatchedDirectories(useProjectAtRoot: boolean) {
7505-
const root = useProjectAtRoot ? "c:/" : "c:/myfolder/allproject/";
7504+
function verifyWatchedDirectories(rootedPath: string, useProjectAtRoot: boolean) {
7505+
const root = useProjectAtRoot ? rootedPath : `${rootedPath}myfolder/allproject/`;
75067506
const configFile: File = {
75077507
path: root + "project/tsconfig.json",
75087508
content: "{}"
@@ -7531,12 +7531,22 @@ namespace ts.projectSystem {
75317531
].concat(useProjectAtRoot ? [] : [root + nodeModulesAtTypes]), /*recursive*/ true);
75327532
}
75337533

7534-
it("When project is in rootFolder", () => {
7535-
verifyWatchedDirectories(/*useProjectAtRoot*/ true);
7534+
function verifyRootedDirectoryWatch(rootedPath: string) {
7535+
it("When project is in rootFolder of style c:/", () => {
7536+
verifyWatchedDirectories(rootedPath, /*useProjectAtRoot*/ true);
7537+
});
7538+
7539+
it("When files at some folder other than root", () => {
7540+
verifyWatchedDirectories(rootedPath, /*useProjectAtRoot*/ false);
7541+
});
7542+
}
7543+
7544+
describe("for rootFolder of style c:/", () => {
7545+
verifyRootedDirectoryWatch("c:/");
75367546
});
75377547

7538-
it("When files at some folder other than root", () => {
7539-
verifyWatchedDirectories(/*useProjectAtRoot*/ false);
7548+
describe("for rootFolder of style c:/users/username", () => {
7549+
verifyRootedDirectoryWatch("c:/users/username/");
75407550
});
75417551
});
75427552

0 commit comments

Comments
 (0)