typescript-eslint: Bug: Inferred parserOptions.tsconfigRootDir concatenates incorrectly when a space is present in the path

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 believe this issue may have something to do with: https://github.com/typescript-eslint/typescript-eslint/pull/11370

This was working in version 8.36.0

This is the line that I think is breaking things

Here is my stacktrace:

npm run lint

> monorepo@0.0.1 lint
> turbo run lint

turbo 2.5.4

• Packages in scope: @configs/eslint-config, @configs/ts-config, @configs/vite-config, @utils/type-utils, monorepo-cdk, node-server
• Running lint in 6 packages
• Remote caching disabled
monorepo-cdk:lint: cache miss, executing c976c864fcf47ec0
node-server:lint: cache miss, executing cf188dbd4300770e
monorepo-cdk:lint: 
node-server:lint: 
node-server:lint: 
node-server:lint: > node-server@0.0.1 lint
node-server:lint: > eslint src
node-server:lint: 
monorepo-cdk:lint: 
monorepo-cdk:lint: > monorepo-cdk@0.0.1 lint
monorepo-cdk:lint: > eslint src
monorepo-cdk:lint: 

node-server:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server/src/index.ts
node-server:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/apps/node-server/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server/Projects/monorepo/src/apps/node-server/tsconfig.json'
node-server:lint: 
node-server:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server/src/vite-env.d.ts
node-server:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/apps/node-server/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server/Projects/monorepo/src/apps/node-server/tsconfig.json'
node-server:lint: 
node-server:lint: ✖ 2 problems (2 errors, 0 warnings)
node-server:lint: 

monorepo-cdk:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/src/constants/backend.ts
monorepo-cdk:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json'
monorepo-cdk:lint: 
monorepo-cdk:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/src/index.ts
monorepo-cdk:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json'
monorepo-cdk:lint: 
monorepo-cdk:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/src/stacks.ts
monorepo-cdk:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json'
monorepo-cdk:lint: 
monorepo-cdk:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/src/stacks/bootstrap/index.ts
monorepo-cdk:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json'
monorepo-cdk:lint: 
monorepo-cdk:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/src/stacks/my-stack/index.ts
monorepo-cdk:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json'
monorepo-cdk:lint: 
monorepo-cdk:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/src/types/stack.ts
monorepo-cdk:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json'
monorepo-cdk:lint: 
monorepo-cdk:lint: /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/src/utils/standard-backend.ts
monorepo-cdk:lint:   0:0  error  Parsing error: error TS5012: Cannot read file 'Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json': ENOENT: no such file or directory, open '/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk/Projects/monorepo/src/cdk/monorepo-cdk/tsconfig.json'
monorepo-cdk:lint: 
monorepo-cdk:lint: ✖ 7 problems (7 errors, 0 warnings)
monorepo-cdk:lint: 
npm error Lifecycle script `lint` failed with error:
npm error code 1
npm error path /Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server
npm error Lifecycle script `lint` failed with error:
npm error workspace node-server@0.0.1
npm error location /Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server
npm error command failed
npm error command sh -c eslint src
npm error code 1
npm error path /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk
npm error workspace monorepo-cdk@0.0.1
npm error location /Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk
npm error command failed
npm error command sh -c eslint src


monorepo-cdk:lint: ERROR: command finished with error: command (/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk) /Users/matthewlin/.nvm/versions/node/v22.14.0/bin/npm run lint exited (1)
node-server:lint: ERROR: command finished with error: command (/Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server) /Users/matthewlin/.nvm/versions/node/v22.14.0/bin/npm run lint exited (1)
monorepo-cdk#lint: command (/Users/matthewlin/Desktop/Personal Projects/monorepo/src/cdk/monorepo-cdk) /Users/matthewlin/.nvm/versions/node/v22.14.0/bin/npm run lint exited (1)
node-server#lint: command (/Users/matthewlin/Desktop/Personal Projects/monorepo/src/apps/node-server) /Users/matthewlin/.nvm/versions/node/v22.14.0/bin/npm run lint exited (1)

My

Reproduction Repository Link

https://github.com/matthew-plusprogramming/monorepo/tree/broken-version/packages/eslint-config

Repro Steps

  1. clone the repo
  2. Switch to the broken-version branch
  3. npm install
  4. npm run lint

Versions

package version
@typescript-eslint/eslint-plugin 8.37.0
@typescript-eslint/parser 8.37.0
TypeScript 5.8.3
ESLint 9.31.0
node 22.14.0

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 10
  • Comments: 27 (11 by maintainers)

Commits related to this issue

Most upvoted comments

^ Added minimal reproduction project.

Our project in company is theoretically affected by this. We have a good internal Renovate setup and we are 100% sure that we can’t update from 8.36 to 8.37 right now.

Anyway, I can confirm locally that the regular expression from #11370 does incorrectly handle spaces. Here’s a part of an example stack from https://github.com/matthew-plusprogramming/monorepo/tree/broken-version for me locally:

Error\n' +
    '    at Object.get [as recommended] (/Users/josh/repos/with space/monorepo/node_modules/typescript-eslint/dist/index.js:116:9)\n' +
    '    at /Users/josh/repos/with space/monorepo/packages/eslint-config/index.ts:37:38\n' +
    '    at async import (/Users/josh/repos/with space/monorepo/node_modules/jiti/dist/jiti.cjs:1:199772)\n' +
    '    at async /Users/josh/repos/with space/monorepo/cdk/monorepo-cdk/eslint.config.ts:1:217\n' +
    '    at async Function.import (/Users/josh/repos/with space/monorepo/node_modules/jiti/dist/jiti.cjs:1:199772)\n' +

Here’s what should be inferred:

/Users/josh/repos/with space/monorepo/cdk/monorepo-cdk

Here’s what is actually being inferred:

space/monorepo/cdk/monorepo-cdk

#11406 won’t fix this issue, as it only fixes for the trailing pash bug (#11407). The problem is in the regular expression:

/(\S+?)[/\\]+eslint\.config\.(c|m)?(j|t)s/

That (\S+?) captures non-space characters. Which works fine in paths that don’t have spaces. But, clearly, not for paths with spaces. We could fix this quickly by allowing spaces and filtering out anything like at async. But long-term that’s messy. @kirkwaiblinger I think you’re right that we should use something more reliable than error stack parsing (Discord discussion).

Confirmed that the tsconfig root dir inference fails for paths with a space as well, so I spun the project: true issue in https://github.com/typescript-eslint/typescript-eslint/issues/11398#issuecomment-3084365219 into a new issue, #11407.

Retargeting this one to be just about paths with spaces in them again, and let’s spin off new issues for anything we confirm to be something else.

Confirmed that the following diff (removing trailing slash from inferred tsconfigRootDir)

diff --git a/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts b/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts
index 01378b161..3348c1cc9 100644
--- a/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts
+++ b/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url';
 
 export function getTSConfigRootDirFromStack(stack: string): string | undefined {
   for (const line of stack.split('\n').map(line => line.trim())) {
-    const candidate = /(\S+)eslint\.config\.(c|m)?(j|t)s/.exec(line)?.[1];
+    const candidate = /(\S+)\/eslint\.config\.(c|m)?(j|t)s/.exec(line)?.[1];
     if (!candidate) {
       continue;
     }

removes the error from the reproduction in https://github.com/kirkwaiblinger/repro-tseslint-11398 (on MacOS; on Windows I expect we’d need to handle \\). So that’s a fixable error. (side note - should the project: true trip up over a trailing path separator or should it interpret the same as without?)

I’m getting the following error when upgrading from typescript-eslint:8.36.0to typescript-eslint:8.37.0.

Parsing error: project was set to `true` but couldn't find any tsconfig.json relative to '~\git\project_frontend\src\api\types\partner.ts' within '~/git/project_frontend/'

For every TS and vue file within my project. Also IMO it is strange that the first path uses \ while the second one uses /. ~ used to hide home dir.

Is that related to this issue? I don’t use a custom parserOptions.tsconfigRootDir though.

I’m currently trying to build a reproduction.

Reproduction

eslint-reproduction.zip (748 bytes when deleting pnpm-lock.json)

Steps:

  • Extract zip
  • pnpm install
  • pnpm run lint
  • Error
  • Downgrade to typescript-eslint: 8.36.0
  • pnpm install
  • pnpm run lint
  • No Error

(There also isn’t an error when the linted file is next to the tsconfig instead of inside src)

100% reproducible for me (Windows)

Hey @JoshuaKGoldberg, thanks for your quick reply.

We generally recommend import.meta.dirname (or __dirname for CJS). Users might not be running ESLint from the same directory as the config file. But if that works for you, great!

It works. I would hazard to say import.meta.dirname or __dirname would not work in my case. If a base configuration uses import.meta.dirname and is located in a different directory than the expected one, this approach is not the way to go. For such a reason I’m more eager to use process.cwd(), specially when is the way to recreate the 8.36.0 behavior which was working for the users watching this issue.

Are we sure the root issue is an space in the path?

No 🙂. Maybe the bug can be triggered by multiple different causes, such as cwd differences? Thanks for noting - I’ve renamed the issue.

tsconfigRootDir: process.cwd()

We generally recommend import.meta.dirname (or __dirname for CJS). Users might not be running ESLint from the same directory as the config file. But if that works for you, great!

Are we sure the root issue is an space in the path? I don’t think so. As an example, this CI run fails without any space being involved in the path.

After having a look at your commit history, this change caught my attention. It seems tsconfigRootDir is no longer the current working directory of the process, but the result of a call to a new getInferredTSConfigRootDir method.

If, as pointed out by @kirkwaiblinger, there’s any bug involving that function, a concrete workaroung could be recreating the previous version behavior:

eslint.config.mjs

export default {
  extends: [
     eslint.configs.recommended,
     ...tseslint.configs.strictTypeChecked,
     // Your extensions
   ],
   languageOptions: {
     parser: tseslint.parser,
     parserOptions: {
       project: './tsconfig.json',
       tsconfigRootDir: process.cwd(), // <-- We set tsconfigRootDir to process.cwd() to recreate 8.36.0 behavior
     },
   },
   plugins: {
     '@typescript-eslint': tseslint.plugin,
     // your plugins
   },
   rules: {
     // your rules
   }
}

This workaround worked like a charm in all the repos I tried. I’m really looking forward for a fix to this bug though.

Oh got it! Thanks for the help I’ll try that out later today

Looking at the path in the error message (Projects/monorepo/src/apps/node-server/Projects/monorepo/src/apps/node-server/tsconfig.json) it looks like there’s a mistake in the path concatenation/resolution (should probably infer Projects/monorepo/src/apps/node-server/tsconfig.json instead).

For a solution/workaround that should always work, one can always explicitly specify the tsconfigRootDir in parserOptions. Without an explicitly specified tsconfigRootDir, we really have no consistent way to specify a relative path to a tsconfig, and are reduced to making a best guess as to where to look (see more info at https://github.com/typescript-eslint/typescript-eslint/issues/10841). The change in #11370 was trying to make a more clever guess as to where to look for a tsconfig when no tsconfigRootDir is specified (but ofc has some kind of a mistake as noted about path concatenation)