typescript-eslint: Bug: `tsconfig.json` not respected in new files when multiple tsconfigs include the same directory with `projectService` enabled

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

When EXPERIMENTAL_useProjectService: true and at least two tsconfig.jsons include a non-empty common directory, typescript-eslint will not respect tsconfig.json for new/renamed files until ESLint is reloaded.

Possibly related: #8206 #7435

Reproduction Repository Link

https://github.com/fire332/typescript-eslint-bug-repro/

Repro Steps

  1. clone the repo
  2. pnpm install
  3. open project in VSCode with the ESLint extension installed
  4. IMPORTANT: open .prettierrc.js or eslint.config.js and wait for ESLint to initialize
  5. create a new .ts file in ./src or ./src-alt
  6. problems reported will show:
This rule requires the `strictNullChecks` compiler option to be turned on to function correctly. eslint(@typescript-eslint/no-unnecessary-condition)
  1. run Developer: Reload Window from the command palette
  2. check the file you created and note there’s no problems reported
  3. rename the file and note that the same problem will be reported again

Versions

package version
typescript-eslint 7.5.0
TypeScript 5.4.3
ESLint 8.57.0
node >=20.11.1

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 6
  • Comments: 19 (13 by maintainers)

Most upvoted comments

If anyone wants to test, I’ve moved to this branch: https://github.com/higherorderfunctor/typescript-eslint/tree/patches2upstream. There are a lot of modifications, so I’ll work to get these up-streamed over several PRs. I’ve confirmed the strictNullChecks issue is still fixed for the scenario I was able to reproduce. I’m still on eslint@8.57.0 for reference as there is one plugin I use that doesn’t work with FlatCompat in eslint@9 (eslint-config-airbnb-base/whitespace).

I’ve included the types and typescript-estree builds in the repo if your package manager supports installing from git. My package.json looks like below with pnpm@9.1.x. The patch is for https://github.com/typescript-eslint/typescript-eslint/issues/9223 until those fixes make it into eslint-plugin-import-x.

package.json
{
  ...
  "pnpm": {
    "patchedDependencies": {
      "eslint-plugin-import-x@0.5.1": "patches/eslint-plugin-import-x@0.5.1.patch",
    },
    // forces dependencies to use the same version
    "overrides": {
      "typescript": "$typescript",
      "typescript-eslint": "$typescript-eslint",
      "@typescript-eslint/eslint-plugin": "$typescript-eslint",
      "@typescript-eslint/parser": "$typescript-eslint",
      "@typescript-eslint/rule-schema-to-typescript-types": "$typescript-eslint",
      "@typescript-eslint/scope-manager": "$typescript-eslint",
      "@typescript-eslint/type-utils": "$typescript-eslint",
      "@typescript-eslint/utils": "$typescript-eslint",
      "@typescript-eslint/visitor-keys": "$typescript-eslint",
      // my custom fork
      "@typescript-eslint/types": "higherorderfunctor/typescript-eslint#patches2upstream&path:packages/types",
      "@typescript-eslint/typescript-estree": "higherorderfunctor/typescript-eslint#patches2upstream&path:packages/typescript-estree"
    }

  },
  "devDependencies": {
    "eslint": "^8.57.0",
    "typescript": "rc",
    "typescript-eslint": "rc-v8",
    // just for reference on what I have tested against
    "@eslint/eslintrc": "^3.1.0",
    "@eslint/js": "^8.57.0",
    "@stylistic/eslint-plugin": "^2.1.0",
    "@types/eslint": "^8.56.10",
    "@typescript-eslint/utils": "rc-v8", // uses some types in my eslint.config.js for type checking
    "@vue/eslint-config-prettier": "^9.0.0",
    "@vue/eslint-config-typescript": "^13.0.0",
    "eslint-config-airbnb-base": "^15.0.0",
    "eslint-config-airbnb-typescript": "^18.0.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-import-resolver-alias": "^1.1.2",
    "eslint-import-resolver-typescript": "^3.6.1",
    "eslint-plugin-codegen": "0.28.0",
    "eslint-plugin-deprecation": "^3.0.0",
    "eslint-plugin-import-x": "^0.5.1",
    "eslint-plugin-jsonc": "^2.16.0",
    "eslint-plugin-markdown": "^5.0.0",
    "eslint-plugin-prefer-arrow-functions": "^3.3.2",
    "eslint-plugin-prettier": "^5.1.3",
    "eslint-plugin-promise": "^6.2.0",
    "eslint-plugin-security": "^3.0.0",
    "eslint-plugin-simple-import-sort": "^12.1.0",
    "eslint-plugin-sonar": "^0.14.1",
    "eslint-plugin-sonarjs": "^1.0.3",
    "eslint-plugin-sort-destructure-keys": "^2.0.0",
    "eslint-plugin-tsdoc": "^0.3.0",
    "eslint-plugin-vue": "^9.26.0",
    "eslint-plugin-vuejs-accessibility": "^2.3.0",
    "eslint-plugin-vuetify": "^2.4.0",
    "eslint_d": "^13.1.2",
    "jsonc-eslint-parser": "^2.4.0",
    "vue-eslint-parser": "^9.4.3",
    ...
  },
  ...
}

My RAM utilization is way down today… not sure if it is something I did, something upstream in the rc-v8 branch, or something in the latest typescript@rc, but linting my whole project now is down to ~2min and RAM is slightly over 3G. I had my heap up to 9G a couple days ago or it would crash.

        Elapsed (wall clock) time (h:mm:ss or m:ss): 2:04.05
        Maximum resident set size (kbytes): 3235328

If you do get OOM, the heap can be increased.

NODE_OPTIONS="--max-old-space-size=8184" eslint ...

Known Issues

With the flat config and extraFileExtensions, it is best to set it once if possible. I had different projectService settings between my front-end and back-end. Changing the extraFileExtensions mid-lint causes tsserver to do a full project reload. I have guards in place to do it, but it will cause a performance hit. I originally only set it once and was wondering why I was having issue with .vue files intermittently. Was caused by which projectService happened to be loaded first.

Additional Options

I’ve added two new options with their defaults below. incremental only sends deltas to tsserver which I am hoping makes the LSP experience better. maximumOpenFiles will automatically close files with tsserver, which is nice on the CLI which has no mechanism to know when it is done with a file. The new options are why the types package is needed if you enable typing on your eslint.config.js.

{
  languageOptions: {
    parserOptions: {
      projectService: {
        incremental: false, // when true, only sends changes to tsserver instead of whole files
        maximumOpenFiles: 16, // auto closes open files with tsserver based on access order
      }
    }
  }
}

Hmm. Interesting. I’ll take another look, thanks.

In case it’s useful, I’m only testing with v8 - as of yesterday, typescript-eslint@8.0.0-alpha.25. We have some improvements to the project service on the v8 line that we don’t plan on backporting to v7.

Yeah, these are a lot of open questions. I really really appreciate your offer to hustle but long-term I don’t think it’s a given that we’d want to try to handle these cases internally (https://github.com/typescript-eslint/typescript-eslint/issues/8835#issuecomment-2254489382). I’m going to go ahead and remove this from the v8 milestone.

Sharing the same project service instance could solve these project structure updates

That’s one of the things I’m very hopeful for with the project service!

@JoshuaKGoldberg I can probably get a minimum viable implementation done by Monday. Basically, is the file in the project service? If no, trigger the best matching watcher. Recheck and if still no add to default project or treat as an isolated file.

This will deal with new source files requiring the LSP to be restarted.

Some of the more complex decision making I don’t think I’d be able to have ready by release day. Examples:

  • Handling changes to tsconfig.
  • Handling changes to package.json and/or node_modules to reload dependencies.
  • Handling changes to deleted or moved files. This shouldn’t impact runtime behavior, but the indexing of watchers would hold refs to non existent files and directories leaking some memory.
  • Looking for potential optimizations. Basic one I could implement is not indexing or tracking file watchers and only indexing directory watchers. File changes are handled by opening the files and sending “editor” updates and not through watchers. That one is easy, but there may be others I haven’t considered.
  • Is this even the best approach long term? Users will most likely be using the tsserver lsp which is doing real watching including tsconfigs and dependencies. Sharing the same project service instance could solve these project structure updates. A lot to unpack with that idea though.

I mentioned this in another thread, but I have a busy week this week in case someone else needs to pick up the PR. Dropping my notes in case anyone else does pick it up.

The working implementation is https://github.com/higherorderfunctor/typescript-eslint/tree/patches2upstream, the draft PR is not currently in a working state.

While I have a working implementation, it duplicates some of the existing logic in getWatchProgramsForProjectsts.ts.

saveWatchCallback looks likely to be a drop in candidate for my implementation.

Exporting or proving an access function for fileWatchCallbackTrackingMap and folderWatchCallbackTrackingMap could allow for the watches to be updated easily. I haven’t analyzed the paths tsserver requests the host to watch. I opted to use a trie so I could find the most specific path. getWatchProgramsForProjectsts.ts just uses a Map, so maybe supporting partial matches is not even needed.

hasTSConfigChanged is probably good to consider since changes to tsconfig.json I don’t believe will reported to the plugin.

maybeInvalidateProgram which calls the above has quite a bit of logic, but I am not sure all of it is needed for project services. I didn’t see a need to do all the caching that is done. Linting results were almost immediate by just walking the service, even on a larger project, and triggering the watcher for the most specific matching path.

const filePathMatchedByConfiguredProject = (
  service: ts.server.ProjectService,
  filePath: string,
): boolean => {
  for (const project of service.configuredProjects.values()) {
    if (project.containsFile(filePath as ts.server.NormalizedPath)) {
      return true;
    }
  }
  return false;
};

So I have a hobbled together set of what I hope are the start of fixes . One note if anyone clones, the package.jsons were modified to use links for in-repo dependencies so I could add the build outputs to my larger work repository also using links but with pnpm.

First, I track watches set by the ProjectService with a new getWatchesForProjectService.ts. Will need to look through getWatchProgramsForProjects.ts to see if there is room to consolidate.

This is used by createProjectService.ts to call the watch constructors via the host and useProgramFromProjectService.ts to trigger the watches when a file has not been seen by the ProjectService. I trigger the most specific watch which fixed the strictNullChecks for me.

I use getScriptInfo and ScriptInfo.editContent to deal with lint requests for files that are already open (e.g., --fix). Some @stylistic rules crash on the AST for vue files without it. I have some commented out caches for open files and programs that produced similar issues. I was hoping to get away with not needing to re-open the file after a fix. There may be some room for improvements, but it would take some more research.

Also noteworthy is that calling service.setHostConfiguration triggers a full project reload for files with irregular extensions like vue. It should probably be set once with ProjectService during construction instead of on every file. Linting a vue package in my work monorepo went from 3m30s to 20s. The whole monorepo I am testing on went from 16m in CI on server hardware to 3m40s on the mini pc I am currently using which is a much slower machine. I’m excited by those numbers and they produced the same results for both static analysis and running with --fix.

I may have a memory leak as I had to increase the heap. Could also just be linting a large monorepo using only a default project withProjectServices without ever closing a file with tsserver. Again, some more research is needed to cut that memory usage down.

edit: Been looking through the heap. I’m thinking it might be “okay”. We use a lot of complex types which is likely inflating the memory usage. The SourceFileObject count is accurate. Most of the memory is taken up by NodeObjects which looks like some low level AST data structure. Should be good enough for an LSP and I’ll just have to bump the heap if I want the speed on a full CLI lint. I did put an LRU in to limit the number of opened files. Not really seeing a benefit on the CLI where it just churns through everything quickly.

I ran into the same warning but with a different setup.

This rule requires the strictNullChecks compiler option to be turned on to function correctly

I don’t use EXPERIMENTAL_useProjectService: true but there may be a common root cause. If I use --fix I get the warning, if I don’t use --fix I don’t get that warning.

I did some debugging. It looks like typescript-estree creates two different “programs” for the same file internally, because --fix uses multiple cycles:

https://github.com/typescript-eslint/typescript-eslint/blob/63e53e28f68f84764ec632d502da3d33095f6a0d/packages/typescript-estree/src/parser.ts#L235-L253

The first program is created through getProgramAndAST, the second one using createIsolatedProgram.

The first cycle uses getProgramAndAST which in turn uses createProgramFromConfigFile to load the correct tsconfig.json and pass the options to createProgram: https://github.com/typescript-eslint/typescript-eslint/blob/63e53e28f68f84764ec632d502da3d33095f6a0d/packages/typescript-estree/src/create-program/useProvidedPrograms.ts#L55-L89

The second cycle uses createIsolatedProgram which completely ignores tsconfig.json and uses pre-defined options: https://github.com/typescript-eslint/typescript-eslint/blob/63e53e28f68f84764ec632d502da3d33095f6a0d/packages/typescript-estree/src/create-program/createIsolatedProgram.ts#L64-L74

That in turn explains the warning because strictNullChecks is indeed false in that case.

Since my problem is caused by the same file being compiled multiple times due to --fix, it may as well be related to this issue where the same file is compiled multiple times.

Did some log analysis to see if I could find the inflection point between the original file and a copied file as I am able to reproduce on a large repo. It did not work on a small example repo. The last line is where the difference occurs.

Original File

typescript-eslint:typescript-estree:useProgramFromProjectService Opening project service file for: <REDACTED>/packages/project-a/src/stores/auth.ts at absolute path <REDACTED>/packages/project-a/src/stores/auth.ts
typescript-eslint:tsserver INFO: 'Search path: <REDACTED>/packages/project-a/src/stores'
typescript-eslint:tsserver INFO: 'For info: <REDACTED>/packages/project-a/src/stores/auth.ts :: Config file name: <REDACTED>/packages/project-a/tsconfig.json'
typescript-eslint:tsserver INFO: 'Creating configuration project <REDACTED>/packages/project-a/tsconfig.json'

This log message happens here: https://github.com/microsoft/TypeScript/blob/27bcd4cb5a98bce46c9cdd749752703ead021a4b/src/server/editorServices.ts#L2462

Copied File

typescript-eslint:typescript-estree:useProgramFromProjectService Opening project service file for: <REDACTED>/packages/project-a/src/stores/auth2.ts at absolute path <REDACTED>/packages/project-a/src/stores/auth2.ts
typescript-eslint:tsserver INFO: 'Search path: <REDACTED>/packages/project-a/src/stores'
typescript-eslint:tsserver INFO: 'For info: <REDACTED>/packages/project-a/src/stores/auth2.ts :: Config file name: <REDACTED>/packages/project-a/tsconfig.json'
typescript-eslint:tsserver INFO: 'FileWatcher:: Added:: WatchInfo: <REDACTED>/packages/project-a/src/stores/tsconfig.json 2000 undefined WatchType: Config file for the inferred project root'

That log message is set here when setting up the watch: https://github.com/microsoft/TypeScript/blob/27bcd4cb5a98bce46c9cdd749752703ead021a4b/src/server/editorServices.ts#L2192

I haven’t had the time to do a full code level trace or fire up the debugger yet, but at a glance it looks like tsserver treats the new file as an orphan. Since I cannot reproduce on a small repo, it may suggest a race condition.

Wrapping up for the night I took a shot in the dark at changing this line in the plugin to use getScriptInfoEnsuringProjectsUptoDate instead of getScriptInfo: https://github.com/typescript-eslint/typescript-eslint/blob/c9a6dd9f3df4e3c2bfcc5dceb770b021709152ae/packages/typescript-estree/src/useProgramFromProjectService.ts#L73

It didn’t fix the issue, but this week is also my first venture into the tsserver code, so I may not be using it correctly.

To reproduce the logging this is roughly the code I ended up using at https://github.com/typescript-eslint/typescript-eslint/blob/585423db7dda28c0c7b8efc05a8c244dde6ca92b/packages/typescript-estree/src/create-program/createProjectService.ts#L56

const util = require('util');
const debug = require('debug');
const log = debug('typescript-eslint:tsserver');
function createProjectService(optionsRaw, jsDocParsingMode) {
    const service = new tsserver.server.ProjectService({
        ...
        logger: {
          close: doNothing,
          endGroup: doNothing,
          getLogFileName: () => undefined,
          hasLevel: () => true,
          info: (s) => log(`INFO: %s`, util.inspect(s, false, null, false)),
          loggingEnabled: () => true,
          msg: (s, type) => log(`MSG (%s): %s`, util.inspect(type, false, null, false), util.inspect(s, false, null, false)),
          perftrc: (s) => log(`PERF: %s`, util.inspect(s, false, null, false)),
          startGroup: doNothing,
        },
        ...
    });
    ...
}

I am using neovim but with the vscode eslint LSP. I just started my editor with the DEBUG=* environment variable set to generate the logs which end up in ~/.local/state/nvim/lsp.log. This generates ALOT of data so I wouldn’t recommend leaving it on.