-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(typescript-estree): infer tsconfigRootDir from call stack #11370
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
Merged
JoshuaKGoldberg
merged 17 commits into
typescript-eslint:main
from
JoshuaKGoldberg:infer-tsconfigrootdir-candidates
Jul 14, 2025
+275
−3
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f368171
feat(typescript-estree): infer tsconfigRootDir from call stack
JoshuaKGoldberg 88edcc3
rename
JoshuaKGoldberg c42ce4c
Adjust for Windows
JoshuaKGoldberg 2856485
lint
JoshuaKGoldberg dc37428
Merge branch 'main' into infer-tsconfigrootdir-candidates
JoshuaKGoldberg 5474282
Unit tested the candidate detection
JoshuaKGoldberg 4d7aae9
mv
JoshuaKGoldberg cecf62e
Merge branch 'main' into infer-tsconfigrootdir-candidates
JoshuaKGoldberg 3a94f40
rm packages/website/data/recent-blog-posts.json
JoshuaKGoldberg 5718708
chore: have website lint depend on its build
JoshuaKGoldberg 53cbcb1
Merge branch 'main' into infer-tsconfigrootdir-candidates
JoshuaKGoldberg 9266c7d
Add file:/// testing back
JoshuaKGoldberg 7c0a5fc
Add doc link
JoshuaKGoldberg 13879b7
dep
JoshuaKGoldberg 5ee0eaf
Merge branch 'main'
JoshuaKGoldberg 37d822b
Heh, wrong paackage
JoshuaKGoldberg 718b881
yarn test -u
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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]; | ||
if (!candidate) { | ||
continue; | ||
} | ||
|
||
return candidate.startsWith('file://') | ||
? fileURLToPath(candidate) | ||
: candidate; | ||
} | ||
|
||
return undefined; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/typescript-eslint/tests/getTsconfigRootDirFromStack.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import path from 'node:path'; | ||
|
||
import { getTSConfigRootDirFromStack } from '../src/getTSConfigRootDirFromStack'; | ||
|
||
const isWindows = process.platform === 'win32'; | ||
|
||
describe(getTSConfigRootDirFromStack, () => { | ||
it('returns undefined when no file path seems to be an ESLint config', () => { | ||
const actual = getTSConfigRootDirFromStack( | ||
[ | ||
`Error`, | ||
' at file:///other.config.js', | ||
' at ModuleJob.run', | ||
'at async NodeHfs.walk(...)', | ||
].join('\n'), | ||
); | ||
|
||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it.runIf(!isWindows)( | ||
'returns a Posix config file path when a file:// path to an ESLint config is in the stack', | ||
() => { | ||
const actual = getTSConfigRootDirFromStack( | ||
[ | ||
`Error`, | ||
' at file:///path/to/file/eslint.config.js', | ||
' at ModuleJob.run', | ||
'at async NodeHfs.walk(...)', | ||
].join('\n'), | ||
); | ||
|
||
expect(actual).toBe('/path/to/file/'); | ||
}, | ||
); | ||
|
||
it.each(['cjs', 'cts', 'js', 'mjs', 'mts', 'ts'])( | ||
'returns the path to the config file when its extension is %s', | ||
extension => { | ||
const expected = isWindows ? 'C:\\path\\to\\file\\' : '/path/to/file/'; | ||
|
||
const actual = getTSConfigRootDirFromStack( | ||
[ | ||
`Error`, | ||
` at ${path.join(expected, `eslint.config.${extension}`)}`, | ||
' at ModuleJob.run', | ||
'at async NodeHfs.walk(...)', | ||
].join('\n'), | ||
); | ||
|
||
expect(actual).toBe(expected); | ||
}, | ||
); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/typescript-estree/src/parseSettings/candidateTSConfigRootDirs.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const candidateTSConfigRootDirs = new Set<string>(); | ||
|
||
export function addCandidateTSConfigRootDir(candidate: string): void { | ||
candidateTSConfigRootDirs.add(candidate); | ||
} | ||
|
||
export function clearCandidateTSConfigRootDirs(): void { | ||
candidateTSConfigRootDirs.clear(); | ||
} | ||
|
||
export function getInferredTSConfigRootDir(): string { | ||
const entries = [...candidateTSConfigRootDirs]; | ||
|
||
switch (entries.length) { | ||
case 0: | ||
return process.cwd(); | ||
|
||
case 1: | ||
return entries[0]; | ||
|
||
default: | ||
throw new Error( | ||
[ | ||
'No tsconfigRootDir was set, and multiple candidate TSConfigRootDirs are present:', | ||
...entries.map(candidate => ` - ${candidate}`), | ||
"You'll need to explicitly set tsconfigRootDir in your parser options.", | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'See: https://typescript-eslint.io/packages/parser/#tsconfigrootdir', | ||
].join('\n'), | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/typescript-estree/tests/lib/candidateTSConfigRootDirs.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { addCandidateTSConfigRootDir } from '../../src'; | ||
import { | ||
clearCandidateTSConfigRootDirs, | ||
getInferredTSConfigRootDir, | ||
} from '../../src/parseSettings/candidateTSConfigRootDirs'; | ||
|
||
describe(getInferredTSConfigRootDir, () => { | ||
beforeEach(() => { | ||
clearCandidateTSConfigRootDirs(); | ||
}); | ||
|
||
it('returns process.cwd() when there are no candidates', () => { | ||
const actual = getInferredTSConfigRootDir(); | ||
|
||
expect(actual).toBe(process.cwd()); | ||
}); | ||
|
||
it('returns the candidate when there is one candidate', () => { | ||
const candidate = 'a/b/c'; | ||
addCandidateTSConfigRootDir(candidate); | ||
|
||
const actual = getInferredTSConfigRootDir(); | ||
|
||
expect(actual).toBe(candidate); | ||
}); | ||
|
||
it('throws an error when there are multiple candidates', () => { | ||
addCandidateTSConfigRootDir('a'); | ||
addCandidateTSConfigRootDir('b'); | ||
|
||
expect(() => getInferredTSConfigRootDir()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
[Error: No tsconfigRootDir was set, and multiple candidate TSConfigRootDirs are present: | ||
- a | ||
- b | ||
You'll need to explicitly set tsconfigRootDir in your parser options. | ||
See: https://typescript-eslint.io/packages/parser/#tsconfigrootdir] | ||
`); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this adds dependency on a transitive dep -- which will break consumers.
the dep needs to be listed in the
package.json
there's something with our lint config -- there should be like
import/no-extraneous-deps
or something that's covering this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
import-x
would have caught this 😄