Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/utils/path/is-inside-another-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { relative, isAbsolute } from 'path';

function isInsideAnotherPath(parent: string, directory: string): boolean {
const relativePart = relative(parent, directory);
// Tested folder is above parent.
if (relativePart.startsWith('..')) {
return false;
}
// Tested folder is the same as parent.
if (relativePart.length === 0) {
return false;
}
// Tested directory has nothing in common with parent.
if (isAbsolute(relativePart)) {
return false;
}
// Last option, must be subfolder.
return true;
}

export { isInsideAnotherPath };
5 changes: 3 additions & 2 deletions src/watch/inclusive-node-watch-file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extname } from 'path';
import { extname, relative, isAbsolute } from 'path';

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

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

Expand All @@ -30,7 +31,7 @@ function createIsIgnored(
}
});
ignoredFunctions.push((path: string) =>
excluded.some((excludedPath) => path.startsWith(excludedPath))
excluded.some((excludedPath) => isInsideAnotherPath(excludedPath, path))
);
ignoredFunctions.push((path: string) =>
BUILTIN_IGNORED_DIRS.some(
Expand Down
25 changes: 25 additions & 0 deletions test/unit/utils/path/is-inside-another-path-unix.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path';

jest.mock('path', () => jest.requireActual('path').posix);

const unixTests: [string, string, boolean][] = [
// Identical
['/foo', '/foo', false],
// Nothing in common
['/foo', '/bar', false],
// subfolder
['/foo', '/foo/bar', true],
// parallel
['/foo', '/foo/../bar', false],
// relative subfolder
['/foo', '/foo/./bar', true],
];

describe('Properly detects ignored sub-folders on Unix', () => {
it('should work on Unix', () => {
unixTests.forEach(([parent, testedPath, expectedResult]) => {
const result = isInsideAnotherPath(parent, testedPath);
expect(result).toEqual(expectedResult);
});
});
});
21 changes: 21 additions & 0 deletions test/unit/utils/path/is-inside-another-path-windows.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path';

jest.mock('path', () => jest.requireActual('path').win32);

const windowsTests: [string, string, boolean][] = [
// subfolder
['C:\\Foo', 'C:\\Foo\\Bar', true],
// Nothing in common
['C:\\Foo', 'C:\\Bar', false],
// Wrong drive.
['C:\\Foo', 'D:\\Foo\\Bar', false],
];

describe('Properly detects ignored sub-folders on Windows', () => {
it('should work on Windows', () => {
windowsTests.forEach(([parent, testedPath, expectedResult]) => {
const result = isInsideAnotherPath(parent, testedPath);
expect(result).toEqual(expectedResult);
});
});
});