From 890d1ee911a5426b3d8c741c9779041a4fe4d8db Mon Sep 17 00:00:00 2001 From: rebuildup Date: Mon, 30 Nov 2026 14:53:06 +0900 Subject: [PATCH] =?UTF-8?q?fix(#260):=20link.ps1=20=E3=81=AE=20repository?= =?UTF-8?q?=20root=20=E8=A7=A3=E6=B1=BA=E3=82=92=20param=20default=20?= =?UTF-8?q?=E3=81=8B=E3=82=89=20body=20=E3=81=B8=E7=A7=BB=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `script/link.ps1` の param block が default で `$PSScriptRoot` を使うと、Windows PowerShell 5.1 では param 評価時に `$PSScriptRoot` が空のため `Split-Path` が 失敗する。実機では pc-setup の Windows converge が link.ps1 を呼ぶたびに Split-Path : 引数が空の文字列であるため、パラメーター 'Path' にバインドできません。 で落ちていた。PowerShell 7 では param default でも解決されるため、7 だけで 気づけない。 - repository root の解決を param block の評価後 (body) へ移す。`$PSScriptRoot` は body では 5.1 でも 7 でも解決されるため、両方で同じ結果になる。 - `-RepoRoot` の明示指定が引き続き優先される。 - `script/link.windows.test.ts` に、Windows PowerShell 5.1 で `-RepoRoot` なしで 実行できることの回帰テストを追加 (修正前は失敗する)。 Co-Authored-By: Claude Code --- script/link.ps1 | 12 +++++++++++- script/link.windows.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/script/link.ps1 b/script/link.ps1 index ed95166..c43ce39 100644 --- a/script/link.ps1 +++ b/script/link.ps1 @@ -25,12 +25,22 @@ param( [switch]$Apply, [string]$HomeDir = $HOME, - [string]$RepoRoot = (Split-Path -Parent $PSScriptRoot) + [string]$RepoRoot = '' ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' +# Windows PowerShell 5.1 does not populate $PSScriptRoot while the param block +# is evaluated, so a default value that uses it fails there with +# "Split-Path: cannot bind argument to parameter 'Path' because it is an empty +# string". The repository root is resolved after the param block instead, which +# works on both Windows PowerShell 5.1 and PowerShell 7. An explicit +# -RepoRoot still wins (rebuildup/dotfiles#260). +if (-not $RepoRoot) { + $RepoRoot = Split-Path -Parent $PSScriptRoot +} + if ([System.Environment]::OSVersion.Platform -ne [System.PlatformID]::Win32NT) { [Console]::Error.WriteLine('link.ps1 targets Windows; use script/link on POSIX hosts.') exit 2 diff --git a/script/link.windows.test.ts b/script/link.windows.test.ts index f5eadc6..4394c98 100644 --- a/script/link.windows.test.ts +++ b/script/link.windows.test.ts @@ -207,4 +207,26 @@ describeWin("link.ps1 (Windows adoption adapter)", () => { expect(readFileSync(target, "utf8")).toContain("managed"); }); }); + + test("runs under Windows PowerShell 5.1 without an explicit repository root", () => { + withTemp((root) => { + const repo = makeRepo(root, { initSubmodule: true }); + const home = join(root, "home"); + mkdirSync(home); + // Windows PowerShell 5.1 does not populate $PSScriptRoot while the param + // block is evaluated, so a default value that uses it fails there. The + // repository root has to be resolved in the script body instead + // (rebuildup/dotfiles#260). The script is copied into the fixture repo so + // that the resolved root is the fixture and nothing outside the temp + // directory is read. + const scriptDir = join(repo, "script"); + mkdirSync(scriptDir, { recursive: true }); + const script = join(scriptDir, "link.ps1"); + writeFileSync(script, readFileSync(LINK_PS1)); + const r = run(["powershell.exe", "-NoProfile", "-File", script, "-HomeDir", home]); + expect(r.err).not.toContain("Split-Path"); + expect(r.code).toBe(0); + expect(r.out).toContain("would-link .gitconfig"); + }); + }); });