已忽略dist目录文件,ESLint仍报项目服务未找到文件错误求助
问题:已在ESLint中忽略dist目录和eslint.config.mjs,但仍出现解析错误
我希望tsc将编译产物输出到dist目录(我是TypeScript新手,不确定这是否是正确的TS项目组织方式)。
项目结构
- dist(TypeScript编译后的代码)
- lib
- lib.d.ts
- lib.js
- index.d.ts
- index.js
- lib
- src(源代码)
- lib
- lib.ts
- index.ts
- lib
- eslint.config.mjs
- packages.json
- tsconfig.json
- yarn.lock
tsconfig.json配置
{ "compilerOptions": { "target": "ES2022", "module": "commonjs", "outDir": "./dist", "rootDir": "./", "strict": true, "esModuleInterop": true, "resolveJsonModule": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "baseUrl": "./src", "declaration": true, "allowJs": true }, "include": [ "src/**/*.ts", ], "exclude": [ "eslint.config.mjs", "dist/**" ] }
eslint.config.mjs配置
// @ts-check import eslint from '@eslint/js'; import tseslint from 'typescript-eslint'; import tsParser from '@typescript-eslint/parser' import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = dirname(fileURLToPath(import.meta.url)); export default tseslint.config( { files: [ "src/**/*.ts" ], ignores: [ 'dist/**/*.ts', 'dist/**', "**/*.mjs", "eslint.config.mjs", "**/*.js" ], rules: { "@typescript-eslint/no-unnecessary-condition": "error", "@typescript-eslint/no-unnecessary-type-assertion": "error", }, }, ...tseslint.configs.strictTypeChecked, ...tseslint.configs.recommendedTypeChecked, { languageOptions: { parserOptions: { project: "./tsconfig.json", projectService: true, tsconfigRootDir: __dirname, projectFolderIgnoreList: [ "**dist**", ], }, }, }, );
报错信息
/Users/kainzhong/VSCode/eslintTmp/dist/src/index.d.ts 0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/index.d.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject /Users/kainzhong/VSCode/eslintTmp/dist/src/index.js 0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/index.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.d.ts 0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.d.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.js 0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject /Users/kainzhong/VSCode/eslintTmp/eslint.config.mjs 0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/eslint.config.mjs was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject
明明已经在ESLint中忽略了这些文件,为什么还是会报错?
解决方案
1. 调整ESLint忽略规则的位置
你当前把ignores放在了第一个配置对象里,但后续引入的strictTypeChecked、recommendedTypeChecked等共享配置块不会继承这个局部忽略规则。ESLint的全局忽略规则必须放在顶层配置项中,才能对所有配置块生效。
修改后的eslint.config.mjs:
// @ts-check import eslint from '@eslint/js'; import tseslint from 'typescript-eslint'; import tsParser from '@typescript-eslint/parser' import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = dirname(fileURLToPath(import.meta.url)); export default tseslint.config( { // 全局忽略规则,对所有配置生效 ignores: [ 'dist/**', "eslint.config.mjs", ], }, { files: [ "src/**/*.ts" ], rules: { "@typescript-eslint/no-unnecessary-condition": "error", "@typescript-eslint/no-unnecessary-type-assertion": "error", }, }, ...tseslint.configs.strictTypeChecked, ...tseslint.configs.recommendedTypeChecked, { languageOptions: { parserOptions: { project: "./tsconfig.json", projectService: true, tsconfigRootDir: __dirname, projectFolderIgnoreList: [ "**/dist/**", ], }, }, }, );
2. 修复tsconfig的rootDir配置
你的rootDir设置为./,会导致tsc把整个项目根目录作为源目录,编译后在dist下生成嵌套的src层级(比如dist/src/...),这不符合标准TS项目结构。正确做法是将rootDir设为./src,这样编译产物会直接放在dist根目录下:
修改tsconfig.json的compilerOptions:
{ "compilerOptions": { // ...其他配置 "rootDir": "./src", // ...其他配置 }, // ...其他配置 }
3. 补充说明
- 你的项目结构方向是对的:src存源码、dist存编译产物,调整rootDir后会更规范。
projectFolderIgnoreList路径改为**/dist/**,匹配更准确,避免TS项目服务扫描到dist目录。
内容的提问来源于stack exchange,提问作者Jacket
相关产品推荐
相关产品推荐

