Skip to content

Commit e5264e8

Browse files
committed
fix(typescript-estree): truncate number of files printed
* fix(typescript-estree): truncate number of files printed Address issue with size of error string produced causing serialization failure typescript-eslint#9124. * test(typescript-estree): add additional unit test for project service Additional test for truncated error message.
1 parent c18226e commit e5264e8

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

Diff for: packages/typescript-estree/src/useProgramFromProjectService.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,19 @@ export function useProgramFromProjectService(
8787
defaultProjectMatchedFiles.add(filePathAbsolute);
8888
}
8989
if (defaultProjectMatchedFiles.size > maximumDefaultProjectFileMatchCount) {
90+
const filePrintLimit = 20;
91+
const filesToPrint = Array.from(defaultProjectMatchedFiles).slice(
92+
0,
93+
filePrintLimit,
94+
);
95+
const truncatedFileCount =
96+
defaultProjectMatchedFiles.size - filesToPrint.length;
97+
9098
throw new Error(
9199
`Too many files (>${maximumDefaultProjectFileMatchCount}) have matched the default project.${DEFAULT_PROJECT_FILES_ERROR_EXPLANATION}
92100
Matching files:
93-
${Array.from(defaultProjectMatchedFiles)
94-
.map(file => `- ${file}`)
95-
.join('\n')}
96-
101+
${filesToPrint.map(file => `- ${file}`).join('\n')}
102+
${truncatedFileCount ? `...and ${truncatedFileCount} more files\n` : ''}
97103
If you absolutely need more files included, set parserOptions.EXPERIMENTAL_useProjectService.maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING to a larger value.
98104
`,
99105
);

Diff for: packages/typescript-estree/tests/lib/useProgramFromProjectService.test.ts

+52
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,58 @@ Matching files:
149149
- b
150150
- ${path.normalize('/repos/repo/path/PascalCaseDirectory/camelCaseFile.ts')}
151151
152+
If you absolutely need more files included, set parserOptions.EXPERIMENTAL_useProjectService.maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING to a larger value.
153+
`);
154+
});
155+
156+
it('truncates the files printed by the maximum allowed files error when they exceed the print limit', () => {
157+
const { service } = createMockProjectService();
158+
const program = { getSourceFile: jest.fn() };
159+
160+
mockGetProgram.mockReturnValueOnce(program);
161+
162+
service.openClientFile.mockReturnValueOnce({});
163+
164+
expect(() =>
165+
useProgramFromProjectService(
166+
createProjectServiceSettings({
167+
allowDefaultProjectForFiles: [mockParseSettings.filePath],
168+
maximumDefaultProjectFileMatchCount: 2,
169+
service,
170+
}),
171+
mockParseSettings,
172+
true,
173+
new Set(Array.from({ length: 100 }, (_, i) => String(i))),
174+
),
175+
).toThrow(`Too many files (>2) have matched the default project.
176+
177+
Having many files run with the default project is known to cause performance issues and slow down linting.
178+
179+
See https://typescript-eslint.io/troubleshooting/#allowdefaultprojectforfiles-glob-too-wide
180+
181+
Matching files:
182+
- 0
183+
- 1
184+
- 2
185+
- 3
186+
- 4
187+
- 5
188+
- 6
189+
- 7
190+
- 8
191+
- 9
192+
- 10
193+
- 11
194+
- 12
195+
- 13
196+
- 14
197+
- 15
198+
- 16
199+
- 17
200+
- 18
201+
- 19
202+
...and 81 more files
203+
152204
If you absolutely need more files included, set parserOptions.EXPERIMENTAL_useProjectService.maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING to a larger value.
153205
`);
154206
});

0 commit comments

Comments
 (0)