Skip to content
Draft
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
10 changes: 7 additions & 3 deletions src/platform/path-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function removeTrailingSeparators(
value: string,
parser: typeof path.posix | typeof path.win32,
): string {
const root = parser.parse(value).root;
const root = Math.max(
parser.parse(value).root.length,
path.posix.parse(value).root.length,
path.win32.parse(value).root.length,
);
let end = value.length;
while (
end > root.length &&
end > root &&
(value[end - 1] === "/" || value[end - 1] === "\\")
) {
end -= 1;
Expand Down Expand Up @@ -65,7 +69,7 @@ export function createPathAdapter(options: PathAdapterOptions): PathAdapter {
options.platform,
options.homeDirectory,
);
const normalized = parser.isAbsolute(expanded)
const normalized = path.posix.isAbsolute(expanded) || path.win32.isAbsolute(expanded)
? parser.normalize(expanded)
: parser.resolve(normalizedCwd, expanded);
return removeTrailingSeparators(normalized, parser);
Expand Down
8 changes: 7 additions & 1 deletion src/state/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function nativePlatform(): PathPlatform {
return process.platform === "win32" ? "win32" : "posix";
}

function isAbsolutePath(value: string): boolean {
return path.posix.isAbsolute(value) || path.win32.isAbsolute(value);
}

export function resolveStateLocation(
options: StateLocationOptions = {},
): StateLocation {
Expand All @@ -38,7 +42,9 @@ export function resolveStateLocation(
throw new Error("Codex home must not be empty");
}

const codexHome = parser.resolve(cwd, configuredHome);
const codexHome = isAbsolutePath(configuredHome)
? configuredHome
: parser.resolve(cwd, configuredHome);
return {
codexHome,
configDirectory: parser.join(userHome, ".config", "codex-project"),
Expand Down
10 changes: 9 additions & 1 deletion src/terminal/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@ export function folderLink(
{ tone: "folder", underline: options.hyperlinks === true },
options,
);
return hyperlink(text, pathToFileURL(folder).href, options);
const normalized = folder.replaceAll("\\", "/");
const uri = /^[A-Za-z]:\//.test(normalized)
? `file:///${encodeURI(normalized)}`
: normalized.startsWith("//")
? `file:${encodeURI(normalized)}`
: normalized.startsWith("/")
? `file://${encodeURI(normalized)}`
: pathToFileURL(folder).href;
return hyperlink(text, uri, options);
}

export interface DetectTerminalStyleOptions {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/path-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe("POSIX path adapter", () => {
expect(paths.normalize("/")).toBe("/");
});

it("preserves absolute Windows paths in POSIX mode", () => {
expect(paths.normalize("C:\\Work\\Alpha\\")).toBe("C:\\Work\\Alpha");
});

it("keeps POSIX comparisons case-sensitive", () => {
expect(paths.equals("/Work/Alpha", "/Work/Alpha/")).toBe(true);
expect(paths.equals("/Work/Alpha", "/work/alpha")).toBe(false);
Expand Down
17 changes: 17 additions & 0 deletions test/unit/state-discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,21 @@ describe("state discovery", () => {
"C:\\work\\tests\\fixture-home\\.codex-global-state.json",
});
});

it("keeps absolute Windows Codex home unchanged in posix-mode resolution", () => {
expect(
resolveStateLocation({
codexHome: "C:\\Users\\Example\\AppData\\Local\\Temp\\codex-home",
cwd: "D:\\work\\repo",
env: {},
platform: "posix",
userHome: "/Users/example",
}),
).toEqual({
codexHome: "C:\\Users\\Example\\AppData\\Local\\Temp\\codex-home",
configDirectory: "/Users/example/.config/codex-project",
stateFile:
"C:\\Users\\Example\\AppData\\Local\\Temp\\codex-home/.codex-global-state.json",
});
});
});
10 changes: 9 additions & 1 deletion test/unit/terminal-style.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { detectTerminalStyle } from "../../src/terminal/style.js";
import { detectTerminalStyle, folderLink } from "../../src/terminal/style.js";

describe("terminal style detection", () => {
it("enables colors and hyperlinks for an interactive terminal", () => {
Expand Down Expand Up @@ -46,4 +46,12 @@ describe("terminal style detection", () => {
}),
).toEqual({ colors: true, hyperlinks: true });
});

it("formats absolute paths into stable file hyperlinks", () => {
const linked = folderLink("/work/rose garden", {
colors: false,
hyperlinks: true,
});
expect(linked).toContain("\u001B]8;;file:///work/rose%20garden\u001B\\");
});
});