Skip to content

fix(cli): expand "~/" home shorthand on Windows#1055

Open
LukeTheoJohnson wants to merge 1 commit into
EveryInc:mainfrom
LukeTheoJohnson:fix-expandhome-forward-slash-windows
Open

fix(cli): expand "~/" home shorthand on Windows#1055
LukeTheoJohnson wants to merge 1 commit into
EveryInc:mainfrom
LukeTheoJohnson:fix-expandhome-forward-slash-windows

Conversation

@LukeTheoJohnson

@LukeTheoJohnson LukeTheoJohnson commented Jul 2, 2026

Copy link
Copy Markdown

Problem

On Windows, the ~/ home shorthand is not expanded, so the CLI treats a literal ~ as a real path segment.

expandHome() in src/utils/resolve-home.ts only matched `~${path.sep}`. On Windows path.sep is \, so that check is ~\. The portable ~/ spelling, the one users and config files write across platforms, falls through unmatched and is returned verbatim.

Because expandHome backs resolveTargetHome / resolveCodexHome, the unexpanded value reaches convert, install, and cleanup through --output, --codex-home, --pi-home, CODEX_HOME, and OPENCODE_CONFIG_DIR. The result is a bogus ~ directory created in the working tree instead of a path under the user's home.

This is Linux/macOS-invisible: there path.sep is /, so ~/ already matches. CI runs on ubuntu-latest only, which is why it was not caught.

Root cause / evidence

Reproduced with the real function on Windows (path.sep === "\\", os.homedir() === C:\Users\lukej):

expandHome("~")         -> "C:\Users\lukej"          (ok)
expandHome("~/.codex")  -> "~/.codex"                (BUG: unexpanded)
expandHome("~\.codex")  -> "C:\Users\lukej\.codex"   (native separator already worked)

Fix

Match ~/ in addition to the native separator:

if (value.startsWith("~/") || value.startsWith(`~${path.sep}`)) {
  return path.join(os.homedir(), value.slice(2))
}

Surgical by design: POSIX behaviour is unchanged. On POSIX ~\foo still passes through untouched (backslash is a legal filename character there); only Windows ~/x handling changes.

Tests

Adds tests/resolve-home.test.ts:

  • ~/.codex and ~/sub/dir expand to a home-joined path on every platform (the regression guard: red on Windows before this change, green after).
  • the native-separator form and bare ~ still expand.
  • ~foo, relative, and absolute paths pass through unchanged.

Verification on Windows:

  • The forward-slash test fails on the pre-fix code (Expected "C:\Users\lukej\.codex", Received "~/.codex") and passes after the fix (4/4).
  • Full-suite baseline diff (clean main vs this branch, per-test timings stripped): 168 pre-existing Windows failures on both sides, zero added, zero removed. The new suite adds four passing tests.

Related

Same cross-platform path class as prior Windows fixes in this area: #398 (sanitize colons in skill/agent names for Windows), #226 (Windows NTFS colons), and issues #366 / #48 / #49 (colon-in-path ENOENT on Windows). Windows is a supported target with a history of exactly these Unix-invisible path bugs.

expandHome only matched `~${path.sep}`, which is `~\` on Windows, so the
portable `~/` home shorthand -- the spelling users and config files write
across platforms -- was returned unexpanded. Callers then treated a literal
`~` as a real path segment (reachable via `--output ~/foo`, `--codex-home`,
`CODEX_HOME`, `OPENCODE_CONFIG_DIR`), creating a bogus `~` directory in the
working tree instead of resolving under the user's home.

Match `~/` in addition to the native separator. POSIX behaviour is unchanged
(`~\foo` still passes through, since backslash is a legal filename character
there); only Windows `~/x` handling changes.

Adds tests/resolve-home.test.ts covering the forward-slash shorthand, the
native-separator form, bare `~`, and the pass-through cases.
@LukeTheoJohnson LukeTheoJohnson marked this pull request as ready for review July 2, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant