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
- clone the repo
- Switch to the
broken-versionbranch npm installnpm 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
- fix: pin typescript-eslint due to https://github.com/typescript-eslint/typescript-eslint/issues/11398 — committed to NaturalCycles/js-libs by kirillgroshkov a year ago
^ 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:
Here’s what should be inferred:
Here’s what is actually being inferred:
#11406 won’t fix this issue, as it only fixes for the trailing pash bug (#11407). The problem is in the regular expression:
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 likeat 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: trueissue 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)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 theproject: truetrip 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.0totypescript-eslint:8.37.0.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:
pnpm installpnpm run linttypescript-eslint: 8.36.0pnpm installpnpm run lint(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.
It works. I would hazard to say
import.meta.dirnameor__dirnamewould not work in my case. If a base configuration usesimport.meta.dirnameand 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 useprocess.cwd(), specially when is the way to recreate the8.36.0behavior which was working for the users watching this issue.No 🙂. Maybe the bug can be triggered by multiple different causes, such as cwd differences? Thanks for noting - I’ve renamed the issue.
We generally recommend
import.meta.dirname(or__dirnamefor 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
tsconfigRootDiris no longer the current working directory of the process, but the result of a call to a newgetInferredTSConfigRootDirmethod.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
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 inferProjects/monorepo/src/apps/node-server/tsconfig.jsoninstead).For a solution/workaround that should always work, one can always explicitly specify the
tsconfigRootDirinparserOptions. Without an explicitly specifiedtsconfigRootDir, 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 notsconfigRootDiris specified (but ofc has some kind of a mistake as noted about path concatenation)