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

@typescript-eslint/parser error with TypeScript test files #637

Closed
patrik-csak opened this issue Nov 19, 2021 · 5 comments · Fixed by #677
Closed

@typescript-eslint/parser error with TypeScript test files #637

patrik-csak opened this issue Nov 19, 2021 · 5 comments · Fixed by #677

Comments

@patrik-csak
Copy link

I'd like to use XO to lint and format my TypeScript tests in addition to my source code.

When I add a test file to my project, I get the following error from XO:

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

  1 error

I think this is happening because my test files are excluded in TSConfig.

Here are some minimal reproductions in StackBlitz:

@spence-s
Copy link
Contributor

spence-s commented Nov 19, 2021

Ah this is an edge case I missed when I was fixing our TS handling - but the fix is pretty simple. Until I have time to get a PR, an ok workaround is to create an
tsconfig.xo.json that extends your config but does NOT exclude your tests. Then in your xo configuration, add:

parserOptions: {
	project: './tsconfig.xo.json'
}

Once I get the fix in - xo will basically do this exact same thing under the hood.

If you want an example of a project that already does this exact same thing, see: ow

@patrik-csak
Copy link
Author

Appreciate the quick reply, @spence-s

The workaround makes sense, but it's not working for me

Here's a StackBlitz project with the workaround: https://stackblitz.com/edit/node-q5oxrl. XO continues to fail.

I tried to replicate the error on https://github.com/sindresorhus/ow/tree/v0.28.1 (npm install failed on main). If I delete package.json.xo.parserOptions, I expect to see the parsing error, but there are no errors.

ow@0.28.1 uses xo@0.38.2, so here's the same 'XO + TS with exclude' StackBlitz project but with xo@^0.38 instead of xo@latest: https://stackblitz.com/edit/node-atsn3f. XO works for this project.

@spence-s
Copy link
Contributor

spence-s commented Nov 19, 2021

@ptrkcsk yep - xo 0.40+ has some major architectural changes.

The reason it fails on your stack blitz is because of 2 reasons.

  1. tsconfig excludes override includes. So your tsconfig.xo.json is still ignoring you test files.
  2. your tsconfig.xo.json should include all the files you want to lint (for some tsconfig this means including more files, for some this means removing excluded files)

try this:

{
  "extends": "./tsconfig.json",
  "exclude": []
}

also - beware of a stale cache after changing a tsconfig file here. May be a problem with typescript-eslint that caching doesn't get busted properly for affected files when changing it. If you still get errors remove the cache: rm -rf node_modules/.cache

Hope this makes more sense to you now! This will be patched soon so you won't have to worry about this work around.

edit: attempted to make a stack blitz here to make everything work https://stackblitz.com/edit/node-fgblkr

@patrik-csak
Copy link
Author

There we go. I was mistakenly treating include as an exclude undo, but it's the other way around.

Important: exclude only changes which files are included as a result of the include setting. [TSConfig Reference]

I was aware of the cache. I had been running rm -fr node_modules && npm i && npm test. Didn't know about node_modules/.cache. Thanks for that!

Appreciate your time on this one, @spence-s !

@sekhavati
Copy link

Hope this makes more sense to you now! This will be patched soon so you won't have to worry about this work around

@spence-s thank you for sharing the workaround, we've been using it for a number of months now 👍 Is a fix still planning to be released soon?

msamblanet added a commit to msamblanet/node-project-template that referenced this issue Jun 14, 2022
- 2022-06-14 - v0.7.2
  - Update @tsconfig to Node18
  - Add excludes to tsconfig base (xojs/xo#637)
  - Version bumps
spence-s added a commit to spence-s/xo that referenced this issue Aug 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants