Skip to content

Commit 604beba

Browse files
authored
Merge pull request microsoft#24769 from Microsoft/ignoreWindowsUsersFolder
Do not watch folders like "c:/users/username", "c:/users/username/folderAtRoot"
2 parents 399ae51 + 16e0117 commit 604beba

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
@@ -7564,8 +7564,8 @@ namespace ts.projectSystem {
75647564
});
75657565

75667566
describe("tsserverProjectSystem Watched recursive directories with windows style file system", () => {
7567-
function verifyWatchedDirectories(useProjectAtRoot: boolean) {
7568-
const root = useProjectAtRoot ? "c:/" : "c:/myfolder/allproject/";
7567+
function verifyWatchedDirectories(rootedPath: string, useProjectAtRoot: boolean) {
7568+
const root = useProjectAtRoot ? rootedPath : `${rootedPath}myfolder/allproject/`;
75697569
const configFile: File = {
75707570
path: root + "project/tsconfig.json",
75717571
content: "{}"
@@ -7594,12 +7594,22 @@ namespace ts.projectSystem {
75947594
].concat(useProjectAtRoot ? [] : [root + nodeModulesAtTypes]), /*recursive*/ true);
75957595
}
75967596

7597-
it("When project is in rootFolder", () => {
7598-
verifyWatchedDirectories(/*useProjectAtRoot*/ true);
7597+
function verifyRootedDirectoryWatch(rootedPath: string) {
7598+
it("When project is in rootFolder of style c:/", () => {
7599+
verifyWatchedDirectories(rootedPath, /*useProjectAtRoot*/ true);
7600+
});
7601+
7602+
it("When files at some folder other than root", () => {
7603+
verifyWatchedDirectories(rootedPath, /*useProjectAtRoot*/ false);
7604+
});
7605+
}
7606+
7607+
describe("for rootFolder of style c:/", () => {
7608+
verifyRootedDirectoryWatch("c:/");
75997609
});
76007610

7601-
it("When files at some folder other than root", () => {
7602-
verifyWatchedDirectories(/*useProjectAtRoot*/ false);
7611+
describe("for rootFolder of style c:/users/username", () => {
7612+
verifyRootedDirectoryWatch("c:/users/username/");
76037613
});
76047614
});
76057615

0 commit comments

Comments
 (0)