Skip to content

Commit 83cbefb

Browse files
authored
fix: #775 node_modules not ignored on Windows (#802)
1 parent 22eb695 commit 83cbefb

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { relative, isAbsolute } from 'path';
2+
3+
function isInsideAnotherPath(parent: string, directory: string): boolean {
4+
const relativePart = relative(parent, directory);
5+
// Tested folder is above parent.
6+
if (relativePart.startsWith('..')) {
7+
return false;
8+
}
9+
// Tested folder is the same as parent.
10+
if (relativePart.length === 0) {
11+
return false;
12+
}
13+
// Tested directory has nothing in common with parent.
14+
if (isAbsolute(relativePart)) {
15+
return false;
16+
}
17+
// Last option, must be subfolder.
18+
return true;
19+
}
20+
21+
export { isInsideAnotherPath };

src/watch/inclusive-node-watch-file-system.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extname } from 'path';
1+
import { extname, relative, isAbsolute } from 'path';
22

33
import type { FSWatcher } from 'chokidar';
44
import chokidar from 'chokidar';
@@ -8,6 +8,7 @@ import type { Compiler } from 'webpack';
88
import { clearFilesChange, updateFilesChange } from '../files-change';
99
import { getInfrastructureLogger } from '../infrastructure-logger';
1010
import type { ForkTsCheckerWebpackPluginState } from '../plugin-state';
11+
import { isInsideAnotherPath } from '../utils/path/is-inside-another-path';
1112

1213
import type { WatchFileSystem } from './watch-file-system';
1314

@@ -30,7 +31,7 @@ function createIsIgnored(
3031
}
3132
});
3233
ignoredFunctions.push((path: string) =>
33-
excluded.some((excludedPath) => path.startsWith(excludedPath))
34+
excluded.some((excludedPath) => isInsideAnotherPath(excludedPath, path))
3435
);
3536
ignoredFunctions.push((path: string) =>
3637
BUILTIN_IGNORED_DIRS.some(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path';
2+
3+
jest.mock('path', () => jest.requireActual('path').posix);
4+
5+
const unixTests: [string, string, boolean][] = [
6+
// Identical
7+
['/foo', '/foo', false],
8+
// Nothing in common
9+
['/foo', '/bar', false],
10+
// subfolder
11+
['/foo', '/foo/bar', true],
12+
// parallel
13+
['/foo', '/foo/../bar', false],
14+
// relative subfolder
15+
['/foo', '/foo/./bar', true],
16+
];
17+
18+
describe('Properly detects ignored sub-folders on Unix', () => {
19+
it('should work on Unix', () => {
20+
unixTests.forEach(([parent, testedPath, expectedResult]) => {
21+
const result = isInsideAnotherPath(parent, testedPath);
22+
expect(result).toEqual(expectedResult);
23+
});
24+
});
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path';
2+
3+
jest.mock('path', () => jest.requireActual('path').win32);
4+
5+
const windowsTests: [string, string, boolean][] = [
6+
// subfolder
7+
['C:\\Foo', 'C:\\Foo\\Bar', true],
8+
// Nothing in common
9+
['C:\\Foo', 'C:\\Bar', false],
10+
// Wrong drive.
11+
['C:\\Foo', 'D:\\Foo\\Bar', false],
12+
];
13+
14+
describe('Properly detects ignored sub-folders on Windows', () => {
15+
it('should work on Windows', () => {
16+
windowsTests.forEach(([parent, testedPath, expectedResult]) => {
17+
const result = isInsideAnotherPath(parent, testedPath);
18+
expect(result).toEqual(expectedResult);
19+
});
20+
});
21+
});

0 commit comments

Comments
 (0)