From dc28e18dc5ea3d839056a132a1c06beae0bab379 Mon Sep 17 00:00:00 2001 From: Luc Varoqui Date: Thu, 26 Nov 2026 15:59:05 +0100 Subject: [PATCH] fix(core): find the git root of ES module benchmarks from their file getCallingFile passed the stack trace file name to getGitDir as is. For ES modules, that name is a file:// URL, which path.resolve treats as a relative path under the current directory. The lookup then found the repository of the current directory instead of the one of the benchmark file: - run from another repository, the URI started with `../`, which breaks the benchmark page and the flame graph - run from a directory outside any repository, it threw "Could not find a git repository" The URL is now converted to a path before the lookup, so ES modules behave like CommonJS and the Vitest plugin. URIs of ES module benchmarks change when the benchmark file is in another repository than the current directory, including a submodule or nested repository run from the outer one. Refs COD-4223 Co-Authored-By: Claude --- packages/core/src/utils.ts | 24 +++++++++--- packages/core/tests/utils.integ.test.ts | 52 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 packages/core/tests/utils.integ.test.ts diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index a4441dbc..b66c5c8f 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -30,15 +30,27 @@ export function getGitDir(from: string): string | undefined { */ export function getCallingFile(depth: number): string { const stack = getStackTrace(); - let callingFile = stack[depth + 1].getFileName(); - const gitDir = getGitDir(callingFile); + return getGitRelativePath(stack[depth + 1].getFileName()); +} + +/** + * Returns `fileName`, a path or a `file://` URL (ES modules), relative to the + * root of the git repository that contains it. + * + * It throws when the file is in no git repository. + */ +export function getGitRelativePath(fileName: string): string { + // Converts the URL first: as a path, a URL resolves against the current + // directory, so the search would find the repository of the current + // directory instead of the one of the file. + const filePath = fileName.startsWith("file://") + ? fileURLToPath(fileName) + : fileName; + const gitDir = getGitDir(filePath); if (gitDir === undefined) { throw new Error("Could not find a git repository"); } - if (callingFile.startsWith("file://")) { - callingFile = fileURLToPath(callingFile); - } - return path.relative(gitDir, callingFile); + return path.relative(gitDir, filePath); } /** diff --git a/packages/core/tests/utils.integ.test.ts b/packages/core/tests/utils.integ.test.ts new file mode 100644 index 00000000..a8a309d8 --- /dev/null +++ b/packages/core/tests/utils.integ.test.ts @@ -0,0 +1,52 @@ +import { mkdirSync, mkdtempSync, realpathSync, rmSync } from "fs"; +import { tmpdir } from "os"; +import path from "path"; +import { pathToFileURL } from "url"; + +import { getGitRelativePath } from "../src/utils"; + +// Replaces the ES-only `stack-trace`, which Jest cannot load and these tests +// do not use. +jest.mock("stack-trace", () => ({ get: jest.fn() })); + +describe("getGitRelativePath", () => { + let root: string; + let cwd: string; + + // Creates `/bench-repo` (holds the benchmark) and `/other-repo` + // (the current directory), each with its own `.git`. + beforeEach(() => { + root = realpathSync(mkdtempSync(path.join(tmpdir(), "codspeed-git-"))); + for (const repo of ["bench-repo", "other-repo"]) { + mkdirSync(path.join(root, repo, ".git"), { recursive: true }); + } + mkdirSync(path.join(root, "bench-repo", "benches")); + mkdirSync(path.join(root, "no-repo")); + cwd = process.cwd(); + process.chdir(path.join(root, "other-repo")); + }); + + afterEach(() => { + process.chdir(cwd); + rmSync(root, { recursive: true, force: true }); + }); + + it("uses the repository of the file for a path", () => { + const file = path.join(root, "bench-repo", "benches", "a.bench.js"); + expect(getGitRelativePath(file)).toBe("benches/a.bench.js"); + }); + + it("uses the repository of the file for a file URL", () => { + const file = path.join(root, "bench-repo", "benches", "a.bench.mjs"); + expect(getGitRelativePath(pathToFileURL(file).href)).toBe( + "benches/a.bench.mjs", + ); + }); + + it("throws when the file is in no repository", () => { + const file = path.join(root, "no-repo", "a.bench.mjs"); + expect(() => getGitRelativePath(pathToFileURL(file).href)).toThrow( + "Could not find a git repository", + ); + }); +});