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

ESLint was configured to run on <tsconfigRootDir>/.eslintrc.cjs using parserOptions.project #13747

Closed
7 tasks done
xixixao opened this issue Jul 7, 2023 · 8 comments · Fixed by #13749
Closed
7 tasks done
Assignees
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@xixixao
Copy link

xixixao commented Jul 7, 2023

Describe the bug

With React+Typescript template ESLint is configured to run on /.eslintrc.cjs but tsconfig is not configured to run on /.eslintrc.cjs.

Reproduction

https://github.com/xixixao/repro-vite-template-bad-eslint-config

Steps to reproduce

npm create vite@latest foo -- --template react-ts
cd foo
npm install
Open .eslintrc.cjs in VS Code, make some no op change like adding a new line at the end, and save the file.

System Info

System:
    OS: macOS 13.3.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 353.23 MB / 64.00 GB
    Shell: 3.5.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 18.15.0 - ~/Library/Caches/fnm_multishells/30307_1688761494412/bin/node
    npm: 9.5.0 - ~/Library/Caches/fnm_multishells/30307_1688761494412/bin/npm
    pnpm: 7.27.0 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 114.0.5735.198
    Edge: 112.0.1722.48
    Safari: 16.4
  npmPackages:
    @vitejs/plugin-react: ^4.0.1 => 4.0.2
    vite: ^4.4.0 => 4.4.2

Used Package Manager

npm

Logs

No response

Validations

@gmwill934
Copy link

happening the same to me on 4.4.0

@ben-x9
Copy link

ben-x9 commented Jul 8, 2023

Add ignorePatterns: [".eslintrc.cjs", "vite.config.ts"], to .eslintrc.cjs to fix

@vaynevayne
Copy link

Is this a painkiller? Don't solve the problem, just cover it up?

@ArnaudBarre
Copy link
Member

Another fix is to use project: ['./tsconfig.json', './tsconfig.node.json'],

@HendrikThePendric
Copy link

HendrikThePendric commented Jul 13, 2023

I might be stating the obvious here, but adding the files to ignorePatterns will disable ESLint for these files, which probably isn't what you want. So the suggestion @ArnaudBarre made is probably more helpful. Personally I had to go through a few additional steps as well. I'll list them below:

  1. In .eslintrc.cjs add project: ['./tsconfig.json', './tsconfig.node.json'], to parserOptions
  2. In tsconfig.node.json add "allowJs": true, to compilerOptions and update the include array to this "include": ["vite.config.ts", ".eslintrc.cjs",]
  3. But now I saw a warning Cannot write file './eslintrc.cjs' because it would overwrite input file.. To fix, add "noEmit": true, to the compilerOptions in tsconfig.node.json.
  4. And finally I now saw a lint error in .eslintrc.cjs which said tsconfigRootDir: __dirname Unsafe assignment of an "any" value. Since __dirname is a NodeJS thing I figured I needed to install the types for nodeJS yarn add -D @types/node, and tell the compiler to use them, so I added "types": ["@types/node"], to compilerOptions in tsconfig.node.json

Now everything is being linted and I am not seeing any lint errors anymore. So probably this is the "correct" configuration.

Caveat: Whenever you add a file to the project root which you want to be linted, you'll have to manually add that file to the include array in tsconfig.node.json. I tried editing the include and exclude fields in tsconfig.node.json to effectively make it include "all relevant files at the project root" but I couldn't get it to work as I intended and got the initial error again.

@rrapstine
Copy link

rrapstine commented Jul 14, 2023

@HendrikThePendric I get an error adding project: ['./tsconfig.json', './tsconfig.node.json'], to compilerOptions in .eslintrc.cjs. Says there's no such top-level property. Did you mean to say it should be added to parserOptions?

Specifically I am encountering an error whenever I add a server.js file to the project root. Everything compiles and lints fine, but I still get the red squiggles in VSCode.

@HendrikThePendric
Copy link

HendrikThePendric commented Jul 15, 2023

Did you mean to say it should be added to parserOptions?

Sorry my bad. Yes I meant parser, not compiler. I'll update my original post to reflect this.

However, I found that the config I suggested is not working perfectly either. When I run yarn tsc I get the following error:

Referenced project '[...]/tsconfig.node.json' may not disable emit.

I can run the app in development mode, but obviously the error will cause problems whenever it is expected that the project can compile without errors.

I've been able to address this to some extend:

  • Remove this from ./tsconfig.json: "references": [{ "path": "./tsconfig.node.json" }]
  • Add this to ./tsconfig.node.json: "extends": "./tsconfig.json"

This makes the error go away and everything still works as expected.

However the TypeScript docs seem to suggest that using project references offers some performance benefits, which you would be opting out of when you ditch the references field. So I probably wouldn't classify the above as a proper solution. Although for projects that have the vast majority of the files in the src folder anyway, I can't imagine it makes a lot of difference.

I'm no TypeScript expert by any means, and by now slightly out of my depth, so take my advise with a large dose of salt.

@kalijonn
Copy link

I only had to change these

  1. change parserOptions.project to ['./tsconfig.json', './tsconfig.node.json']
  2. add './.eslintrc.cjs' in include of tsconfig.node.json

I think eslint isn't picking up both tsconfig and tsconfig.node

stephane-ruhlmann added a commit to incubateur-ademe/benefriches that referenced this issue Jul 26, 2023
ESLint shoots a warning when linting vite.config.ts because it was not included. The warning only occured while linting through lint-staged which includes every .ts files whereas in our package.json we tell eslint to run through all the files under src/ (vite.config.ts is a the root). Workaround found here vitejs/vite#13747
@github-actions github-actions bot locked and limited conversation to collaborators Aug 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants