xo icon indicating copy to clipboard operation
xo copied to clipboard

@typescript-eslint/parser error with TypeScript test files

Open patrik-csak opened this issue 3 years ago • 5 comments

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:

patrik-csak avatar Nov 19 '21 16:11 patrik-csak

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

spence-s avatar Nov 19 '21 16:11 spence-s

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.

[email protected] uses [email protected], 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.

patrik-csak avatar Nov 19 '21 18:11 patrik-csak

@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

spence-s avatar Nov 19 '21 18:11 spence-s

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 !

patrik-csak avatar Nov 19 '21 18:11 patrik-csak

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?

sekhavati avatar May 09 '22 09:05 sekhavati