Skip to content

Commit fa8f4b8

Browse files
committed
Fix Windows hook cleanup after disable
1 parent aa1ea40 commit fa8f4b8

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

scripts/install-windows.ps1

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,24 @@ function Remove-ManagedClaudeHookEntries {
661661
return ,$result
662662
}
663663
664+
function Get-ObjectPropertyNames {
665+
param([object]$Object)
666+
667+
if ($null -eq $Object) {
668+
return @()
669+
}
670+
671+
if ($Object -is [System.Collections.IDictionary]) {
672+
return @($Object.Keys)
673+
}
674+
675+
return @(
676+
$Object.PSObject.Properties |
677+
Where-Object { $_.MemberType -eq "NoteProperty" } |
678+
ForEach-Object { $_.Name }
679+
)
680+
}
681+
664682
function Update-ClaudeSettingsHooks {
665683
param(
666684
[object]$Settings,
@@ -720,7 +738,7 @@ function Update-ClaudeSettingsHooks {
720738
$Settings.hooks.PSObject.Properties.Remove("Stop")
721739
}
722740
723-
if ($Settings.hooks.PSObject.Properties.Count -eq 0) {
741+
if ((Get-ObjectPropertyNames $Settings.hooks).Count -eq 0) {
724742
$Settings.PSObject.Properties.Remove("hooks")
725743
}
726744
@@ -1020,7 +1038,7 @@ function Disable-Notifications {
10201038
$settings = Get-Content $settingsFile -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
10211039
if ($settings -and $settings.hooks) {
10221040
$settings = Update-ClaudeSettingsHooks -Settings $settings -NotifyScript (Get-NotifyScript) -Disable
1023-
if ($settings.PSObject.Properties.Count -eq 0) {
1041+
if ((Get-ObjectPropertyNames $settings).Count -eq 0) {
10241042
Remove-Item $settingsFile -Force -ErrorAction SilentlyContinue
10251043
} else {
10261044
$settings | ConvertTo-Json -Depth 10 | Set-Content $settingsFile -Encoding UTF8

tests/test-windows-claude-hook-preservation.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ try {
117117
if (Test-HookEntriesContainCommand -Entries @($settings.hooks.Notification) -Matcher "idle_prompt" -Command $notifyCommand) {
118118
throw "managed Notification hook should be removed during disable"
119119
}
120+
121+
Remove-Item $script:SettingsFile -Force -ErrorAction SilentlyContinue
122+
123+
Enable-Notifications -Tool "claude"
124+
$settings = Get-Content $script:SettingsFile -Raw | ConvertFrom-Json -ErrorAction Stop
125+
if (-not $settings.hooks) {
126+
throw "managed hooks were not created for the clean settings case"
127+
}
128+
129+
Disable-Notifications -Tool "claude"
130+
if (Test-Path $script:SettingsFile) {
131+
$settings = Get-Content $script:SettingsFile -Raw | ConvertFrom-Json -ErrorAction Stop
132+
if ($settings.hooks) {
133+
throw "hooks should be removed entirely when only managed Claude hooks existed"
134+
}
135+
}
120136
}
121137
finally {
122138
Remove-Item $testRoot -Recurse -Force -ErrorAction SilentlyContinue

0 commit comments

Comments
 (0)