8000 Bug: `parserService` allowDefaultProject just isn't working · Issue #9739 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: parserService allowDefaultProject just isn't working #9739

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

Closed
4 tasks done
samesfahani-tuplehealth opened this issue Aug 6, 2024 · 14 comments
Closed
4 tasks done
Labels
locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. triage Waiting for team members to take a look

Comments

@samesfahani-tuplehealth
Copy link
samesfahani-tuplehealth commented Aug 6, 2024

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Repro link

https://stackblitz.com/edit/vitejs-vite-rv1ac6?file=eslint.config.js

Relevant Package

parser

I am in the process of upgrading to ESLint 9 from 8, but running into issues with this package complaining about files that are not source code (they are config files, script files, etc)

ESLint Config

import baseConfig from './eslint.config.mjs';

export default [
  ...baseConfig,
  {
    languageOptions: {
      parserOptions: {
        projectService: {
          allowDefaultProject: ['*.js', '*.mjs'],
        },
        tsconfigRootDir: import.meta.dirname,
      },
    },
  },
  {
    rules: {
      '@typescript-eslint/await-thenable': 'warn',
      '@typescript-eslint/no-floating-promises': 'warn',
      '@typescript-eslint/require-await': 'warn',
      'no-return-await': 'warn',
    },
  },
];

tsconfig

{
  "files": [],
  "references": [{ "path": "./packages/package1" }, { "path": "./packages/package2" }],
  "compilerOptions": {
    "resolveJsonModule": true,
    "esModuleInterop": true
  }
}

Expected Result

I expect no errors as previously was the case on ESLint 8.57.

Actual Result

A bunch of errors similar to:

/Users/sam/repo/prettier.config.mjs
  0:0  error  Parsing error: /Users/sam/repo/prettier.config.mjs was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

Additional Info

No response

Versions

package version
@typescript-eslint/eslint-plugin 8.0.1
@typescript-eslint/parser 8.0.1
@typescript-eslint/rule-tester X.Y.Z
@typescript-eslint/scope-manager X.Y.Z
@typescript-eslint/typescript-estree X.Y.Z
@typescript-eslint/type-utils X.Y.Z
@typescript-eslint/utils X.Y.Z
TypeScript 5.5.4
ESLint 9.8.0
node 20.12.2
@samesfahani-tuplehealth samesfahani-tuplehealth added bug Something isn't working triage Waiting for team members to take a look labels Aug 6, 2024
@JoshuaKGoldberg
Copy link
Member

👋 Hey thanks for trying out the new project service! We'd love to help, except your issue doesn't have a reproduction for us to work with. We'd need more th 8000 an just a few config files and package versions.

See https://antfu.me/posts/why-reproductions-are-required#why-reproduction for an explanation of what we'd need.

@JoshuaKGoldberg JoshuaKGoldberg added unable to repro issues that a maintainer was not able to reproduce awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look bug Something isn't working labels Aug 6, 2024
@samesfahani-tuplehealth
Copy link
Author
samesfahani-tuplehealth commented Aug 6, 2024

I'll dig into the repro in a bit (getting a templated StackBlitz for a monorepo set up is surprisingly annoying).

In the meantime -- Can you maybe explain a bit more about projectService? I used to have a tsconfig.eslint.json which had a bunch of includes listed. The old config had project: ['tsconfig.eslint.json', 'packages/*/tsconfig.json'], and those included files were ok.

Now with the above setup with projectService, all of those files that were previously included are throwing parsing errors. Conceptually, what would need to be done to achieve parity?

@bradzacher
Copy link
Member

The project service uses the same infra as your IDE. So it should just work the same - automatically.

It doesn't use a tsconfig.eslint.json - it uses your tsconfig.jsons.

@samesfahani-tuplehealth
Copy link
Author
samesfahani-tuplehealth commented Aug 7, 2024

Yup, understand that. Was just stating that previously (in Eslint 8, legacy config), our setup (and probably common) was using a tsconfig.eslint.json (even mentioned as a bullet point in the docs here).

@samesfahani-tuplehealth
Copy link
Author

Repro: https://stackblitz.com/edit/vitejs-vite-rv1ac6?file=eslint.config.js

Run pnpm lint to reproduce the same errors I'm seeing. If you remove the // Typed rules section of rules and remove the parserOptions, the same files it complains about pass the linter fine. I am probably setting something up incorrectly here though so appreciate any help.

@JoshuaKGoldberg
Copy link
Member

I get these errors, is that right?

/home/projects/vitejs-vite-rv1ac6/eslint.config.js
  0:0  error  Parsing error: /home/projects/vitejs-vite-rv1ac6/eslint.config.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

/home/projects/vitejs-vite-rv1ac6/packages/app/vite.config.ts
  0:0  error  Parsing error: /home/projects/vitejs-vite-rv1ac6/packages/app/vite.config.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

/home/projects/vitejs-vite-rv1ac6/scripts/format.js
  0:0  error  Parsing error: /home/projects/vitejs-vite-rv1ac6/scripts/format.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

✖ 3 problems (3 errors, 0 warnings)

If so, a few notes:

  • You don't need to call fixupConfigRules or fixupPluginRules on typescript-eslint, it supports ESLint flat config & v9
  • The error message is correct! The tsconfig.json doesn't include those files.

Once we have #9023, it'll be easier to add "see more" docs to error messages, such as https://typescript-eslint.io/packages/parser#projectserviceoptions and the docs we'll write in #7350. In the meantime https://typescript-eslint.io/blog/announcing-typescript-eslint-v8#project-service has the info you'd need here. Would that have helped @samesfahani-tuplehealth?

@samesfahani-tuplehealth
Copy link
Author
samesfahani-tuplehealth commented Aug 7, 2024

Yup those are the errors.

You don't need to call fixupConfigRules or fixupPluginRules on typescript-eslint, it supports ESLint flat config & v9

Yeah this was just automatically done from the ESLint auto migration script. Can clean up later.

The error message is correct! The tsconfig.json doesn't include those files.

Per the error message, adding allowDefaultProject: ['*.js'], to the parserOptions still throws the error for eslint.config.js. I'm perhaps just totally confused what this flag is meant to do, even after reading the blog post, so apologies in advance.

And do I really want to add e.g. eslint.config.js to my tsconfig? As in, is that the correct thing to even be doing?

@JoshuaKGoldberg JoshuaKGoldberg added triage Waiting for team members to take a look and removed awaiting response Issues waiting for a reply from the OP or another party unable to repro issues that a maintainer was not able to reproduce labels Aug 15, 2024
@stepankuzmin
Copy link
stepankuzmin commented Aug 16, 2024

Hey all,

It seems that allowDefaultProject has no effect. I've made a small repo with reproduction here: https://github.com/stepankuzmin/typescript-eslint-parser-bug.

EDIT: It seems to be working in the simple setup; there might be an issue with more complex setups.

After I disabled the import/no-named-as-default, there are no more ... was not found by the project service errors.

@xsjcTony
Copy link
xsjcTony commented Aug 17, 2024

Update

Turns out when using a eslint-config like library which will be installed into node_modules), you have to use process.cwd() instead of import.meta.dirname for tsconfigRootDir otherwise it won't work. Makes sense

But

@JoshuaKGoldberg Wondering whether typescript-eslint treats .js file differently? Because I can see even if I don't include .js files in allowDefaultProject, e.g. ['./*.js] Somehow it's still working. Is it because type-aware rules are not applied to .js files? But for .ts files seems I have to include it.


Original post, (No need to read)

Yeah I do really want to have a detailed guide how should we migrate from e.g. tsconfig.eslint.json to projectService.

Honestly @JoshuaKGoldberg , I can see the effort that you are trying to guide users to use projectService as much as possible in the doc, but that still doesn't make much sense.

It doesn't explain exactly what's allowDefaultProject. Previously I have all my files listed in tsconfig.node.json and tsconfig.node.js.json, one of those ts files like vite.config.ts, one of those js files like eslint.config.js, .ncurc.js, etc.
However, after using projectService, I'm getting all the not found error for those ts files. Those .js files are just working fine.

I'm so confused how I should make them work.
Below is my config:

projectService: {
  allowDefaultProject: ['vite.config.ts'],
  defaultProject: './tsconfig.json',
}

Even with this config, I just want to make vite.config.ts work. However, the file is still raising error. but eslint.config.js are still working for some reason, even it's not listed in the allowDefaultProject array🤣

Of course in the tsconfig.json file, I don't have vite.config.ts included, since I guess that's the purpose to have those options inside projectService, otherwise I can still uses the old project way with multiple tsconfig.x.json file, which just makes this new feature useless.

@JoshuaKGoldberg
Copy link
Member
parserOptions: {
  projectService: true,
  allowDefaultProject: ['*.js'],
  tsconfigRootDir: import.meta.dirname,
},

allowDefaultProject goes inside projectService:

parserOptions: {
  projectService: {
    allowDefaultProject: ['*.js'],
    defaultProject: true,
  },
  tsconfigRootDir: import.meta.dirname,
},

Relevant issues:

When I make that change (https://stackblitz.com/edit/vitejs-vite-bvs5pb?file=eslint.config.js), the ESLint failure reduce down to:

/home/projects/vitejs-vite-rv1ac6/eslint.config.js
  108:7  error  Duplicate key 'no-return-await'  no-dupe-keys

/home/projects/vitejs-vite-rv1ac6/packages/app/vite.config.ts
  0:0  error  Parsing error: /home/projects/vitejs-vite-rv1ac6/packages/app/vite.config.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

/home/projects/vitejs-vite-rv1ac6/scripts/format.js
  0:0  error  Parsing error: /home/projects/vitejs-vite-rv1ac6/scripts/format.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject
  • packages/app/vite.config.ts: if the nearest tsconfig.json to a file doesn't include that file, then its types will have to come from allowDefaultProject
  • scripts/format: missed in allowDefaultProject: ['*.js']

@JoshuaKGoldberg
Copy link
Member

Wondering whether typescript-eslint treats .js file differently? Because I can see even if I don't include .js files in allowDefaultProject, e.g. ['./*.js] Somehow it's still working. Is it because type-aware rules are not applied to .js files? But for .ts files seems I have to include it.

Yup, you got it! The project service itself doesn't treat .js files differently; it just uses the standard TypeScript logic. TypeScript's logic for .js files is to ignore them unless allowJs/checkJs is enabled. So if you have a ./tsconfig.json _without allowJs/checkJs and a ./file.js, neither tsc (CLI) nor the project service (editors/linting) will look at ./file.js.

@JoshuaKGoldberg
Copy link
Member

Anyway! I added those bits to #9753 (comment), our tracking issue for FAQs. Which I think is a good place for this issue's comments - those really are frequently asked questions that we should document. Thanks for the feedback, this was really helpful!

I'm going to go ahead and close this issue for now as allowDefaultProject is working & we have that separate issue for documenting the commonly confusing bits of it. But if you've got more stuff, feel free to post here (if before the automatic old issue locking) or in a new issue. Cheers! ❤️

@JoshuaKGoldberg
Copy link
Member

Oh, and:

do I really want to add e.g. eslint.config.js to my tsconfig? As in, is that the correct thing to even be doing?

Added to #9753 (comment) as well.

In this case, my advice would be to use allowDefaultProject for any JS file that you don't normally build.

@samesfahani-tuplehealth
Copy link
Author

In this case, my advice would be to use allowDefaultProject for any JS file that you don't normally build.

Well, in doing so, I hit this error:

 0:0  error  Parsing error: Too many files (>8) have matched the default project.

Having many files run with the default project is known to cause performance issues and slow down linting.

See https://typescript-eslint.io/troubleshooting/typed-linting#allowdefaultproject-glob-too-wide

If you absolutely need more files included, set parserOptions.projectService.maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING to a larger value

I'm setting maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING to 1000 and moving on at this point. Typed linting has always been slow for our project and as a result, it's run separately from the normal lint process (either in CI or a separate command pre PR).

The one weird thing about all of this is that I need to explicitly add config files to allowDefaultProject such as postcss.config.mjs, prettier.config.mjs, and even eslint.config.mjs/eslint.typed.mjs.

NatoBoram added a commit to NatoBoram/based.ts that referenced this issue Aug 23, 2024
…ross 1 directory (#52)

* ⬆️ Bump typescript-eslint in the eslint group across 1 directory

Bumps the eslint group with 1 update in the / directory: [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/typescript-eslint).


Updates `typescript-eslint` from 7.18.0 to 8.0.1
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/typescript-eslint/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v8.0.1/packages/typescript-eslint)

---
updated-dependencies:
- dependency-name: typescript-eslint
  dependency-type: direct:development
  update-type: version-update:semver-major
  dependency-group: eslint
...

Signed-off-by: dependabot[bot] <support@github.com>

* 🐛 Enable checkJs in tsconfig.eslint.json

typescript-eslint/typescript-eslint#9739 (comment)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Nato Boram <nato.boram+git@gmail.com>
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Aug 27, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. triage Waiting for team members to take a look
Projects
None yet
Development

No branches or pull requests

5 participants
0