Skip to content

Commit

Permalink
fix: TypeStrong#775 node_modules not ignored on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
annitya authored and piotr-oles committed Mar 4, 2023
1 parent 5243592 commit 0204492
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/utils/path/is-inside-another-path.ts
@@ -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
@@ -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
39 changes: 39 additions & 0 deletions test/unit/utils/path/is-inside-another-path.spec.ts
@@ -0,0 +1,39 @@
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path';

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],
];

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', () => {
it('should work on Unix', () => {
unixTests.forEach(([parent, testedPath, expectedResult]) => {
const result = isInsideAnotherPath(parent, testedPath);
expect(result).toEqual(expectedResult);
});
});

it('should work on Windows', () => {
windowsTests.forEach(([parent, testedPath, expectedResult]) => {
const result = isInsideAnotherPath(parent, testedPath);
expect(result).toEqual(expectedResult);
});
});
});

0 comments on commit 0204492

Please sign in to comment.