Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. #967

Closed
doberkofler opened this issue Sep 11, 2019 · 18 comments

Comments

@doberkofler
Copy link
Contributor

@doberkofler doberkofler commented Sep 11, 2019

What code were you trying to parse?

any kind of sub module that is currently not used by any other module

What did you expect to happen?

What actually happened?

When using eslint on a sub module that is currently not used by any other module, the following type of error message is shown:

error: Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: unusedFile.ts.
The file must be included in at least one of the projects provided at unusedFile.ts:

If I import the file in another module the error disappears.

This error message is relatively new and previous version of the @typescript-eslint/parser did not complain.

Versions

package version
@typescript-eslint/parser 2.2.0
TypeScript 3.6.3
ESLint 6.3.0
node 12.10.0
npm 6.11.2
@bradzacher

This comment has been minimized.

Copy link
Member

@bradzacher bradzacher commented Sep 11, 2019

Could you please provide some more information about your environment?
What is your tsconfig? eslint config? Are you using cli or and ide?

Help me to help you

@bradzacher bradzacher added awaiting response and removed triage labels Sep 11, 2019
@doberkofler

This comment has been minimized.

Copy link
Contributor Author

@doberkofler doberkofler commented Sep 11, 2019

@bradzacher I experienced this problem in my (pretty complex) development environment after updating the typescript compiler and the @typescript-eslint components to the latest version and did not yet manage to distill this problem down to a reasonable simple example.

I experience the problem from within VSCode but also when invoking esling from the command line using: eslint --cache --report-unused-disable-directives \"./**/*.{js,jsx,ts,tsx}\""

The key configuration files are:

tsconfig.json

{
  "extends": "./configs/tsconfig_base",
  "compilerOptions": {
    "paths": {
      "Source_Data/*": ["./src/data/*"],
      "Source_Framework/*": ["./src/framework/*"],
      "Source_Modules/*": ["./src/modules/*"],
      "Source_Vendor/*": ["./vendor/*"]
    }
  },
  "include": [
    "src",
    "test/unittest"
  ]
}

tsconfig_base.json

{
  "compilerOptions": {
    "noEmit": true,
    "downlevelIteration": true,
    "allowJs": true,
    "checkJs": false,
    "sourceMap": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": false,
    "noUnusedLocals": true,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true,
    "declaration": false,
    "importHelpers": true,
    "moduleResolution": "node",
    "target": "es2015",
    "module": "es2015",
    "jsx": "react",
    "baseUrl": "../",
    "outDir": "../temp",
    "typeRoots": [
      "../node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "compileOnSave": false,
  "exclude": [
    "node_modules"
  ]
}

.eslintrc.js

module.exports = {
	parserOptions: {
		ecmaVersion: 2017,
		sourceType: 'script',
	},
	env: {
		'es6': true,
	},
	extends: [
		'./configs/eslint_baserules.js',
		'../configs/eslint_typescript.js',
		'../configs/eslint_import.js',
		'../configs/eslint_promise.js',
		'../configs/eslint_react.js',
		'../configs/eslint_jsdoc.js',
	],
};

eslint_typescript.js

module.exports = {
	parser: '@typescript-eslint/parser',
	parserOptions: {
		project: './tsconfig.json'
	},
	extends: [
		'plugin:@typescript-eslint/recommended',
	],
	rules: {
		'@typescript-eslint/array-type': ['warn', {default: 'generic'}],
		'@typescript-eslint/await-thenable': 'warn',
		'@typescript-eslint/ban-ts-ignore': 'off',
		'brace-style': 'off',
		'@typescript-eslint/brace-style': ['warn', '1tbs'],
		'camelcase': 'off',
		'@typescript-eslint/camelcase': ['warn', {'properties': 'never'}],
		'@typescript-eslint/explicit-function-return-type': 'off', // should probably be on
		'@typescript-eslint/explicit-member-accessibility': 'off',
		'indent': 'off',
		'@typescript-eslint/indent': ['warn', 'tab', {'SwitchCase': 1}],
		'@typescript-eslint/member-delimiter-style': ['warn',
			{
				singleline: {
					delimiter: 'comma',
					requireLast: false
				},
				multiline: {
					delimiter: 'comma',
					requireLast: true
				}
			}
		],
		'@typescript-eslint/no-empty-function': 'off',
		'@typescript-eslint/no-explicit-any': 'off',
		'no-extra-parens': 'off',
		'@typescript-eslint/no-extra-parens': 'warn',
		'@typescript-eslint/no-extraneous-class': 'warn',
		'@typescript-eslint/no-for-in-array': 'warn',
		'@typescript-eslint/no-inferrable-types': ['warn', {'ignoreParameters': true}],
		'@typescript-eslint/no-misused-promises': 'off',
		'no-unused-vars': 'off',
		'@typescript-eslint/no-unused-vars': 'warn',
		'@typescript-eslint/no-unnecessary-qualifier': 'warn',
		'@typescript-eslint/no-unnecessary-type-assertion': 'warn',
		'@typescript-eslint/no-useless-constructor': 'warn',
		'@typescript-eslint/no-require-imports': 'warn',
		'@typescript-eslint/no-this-alias': [
			'warn',
			{
				allowDestructuring: true,
				allowedNames: ['that']
			}
		],
		'@typescript-eslint/no-use-before-define': 'off',
		'@typescript-eslint/prefer-function-type': 'warn',
		'@typescript-eslint/prefer-includes': 'off',
		'@typescript-eslint/prefer-interface': 'off',
		'@typescript-eslint/prefer-string-starts-ends-with': 'off',
		'@typescript-eslint/prefer-readonly': 'warn',
		'@typescript-eslint/promise-function-async': 'off',
		'@typescript-eslint/require-array-sort-compare': 'warn',
		'@typescript-eslint/require-await': 'warn',
		'@typescript-eslint/restrict-plus-operands': 'off',
		'@typescript-eslint/strict-boolean-expressions': 'off',
		'@typescript-eslint/unbound-method': 'warn',
	}
};
@doberkofler

This comment has been minimized.

Copy link
Contributor Author

@doberkofler doberkofler commented Sep 12, 2019

@bradzacher I now isolated the issue in this sample project
file_does_not_match_project_config.zip

@calumpeak

This comment was marked as off-topic.

Copy link

@calumpeak calumpeak commented Sep 12, 2019

Also encountering this issue. Currently in my webpack config as it's not being consumed by another module:

 0:0  error  Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: .../webpack.config.js.
The file must be included in at least one of the projects provided

eslintrc

const config = {
  parser: '@typescript-eslint/parser',
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:react/recommended',
    'prettier',
    'prettier/@typescript-eslint',
  ],
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    project: './tsconfig.json',
  },
  env: {
    es6: true,
    browser: true,
    node: true,
  },
  settings: {
    react: {
      version: 'detect',
    },
  },
  rules: {
    '@typescript-eslint/explicit-function-return-type': 'off',
  },
  overrides: [
    {
      // require() statements allowed in .js files
      files: ['*.js'],
      rules: {
        '@typescript-eslint/no-var-requires': 'off',
      },
    },
  ],
};

module.exports = config;

tsconfig

{
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "esnext",
    "target": "esnext",
    "jsx": "react",
    "allowJs": true,
    "noEmit": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}
@calumpeak

This comment was marked as off-topic.

Copy link

@calumpeak calumpeak commented Sep 12, 2019

@doberkofler I found a solution. I came across an issue thread here. issue #853

TL;DR: You need to update the include array in your tsconfig to include all fles you want to lint. That's not exactly clear from any documentation. But this is what it looks like now:

{
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "esnext",
    "target": "esnext",
    "jsx": "react",
    "allowJs": true,
    "noEmit": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": true
  },
  "include": ["src", "**/*.tsx", "**/*.ts", "**/*.js"],
  "exclude": ["node_modules", "dist"]
}

Hope that helps.

P.S. if you're using VScode you may need to reload it for the IDE to recognise the changes, CLI should be fine though.

@doberkofler

This comment has been minimized.

Copy link
Contributor Author

@doberkofler doberkofler commented Sep 12, 2019

@calumpeak Unfortunately this is not possible in my environment as we use several different tsconfig files and each of them includes the appropriate files.

@bradzacher

This comment has been minimized.

Copy link
Member

@bradzacher bradzacher commented Sep 12, 2019

You need to update the include array in your tsconfig to include all fles you want to lint. That's not exactly clear from any documentation

There's a whole section on it in the readme...
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/parser/README.md#configuration

... you must only lint files that are included in the projects as defined by the provided tsconfig.json files. If your existing configuration does not include all of the files you would like to lint, you can create a separate tsconfig.eslint.json as follows:

Additionally, the error message that the parser gives you pretty explicitly states it as well:

The file must be included in at least one of the projects provided

Please let me know if any of the documentation is not clear enough.


if you're using VScode you may need to reload it for the IDE to recognise the changes, CLI should be fine though.

We're working to fix this #864

@bradzacher

This comment has been minimized.

Copy link
Member

@bradzacher bradzacher commented Sep 12, 2019

@doberkofler - looking at your zip repro, it's all working as intended.

Your tsconfig has the following includes:

  "include": [
    "src/core"
  ],

Which means that src/util/util.ts is not included within the project.
As such, we cannot parse it.

You have to ensure all files are included within the tsconfig.

   "include": [
+    "src/util",
     "src/core"
   ],

I made this change, ran yarn test, and everything worked as intended.

If you have many tsconfigs, you can provide as many or as few of them to the parser as you want, can see more information on this in the parser readme.

Alternately, you should create a separate tsconfig.eslint.json file, and just include every single file within that config (can see more information on this in the parser readme).

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/parser/README.md#configuration

@tomasz-szepczynski

This comment was marked as off-topic.

Copy link

@tomasz-szepczynski tomasz-szepczynski commented Sep 16, 2019

Still occurs for me

@doberkofler

This comment has been minimized.

Copy link
Contributor Author

@doberkofler doberkofler commented Sep 16, 2019

@bradzacher Thank you for your feedback. I understand how to setup the configuration files but must admit that on a large (I have about 30 TypeScript configuration files) project this new requirement makes things a lot more complicated. I was wondering why this new required is needed and was not in earlier versions?

@bradzacher

This comment has been minimized.

Copy link
Member

@bradzacher bradzacher commented Sep 16, 2019

More information on why is located in the 2.0.0 release notes:
https://github.com/typescript-eslint/typescript-eslint/releases/tag/v2.0.0

TL;DR is because if you don't include the linted files within the tsconfigs you provide us, then it will cause a serious performance impact, which is O(n^m) where n is the number of files not included in the config, and m is the average depth of the dependency tree of those files. I.e. for some projects we've seen as much as a 10x slow down.
The change introduced in 2.0 hard fails when we encounter a file outside of the tsconfigs, so it's impossible for users to trigger this perf regression.


If you've got many tsconfig files, you can either:

  • provide them individually as an array of strings
    (eg project: ["./tsconfig.json", "./package1/tsconfig.json", ...].
  • provide them as a single (or multiple) globs
    (eg project: "./**/tsconfig.json")
  • mix and match the two
    (eg project: ["./tsconfig.json", "./**/tsconfig.json", ...])

My recommendation is to just do a new tsconfig.eslint.json in your project root which looks something like:

{
  "compilerOptions": { "strict": true },
  "include": [
    "**/src/**/*.ts",
    "**/test/**/*.ts",
    // etc
  ]
}

I'm going to close this for housekeeping purposes, because it looks like there's no bug here.
But we can keep discussing here!

@bradzacher bradzacher closed this Sep 16, 2019
@doberkofler

This comment has been minimized.

Copy link
Contributor Author

@doberkofler doberkofler commented Sep 16, 2019

@bradzacher Thank you very much for your extensive feedback and all the work you are doing on this project!

@r-bman

This comment has been minimized.

Copy link

@r-bman r-bman commented Sep 17, 2019

Resarting VSCode solved this for me 🤔

@tsujp

This comment has been minimized.

Copy link

@tsujp tsujp commented Sep 17, 2019

Perhaps I am being thick right now but is there a way to glob any *.ts file N levels deep from the current context? This would remove the need to use things like createDefaultProgram which would get everything (as far as I know).

So, "recursively get any and all .ts files from here down" essentially.

@bradzacher

This comment has been minimized.

Copy link
Member

@bradzacher bradzacher commented Sep 17, 2019

@r-bman - #864, we're working on it and should have a fix out soon.


@tsujp - the typescript compiler apis we utilise do not work this way; you have to pass it the path to a tsconfig.

We can't really create a temporary tsconfig file in your project during lint, because we don't know the structure of your folders (it's also not a great practice to create temporary files in the project folder).

Additionally, whilst globs are reasonably fast, for some larger/deeply nested projects, providing such wide-reaching globs can cause perf issues.
There's also the problem of needing to make sure to exclude things like node_modules, and other things (like the coverage report folder created by test tools, build folders, config folders, template files, etc).
If you select more files than necessary then you could severely impact perf by parsing / typechecking more files than necessary...

@JeffGuKang

This comment has been minimized.

Copy link

@JeffGuKang JeffGuKang commented Oct 7, 2019

Using createDefaultProgram: true can be the temporary solution in parserOptions for me.

parserOptions:  {
    ...,
    createDefaultProgram: true,        
},
@bradzacher

This comment has been minimized.

Copy link
Member

@bradzacher bradzacher commented Oct 7, 2019

@JeffGuKang - a solution to what exactly?
This particular "issue" isn't going away in the next release.

This is how the parser works now. Either you configure it to include all your files, or you get parsing errors.

Failing to do so, and turning on createDefaultProgram as a bandaid will cause performance problems for your lint runs.

There is one issue which will be fixed soon (IDE errors when creating a new file), but that is unrelated to this thread.

@JeffGuKang

This comment has been minimized.

Copy link

@JeffGuKang JeffGuKang commented Oct 8, 2019

@bradzacher Thank you for feedback. I've fixed some misunderstandings and it was related to #864, not this.
And I just found #890 you already made. 👍

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
7 participants
You can’t perform that action at this time.