Skip to content

Commit 664ecb7

Browse files
authoredAug 14, 2023
fix(compiler): match tsconfig include paths properly (#4676)
this commit fixes a bug where a user was unable to use relative paths in the `include` property of their `tsconfig.json` file if `srcDir` was also specified. previously, when `tsconfig.json#include` contained a relative path: ``` "include": [ "./app/javascript" ], ``` and `tsconfig.json#srcDir` included the non-relative version of that path: ``` srcDir: 'app/javascript', ``` then you would receive a very confusing error: ``` [ WARN ] tsconfig.json "include" required In order for TypeScript to improve watch performance, it's recommended the "tsconfig.json" file should have the "include" property, with at least the app's "app/javascript" directory listed. For example: "include": ["app/javascript"] ``` closes #4667
1 parent a62498c commit 664ecb7

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { hasSrcDirectoryInclude } from '../typescript-config';
2+
3+
describe('typescript-config', () => {
4+
describe('hasSrcDirectoryInclude', () => {
5+
it('returns `false` for a non-array argument', () => {
6+
// the intent of this test is to evaluate when a user doesn't provide an array, hence the type assertion
7+
expect(hasSrcDirectoryInclude('src' as unknown as string[], 'src')).toBe(false);
8+
});
9+
10+
it('returns `false` for an empty array', () => {
11+
expect(hasSrcDirectoryInclude([], 'src/')).toBe(false);
12+
});
13+
14+
it('returns `false` when an entry does not exist in the array', () => {
15+
expect(hasSrcDirectoryInclude(['src'], 'source')).toBe(false);
16+
});
17+
18+
it('returns `true` when an entry does exist in the array', () => {
19+
expect(hasSrcDirectoryInclude(['src', 'foo'], 'src')).toBe(true);
20+
});
21+
22+
it('returns `true` for globs', () => {
23+
expect(hasSrcDirectoryInclude(['src/**/*.ts', 'foo/'], 'src/**/*.ts')).toBe(true);
24+
});
25+
26+
it.each([
27+
[['src'], './src'],
28+
[['./src'], 'src'],
29+
[['../src'], '../src'],
30+
[['*'], './*'],
31+
])('returns `true` for relative paths', (includedPaths, srcDir) => {
32+
expect(hasSrcDirectoryInclude(includedPaths, srcDir)).toBe(true);
33+
});
34+
});
35+
});

‎src/compiler/sys/typescript/typescript-config.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,21 @@ const createDefaultTsConfig = (config: d.Config) =>
166166
2,
167167
);
168168

169-
const hasSrcDirectoryInclude = (includeProp: string[], src: string) =>
170-
Array.isArray(includeProp) && includeProp.includes(src);
169+
/**
170+
* Determines if the included `src` argument belongs in `includeProp`.
171+
*
172+
* This function normalizes the paths found in both arguments, to catch cases where it's called with:
173+
* ```ts
174+
* hasSrcDirectoryInclude(['src'], './src'); // should return `true`
175+
* ```
176+
*
177+
* @param includeProp the paths in `include` that should be tested
178+
* @param src the path to find in `includeProp`
179+
* @returns true if the provided `src` directory is found, `false` otherwise
180+
*/
181+
export const hasSrcDirectoryInclude = (includeProp: string[], src: string): boolean =>
182+
Array.isArray(includeProp) &&
183+
includeProp.some((included) => normalizePath(included, false) === normalizePath(src, false));
171184

172185
const hasStencilConfigInclude = (includeProp: string[]) =>
173186
Array.isArray(includeProp) && includeProp.includes('stencil.config.ts');

0 commit comments

Comments
 (0)
Please sign in to comment.