fix: stop chezmoi ps1 scripts from loading the PowerShell profile - #126
Merged
Merged
Conversation
chezmoi update printed "Unable to find type [Microsoft.PowerShell.PSConsoleReadLine]" twice on Windows. chezmoi runs .ps1 with the default `pwsh -NoLogo -File`, which loads the user profile. The profile activates mise, whose CommandNotFound handler dereferences a PSReadLine type that a non-interactive pwsh does not have, so every failed command lookup inside a script raises InvalidOperation. The trigger was `Get-Command -Name corepack` in the mise sync script. Two layers of fix: - Remove the corepack dependency and manage pnpm directly with mise (ADR-022). Node 25 stopped bundling Corepack, so this was due anyway. - Pin the .ps1 interpreter to `pwsh -NoLogo -NoProfile -File` in .chezmoi.toml.tmpl (ADR-023) so no future command lookup can trip the handler, and so scripts stop inheriting profile side effects. Applying this requires re-running `chezmoi init` to regenerate the config. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: fb74ae4d-90e8-422e-b537-1cd9e1920e97
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
Windows で
chezmoi updateを実行すると、次のエラーが 2 回出ていた。原因は 3 段の連鎖である。
.ps1を既定の実行系pwsh -NoLogo -Fileで実行する。-NoProfileが無いため、スクリプト実行のたびにユーザープロファイルが読まれるmise activate pwshが CommandNotFound ハンドラを登録し、その本体が[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()を参照する今回の発火点は
run_onchange_after_15-mise-sync-tools.ps1のGet-Command -Name 'corepack'だった。corepack が消えていたのは、node の再インストールがnode.exeのロックで中断し、削除だけ進んだ環境破損による。対応
2 層で塞いだ。
発火点の除去(ADR-022): corepack への依存をやめ、pnpm を mise で直接管理する。Node 25 で Corepack の同梱が廃止された(nodejs/node#57617, #59835)ため、いずれ必要だった変更でもある。
config.toml.tmplにpnpm = "latest"を追加し、lockfile へ 5 プラットフォーム分のエントリを追加run_onchange_after_15-mise-sync-tools.{ps1,sh}.tmplの corepack 有効化ブロックを削除run_onchange_after_21-link-mise-shims.sh.tmplのEXCLUDE_EXACTをcorepackからpnpmへ構造の是正(ADR-023):
.chezmoi.toml.tmplの[interpreters.ps1]でpwsh -NoLogo -NoProfile -Fileに固定する。corepack だけを消しても、スクリプトが未導入コマンドの有無を調べる限り同じ現象は再発するため、プロファイル読み込み自体をやめる。副次的に、chezmoi のスクリプト実行がユーザー環境の副作用(alias、関数、PATH 改変)を持ち込まなくなる。注意点
chezmoi initの再実行が必要。config template の変更なのでchezmoi updateだけでは反映されないpwsh -Fileでスクリプトを起動する場合、mise のハンドラ問題は依然として発火する(ADR-022 の「引き継ぐ検証」に記録)run_after_40-check-git-hooks.ps1.tmplにあった「chezmoi は Windows で powershell.exe (5.1) を使う」というコメントは事実誤認だったので、あわせて修正した検証
uv run -m unittest discover -s tests→ 304 tests OK (skipped=16)tests/test_chezmoi_config_template.pyにソース形状とchezmoi execute-template --initのレンダリング結果を検査するテストを追加chezmoi --config <cfg> dump-configを実行し、interpreters.ps1.argsが["-NoLogo","-NoProfile","-File"]になることを確認pwsh -NoLogo -NoProfile -Fileを実行し、未導入コマンドを引いても PSReadLine エラーが出ないことを確認