Skip to content

Commit

Permalink
Do not watch folders like "c:/users/username", "c:/users/username/fol…
Browse files Browse the repository at this point in the history
…derAtRoot"

Fixes microsoft/vscode#51139
  • Loading branch information
sheetalkamat committed Jun 8, 2018
1 parent 3d425c7 commit fb3af65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
37 changes: 26 additions & 11 deletions src/compiler/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,32 @@ namespace ts {
return endsWith(dirPath, "/node_modules/@types");
}

function isDirectoryAtleastAtLevelFromFSRoot(dirPath: Path, minLevels: number) {
for (let searchIndex = getRootLength(dirPath); minLevels > 0; minLevels--) {
/**
* Filter out paths like
* "/", "/user", "/user/username", "/user/username/folderAtRoot",
* "c:/", "c:/users", "c:/users/username", "c:/users/username/folderAtRoot", "c:/folderAtRoot"
* @param dirPath
*/
function canWatchDirectory(dirPath: Path) {
const rootLength = getRootLength(dirPath);
if (dirPath.length === rootLength) {
// Ignore "/", "c:/"
return false;
}

const nextDirectorySeparator = dirPath.indexOf(directorySeparator, rootLength);
if (nextDirectorySeparator === -1) {
// ignore "/user", "c:/users" or "c:/folderAtRoot"
return false;
}

if (dirPath.charCodeAt(0) !== CharacterCodes.slash &&
dirPath.substr(rootLength, nextDirectorySeparator).search(/users/i) === -1) {
// Paths like c:/folderAtRoot/subFolder are allowed
return true;
}

for (let searchIndex = nextDirectorySeparator + 1, searchLevels = 2; searchLevels > 0; searchLevels--) {
searchIndex = dirPath.indexOf(directorySeparator, searchIndex) + 1;
if (searchIndex === 0) {
// Folder isnt at expected minimun levels
Expand All @@ -363,15 +387,6 @@ namespace ts {
return true;
}

function canWatchDirectory(dirPath: Path) {
return isDirectoryAtleastAtLevelFromFSRoot(dirPath,
// When root is "/" do not watch directories like:
// "/", "/user", "/user/username", "/user/username/folderAtRoot"
// When root is "c:/" do not watch directories like:
// "c:/", "c:/folderAtRoot"
dirPath.charCodeAt(0) === CharacterCodes.slash ? 3 : 1);
}

function filterFSRootDirectoriesToWatch(watchPath: DirectoryOfFailedLookupWatch, dirPath: Path): DirectoryOfFailedLookupWatch {
if (!canWatchDirectory(dirPath)) {
watchPath.ignore = true;
Expand Down
22 changes: 16 additions & 6 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7566,8 +7566,8 @@ namespace ts.projectSystem {
});

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

it("When project is in rootFolder", () => {
verifyWatchedDirectories(/*useProjectAtRoot*/ true);
function verifyRootedDirectoryWatch(rootedPath: string) {
it("When project is in rootFolder of style c:/", () => {
verifyWatchedDirectories(rootedPath, /*useProjectAtRoot*/ true);
});

it("When files at some folder other than root", () => {
verifyWatchedDirectories(rootedPath, /*useProjectAtRoot*/ false);
});
}

describe("for rootFolder of style c:/", () => {
verifyRootedDirectoryWatch("c:/");
});

it("When files at some folder other than root", () => {
verifyWatchedDirectories(/*useProjectAtRoot*/ false);
describe("for rootFolder of style c:/users/username", () => {
verifyRootedDirectoryWatch("c:/users/username/");
});
});

Expand Down

0 comments on commit fb3af65

Please sign in to comment.