typescript-eslint: Bug: Project service settings can be nondeterministic based on config and file glob result orders

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.

Issue Description

I’m getting this error under eslint@9.12.0, which did not occur under eslint@9.11.1:

/wherever/vitest.config.ts
  0:0  error  Parsing error: /wherever/vitest.config.ts was not found by the project service. 
Consider either including it in the tsconfig.json or including it in allowDefaultProject

The pertinent config is:

  {
    files: ['*.ts', '{src,db}/**/*.ts'],
    extends: [...ts.configs.recommendedTypeChecked],
    languageOptions: {
      parserOptions: {
        projectService: {
          allowDefaultProject: ['vitest.config.ts'],
        },
        tsconfigRootDir: import.meta.dirname,
      },
    },
  },
  {
    files: ['db/**/*.ts'],
    languageOptions: {
      parserOptions: {
        projectService: true,
        tsconfigRootDir: import.meta.dirname + '/db',
      },
    },
  },

The problem disappears if any of these are changed:

  • eslint is downgraded to lower version
  • the last block (with files: ['db/**/*.ts']) is removed
  • the first block is changed to not overlap with the second (i.e., files: ['*.ts', 'src/**/*.ts'])

Reproduction Repository Link

https://github.com/LukeNotable/eslint-9.12.0-allow-default-project-bug

Repro Steps

  1. clone the repo
  2. yarn install
  3. yarn eslint .

Versions

package version
@typescript-eslint/eslint-plugin 8.9.0
@typescript-eslint/parser 8.9.0
@typescript-eslint/scope-manager 8.9.0
@typescript-eslint/typescript-estree 8.9.0
@typescript-eslint/type-utils 8.9.0
@typescript-eslint/utils 8.9.0
TypeScript 5.5.4
ESLint 9.12.0
node 22.9.0

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 7
  • Comments: 17 (7 by maintainers)

Most upvoted comments

I would be curious if TS 5.7’s project loading changes would make this more well-behaved; might be worth a try. There have been improvements to project loading which should result in projects being more correctly/optimally loaded (and even fewer loads).

Ok, yes, I do see this reproducing sometimes now. It didn’t the first couple of times I tried. 🫠 Doing a bit of debugging with DEBUG=*…

When project resolution succeeds, some relevant log lines:

  typescript-eslint:typescript-estree:createProjectService Creating project service with: { defaultProject: 'tsconfig.json', allowDefaultProject: [ 'vitest.config.ts' ] } +0ms
  ...
  typescript-eslint:typescript-estree:tsserver:info     FileName: /Users/josh/repos/eslint-9.12.0-allow-default-project-bug/vitest.config.ts ProjectRootPath: /Users/josh/repos/eslint-9.12.0-allow-default-project-bug +0ms
  typescript-eslint:typescript-estree:tsserver:info             Projects: /dev/null/inferredProject1* +0ms
  typescript-eslint:typescript-estree:useProgramFromProjectService Result from attempting to open client file: { configFileName: undefined, configFileErrors: undefined } +183ms
  typescript-eslint:typescript-estree:useProgramFromProjectService Default project allowed path: true, based on config file: undefined +0ms

When it fails:

  typescript-eslint:typescript-estree:createProjectService Creating project service with: { defaultProject: 'tsconfig.json' } +0ms
  ...
  typescript-eslint:typescript-estree:tsserver:info     FileName: /Users/josh/repos/eslint-9.12.0-allow-default-project-bug/vitest.config.ts ProjectRootPath: /Users/josh/repos/eslint-9.12.0-allow-default-project-bug +0ms
  typescript-eslint:typescript-estree:tsserver:info             Projects: /dev/null/inferredProject1* +0ms
  typescript-eslint:typescript-estree:useProgramFromProjectService Result from attempting to open client file: { configFileName: undefined, configFileErrors: undefined } +140ms
  typescript-eslint:typescript-estree:useProgramFromProjectService Default project allowed path: false, based on config file: undefined +0ms

Going up, I see that when createParseSettings is first called with db/bar.ts, we do see the error. But when it’s first called with vitest.config.ts then we don’t see the error. So, yes, it looks like it’s nondeterministic based on which of the two config blocks happens to hit the file first.

A root reason for this is that we create a single project service once per lint run: https://github.com/typescript-eslint/typescript-eslint/blob/8d35958494eb0c97cc36c3fc0844aea377542315/packages/typescript-estree/src/parseSettings/createParseSettings.ts#L110-L120

We would love to have more intelligent project service creation, but can’t because ESLint core doesn’t tell parsers such as @typescript-eslint/parser (which is what calls to @typescript-eslint/typescript-estree) session information. We asked for this in https://github.com/eslint/rfcs/pull/102 but it was not accepted.

In the meantime, I’m not sure the best way to do this. Maybe we could, in createParseSettings:

  1. Detect if tsestreeOptions.projectService has changed (like a deep equality check)
  2. If they are different, depending on whether it’s reasonable to have multiple project services in existence:
    • If it’s ok: have a Map hashing project service settings to project services
    • If it’s not ok: throw an error

cc @bradzacher @jakebailey - I suspect we’d have to do some experimentation to see how bad it is to have multiple project services floating around. My hunch is that, no, that’s really bad for memory in larger projects and shouldn’t be allowed.

It’s non-deterministic. For me it fails ~80% of the time with yarn eslint . (4 files), or ~20% of the time with yarn eslint vitest.config.ts db/bar.ts (2 files).