fix(web): respect case in POSIX file links (#9309)

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Michel LiaoandJulius Marminge authored and GitHub committed 2026-09-04 17:27:31 -07:00
1 parent ed2bdbb278
commit 8faf031c2d
5 files changed
+83 -5

No files matched your search

+6
View File
@@ -38,4 +38,10 @@ describe("formatWorkspaceRelativePath", () => {
),
).toBe("t3code/apps/web/src/session-logic.ts:501:9");
});
it("keeps double-slash POSIX paths case-sensitive", () => {
expect(formatWorkspaceRelativePath("//tmp/project/probe.txt", "//tmp/Project")).toBe(
"//tmp/project/probe.txt",
);
});
});
+7 -3
View File
@@ -4,6 +4,7 @@ import {
splitFilePathPosition,
stripSlashPrefixedWindowsDrive,
} from "@t3tools/client-runtime/markdown-links";
import { isWindowsAbsolutePath } from "@t3tools/shared/path";
function normalizePathSeparators(path: string): string {
return path.replaceAll("\\", "/");
@@ -30,10 +31,13 @@ export function formatWorkspaceRelativePath(
normalizePathSeparators(trimTrailingPathSeparators(workspaceRoot)),
);
const workspaceLabel = fileBasename(normalizedWorkspaceRoot);
const pathForCompare = normalizedPath.toLowerCase();
const workspaceForCompare = normalizedWorkspaceRoot.toLowerCase();
const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
const workspaceForCompare = caseInsensitive
? normalizedWorkspaceRoot.toLowerCase()
: normalizedWorkspaceRoot;
const workspaceWithSeparator = `${workspaceForCompare}/`;
const workspaceLabelWithSeparator = `${workspaceLabel.toLowerCase()}/`;
const workspaceLabelWithSeparator = `${caseInsensitive ? workspaceLabel.toLowerCase() : workspaceLabel}/`;
if (pathForCompare === workspaceForCompare) {
displayPath = workspaceLabel;
+55
View File
@@ -254,6 +254,61 @@ describe("resolveMarkdownFileLinkTarget", () => {
});
});
it("does not classify a case-distinct POSIX sibling as a workspace file", () => {
expect(
resolveMarkdownFileLinkMeta(
"/tmp/t3code-case-test/project/probe.txt",
"/tmp/t3code-case-test/Project",
),
).toMatchObject({
displayPath: "/tmp/t3code-case-test/project/probe.txt",
workspaceRelativePath: null,
});
});
it("keeps Windows workspace comparisons case-insensitive", () => {
expect(
resolveMarkdownFileLinkMeta("C:/Users/MIKE/Project/src/main.ts", "c:/users/mike/project"),
).toMatchObject({
displayPath: "project/src/main.ts",
workspaceRelativePath: "src/main.ts",
});
});
it("keeps drive-root workspace comparisons case-insensitive", () => {
expect(resolveMarkdownFileLinkMeta("C:/Users/MIKE/project.ts", "c:/")).toMatchObject({
displayPath: "c:/Users/MIKE/project.ts",
workspaceRelativePath: "Users/MIKE/project.ts",
});
});
it("keeps backslash UNC workspace comparisons case-insensitive", () => {
expect(
resolveMarkdownFileLinkMeta(
"\\\\server\\share\\PROJECT\\src\\main.ts",
"\\\\Server\\Share\\Project",
),
).toMatchObject({
displayPath: "Project/src/main.ts",
workspaceRelativePath: "src/main.ts",
});
});
it.each([
["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
["C:/Users/MIKE/file.ts", "c:/", "Users/MIKE/file.ts"],
["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
["/tmp/repo/file.ts%20", "/tmp/repo", "file.ts "],
])("preserves the preview target for %s in workspace %s", (href, cwd, workspaceRelativePath) => {
expect(resolveMarkdownFileLinkMeta(href, cwd)).toMatchObject({ workspaceRelativePath });
});
it("keeps an encoded final space in the absolute target", () => {
expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
"/tmp/repo/file.ts ",
);
});
it("normalizes slash-prefixed windows drive paths before resolving", () => {
expect(
resolveMarkdownFileLinkTarget(
@@ -160,7 +160,17 @@ describe("workspaceRelativeFilePath", () => {
["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],
["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
["/C:/Users/mike/t3code/apps/web/a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
["/Repo/Project/src/main.ts", "/repo/project", "src/main.ts"],
["/Repo/Project/src/main.ts", "/repo/project", null],
["/tmp/case/project/probe.txt", "/tmp/case/Project", null],
["//tmp/case/project/probe.txt", "//tmp/case/Project", null],
["/tmp/case/Project/probe.txt", "/tmp/case/Project", "probe.txt"],
["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
["/C:/USERS/mike/t3code/main.ts", "/c:/users/MIKE/t3code", "main.ts"],
["\\\\server\\share\\PROJECT\\main.ts", "\\\\Server\\Share\\Project", "main.ts"],
["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
["C:/Users/MIKE/main.ts", "c:/", "Users/MIKE/main.ts"],
["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
["/tmp/repo/file.ts ", "/tmp/repo", "file.ts "],
["/tmp/report.ts", "/repo/project", null],
["/repo/project-two/a.ts", "/repo/project", null],
["/repo/project/a.ts", undefined, null],
+4 -1
View File
@@ -342,6 +342,9 @@ export function workspaceRelativeFilePath(
const normalizedRoot = stripSlashPrefixedWindowsDrive(
workspaceRoot.replaceAll("\\", "/"),
).replace(/\/+$/, "");
if (!normalizedPath.toLowerCase().startsWith(`${normalizedRoot.toLowerCase()}/`)) return null;
const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;
return normalizedPath.slice(normalizedRoot.length + 1);
}