Skip to content

fix(typescript-eslint): handle file:// urls in stack trace when inferring tsconfigRootDir #11464

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"\\(#.+?\\)"
],
"words": [
"AFAICT",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it should be in the cspell dictionary 😂

"Airbnb",
"Airbnb's",
"allowdefaultproject",
Expand Down
12 changes: 10 additions & 2 deletions packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';

/**
* Infers the `tsconfigRootDir` from the current call stack, using the V8 API.
Expand Down Expand Up @@ -26,11 +27,18 @@ export function getTSConfigRootDirFromStack(): string | undefined {
}

for (const callSite of getStack()) {
const stackFrameFilePath = callSite.getFileName();
if (!stackFrameFilePath) {
const stackFrameFilePathOrUrl = callSite.getFileName();
if (!stackFrameFilePathOrUrl) {
continue;
}

// ESM seem to return a file URL, so we'll convert it to a file path.
// AFAICT this isn't documented in the v8 API docs, but it seems to be the case.
// See https://github.com/typescript-eslint/typescript-eslint/issues/11429
const stackFrameFilePath = stackFrameFilePathOrUrl.startsWith('file://')
? fileURLToPath(stackFrameFilePathOrUrl)
: stackFrameFilePathOrUrl;

const parsedPath = path.parse(stackFrameFilePath);
if (/^eslint\.config\.(c|m)?(j|t)s$/.test(parsedPath.base)) {
return parsedPath.dir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@ import * as normalFolder from './path-test-fixtures/tsconfigRootDirInference-nor
import * as notEslintConfig from './path-test-fixtures/tsconfigRootDirInference-not-eslint-config/not-an-eslint.config.cjs';
import * as folderThatHasASpace from './path-test-fixtures/tsconfigRootDirInference-space/folder that has a space/eslint.config.cjs';

const isWindows = process.platform === 'win32';

describe(getTSConfigRootDirFromStack, () => {
it('does stack analysis right for normal folder', () => {
expect(normalFolder.get()).toBe(normalFolder.dirname());
});

it('does stack analysis right for a file that gives a file:// URL as its name', () => {
vi.spyOn(Error, 'captureStackTrace').mockImplementationOnce(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(target: any, _constructorOpt) => {
target.stack = [
{
getFileName() {
return !isWindows
? 'file:///a/b/eslint.config.mts'
: 'file:///F:/a/b/eslint.config.mts';
},
},
];
},
);

const inferredTsconfigRootDir = getTSConfigRootDirFromStack();

expect(inferredTsconfigRootDir).toBe(!isWindows ? '/a/b' : 'F:\\a\\b');
});

it('does stack analysis right for folder that has a space', () => {
expect(folderThatHasASpace.get()).toBe(folderThatHasASpace.dirname());
});
Expand Down
Loading