Skip to content

Commit 06b1962

Browse files
hkaltaiclaude
andcommitted
fix(install): tighten alias shim writes and uninstall hint
- install.ps1: replace each shim just before writing it, so a failed .cmd write keeps the Git Bash shim. - uninstall: only ~/.altimate/bin, case-fold names on Windows only, name a leftover altimate-code.exe.old, skip directories. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 26ebbd3 commit 06b1962

4 files changed

Lines changed: 49 additions & 24 deletions

File tree

‎install.ps1‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,28 @@ function Install-Alias {
5858
$shadowingExe = [IO.Path]::Combine($Dir, "altimate-code.exe")
5959
Remove-Item -LiteralPath "$shadowingExe.old" -Force -ErrorAction SilentlyContinue
6060

61-
$cmdShim = [IO.Path]::Combine($Dir, "altimate-code.cmd")
62-
$shShim = [IO.Path]::Combine($Dir, "altimate-code")
63-
# Replace an existing file or link instead of writing through it, so a link at a
64-
# shim's path can never overwrite the file it points to. GetAttributes does not
65-
# follow links, so a dangling one is caught too. A real directory is left alone
66-
# and the write below fails into the catch.
67-
foreach ($path in @($cmdShim, $shShim)) {
61+
$shims = [ordered]@{
62+
"altimate-code.cmd" = "@echo off`r`n`"%~dp0altimate.exe`" %*`r`nexit /b %ERRORLEVEL%`r`n"
63+
"altimate-code" = "#!/bin/sh`nexec `"`$(dirname `"`$0`")/altimate.exe`" `"`$@`"`n"
64+
}
65+
# Shims first: if one cannot be written, an existing altimate-code.exe stays usable.
66+
# Each shim replaces an existing file or link instead of writing through it, so a link
67+
# at its path can never overwrite the file it points to; GetAttributes does not follow
68+
# links, so a dangling one is caught too. Each entry is replaced just before its own
69+
# write, so a failure leaves the shims not yet reached as they were. A real directory
70+
# is left alone and the write fails into the catch.
71+
foreach ($name in $shims.Keys) {
72+
$path = [IO.Path]::Combine($Dir, $name)
6873
$attrs = $null
6974
try { $attrs = [IO.File]::GetAttributes($path) } catch {}
70-
if ($null -eq $attrs) { continue }
71-
$isDir = [bool]($attrs -band [IO.FileAttributes]::Directory)
72-
if ($isDir -and ($attrs -band [IO.FileAttributes]::ReparsePoint)) { [IO.Directory]::Delete($path) }
73-
elseif (-not $isDir) { [IO.File]::Delete($path) }
75+
if ($null -ne $attrs) {
76+
$isDir = [bool]($attrs -band [IO.FileAttributes]::Directory)
77+
if ($isDir -and ($attrs -band [IO.FileAttributes]::ReparsePoint)) { [IO.Directory]::Delete($path) }
78+
elseif (-not $isDir) { [IO.File]::Delete($path) }
79+
}
80+
[IO.File]::WriteAllText($path, $shims[$name], [Text.Encoding]::ASCII)
7481
}
7582

76-
# Shims first: if one cannot be written, an existing altimate-code.exe stays usable.
77-
[IO.File]::WriteAllText($cmdShim, "@echo off`r`n`"%~dp0altimate.exe`" %*`r`nexit /b %ERRORLEVEL%`r`n", [Text.Encoding]::ASCII)
78-
[IO.File]::WriteAllText($shShim, "#!/bin/sh`nexec `"`$(dirname `"`$0`")/altimate.exe`" `"`$@`"`n", [Text.Encoding]::ASCII)
79-
8083
if (Test-Path -LiteralPath $shadowingExe -PathType Leaf) {
8184
try { Remove-Item -LiteralPath $shadowingExe -Force } catch {
8285
Remove-Item -LiteralPath "$shadowingExe.old" -Force -ErrorAction SilentlyContinue

‎packages/opencode/src/cli/cmd/uninstall.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@ interface RemovalTargets {
2525
// altimate_change start — files the standalone installers place beside the running binary
2626
/**
2727
* The other names the standalone installers put in ~/.altimate/bin next to `binary`: the
28-
* `altimate-code` alias (install.ps1 writes .cmd and extensionless shims, the bash installer
29-
* under Git Bash links or copies an .exe) and the canonical binary, each unless it is the one
30-
* running (`binary` can be a copied alias). Empty outside ~/.altimate/bin, so an unrelated `altimate-code` in a shared
31-
* directory such as ~/.local/bin is never named for removal.
28+
* `altimate-code` alias (install.ps1 writes .cmd and extensionless shims and may leave an
29+
* .exe.old aside, the bash installer under Git Bash links or copies an .exe) and the canonical
30+
* binary, each unless it is the one running (`binary` can be a copied alias). Empty outside
31+
* ~/.altimate/bin, so an unrelated `altimate-code` in a shared directory such as ~/.local/bin is
32+
* never named for removal.
3233
*/
3334
export function installerSiblingFiles(binary: string, platform: NodeJS.Platform): string[] {
3435
const paths = platform === "win32" ? path.win32 : path.posix
3536
const binDir = paths.dirname(binary)
36-
if (paths.basename(paths.dirname(binDir)) !== ".altimate") return []
37+
if (paths.basename(binDir) !== "bin" || paths.basename(paths.dirname(binDir)) !== ".altimate") return []
3738
const names =
3839
platform === "win32"
39-
? ["altimate.exe", "altimate-code.cmd", "altimate-code", "altimate-code.exe"]
40+
? ["altimate.exe", "altimate-code.cmd", "altimate-code", "altimate-code.exe", "altimate-code.exe.old"]
4041
: ["altimate", "altimate-code"]
41-
const running = paths.basename(binary).toLowerCase()
42+
// Windows file names are case-insensitive; POSIX ones are not.
43+
const running = platform === "win32" ? paths.basename(binary).toLowerCase() : paths.basename(binary)
4244
return names.filter((name) => name !== running).map((name) => paths.join(binDir, name))
4345
}
4446
// altimate_change end
@@ -305,10 +307,11 @@ async function executeUninstall(method: Installation.Method, targets: RemovalTar
305307
prompts.log.info(` rm "${targets.binary}"`)
306308

307309
const binDir = path.dirname(targets.binary)
308-
// altimate_change start — also name the installer's other files that exist beside the binary
310+
// altimate_change start — also name the installer's other files that exist beside the binary;
311+
// a real directory at one of those names is not the installer's (it skips them), so it is left out
309312
for (const file of installerSiblingFiles(targets.binary, process.platform)) {
310313
const present = await fs.lstat(file).then(
311-
() => true,
314+
(stat) => !stat.isDirectory(),
312315
() => false,
313316
)
314317
if (present) prompts.log.info(` rm -f "${file}"`)

‎packages/opencode/test/cli/uninstall-hint.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("uninstall — installer files beside the binary", () => {
1313
"C:\\Users\\u\\.altimate\\bin\\altimate-code.cmd",
1414
"C:\\Users\\u\\.altimate\\bin\\altimate-code",
1515
"C:\\Users\\u\\.altimate\\bin\\altimate-code.exe",
16+
"C:\\Users\\u\\.altimate\\bin\\altimate-code.exe.old",
1617
])
1718
})
1819

@@ -21,11 +22,20 @@ describe("uninstall — installer files beside the binary", () => {
2122
"C:\\Users\\u\\.altimate\\bin\\altimate.exe",
2223
"C:\\Users\\u\\.altimate\\bin\\altimate-code.cmd",
2324
"C:\\Users\\u\\.altimate\\bin\\altimate-code",
25+
"C:\\Users\\u\\.altimate\\bin\\altimate-code.exe.old",
26+
])
27+
})
28+
29+
test("matches the running name case-sensitively on POSIX", () => {
30+
expect(installerSiblingFiles("/home/u/.altimate/bin/ALTIMATE", "linux")).toEqual([
31+
"/home/u/.altimate/bin/altimate",
32+
"/home/u/.altimate/bin/altimate-code",
2433
])
2534
})
2635

2736
test("names nothing outside the installers' own directory", () => {
2837
expect(installerSiblingFiles("/home/u/.local/bin/altimate", "linux")).toEqual([])
2938
expect(installerSiblingFiles("/home/u/.opencode/bin/altimate", "linux")).toEqual([])
39+
expect(installerSiblingFiles("/home/u/.altimate/not-bin/altimate", "linux")).toEqual([])
3040
})
3141
})

‎test/windows/install.Tests.ps1‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,15 @@ Describe "install.ps1 Install-Alias" {
436436
Test-Path ([IO.Path]::Combine($script:Sandbox, "altimate-code.exe")) | Should -BeTrue
437437
}
438438

439+
It "keeps the Git Bash shim when the .cmd shim cannot be written" {
440+
Set-Content -Path ([IO.Path]::Combine($script:Sandbox, "altimate.exe")) -Value "x" -NoNewline
441+
Set-Content -Path ([IO.Path]::Combine($script:Sandbox, "altimate-code")) -Value "previous" -NoNewline
442+
New-Item -ItemType Directory -Path ([IO.Path]::Combine($script:Sandbox, "altimate-code.cmd")) | Out-Null
443+
444+
{ Install-Alias -Dir $script:Sandbox } | Should -Not -Throw
445+
[IO.File]::ReadAllText([IO.Path]::Combine($script:Sandbox, "altimate-code")) | Should -Be "previous"
446+
}
447+
439448
It "does not throw when the shim cannot be written" {
440449
Set-Content -Path ([IO.Path]::Combine($script:Sandbox, "altimate.exe")) -Value "x" -NoNewline
441450
New-Item -ItemType Directory -Path ([IO.Path]::Combine($script:Sandbox, "altimate-code.cmd")) | Out-Null

0 commit comments

Comments
 (0)