From eac9e39d794f5abb5affe720784b78e4ffcfd926 Mon Sep 17 00:00:00 2001 From: kaitranntt Date: Sun, 2 Nov 2025 11:40:36 -0500 Subject: [PATCH] fix(ccs): improve environment variable handling and error resilience - use proper .NET enum for environment variable targets - add try-catch blocks for environment variable operations - provide user-friendly warnings when operations fail - fixes PATH operations in install and uninstall scripts --- ccs.ps1 | 29 +++++++++++++++++++---------- install.ps1 | 4 ++-- uninstall.ps1 | 4 ++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/ccs.ps1 b/ccs.ps1 index 3e8c59f5..52112ab6 100644 --- a/ccs.ps1 +++ b/ccs.ps1 @@ -144,11 +144,16 @@ if ($EnvVars.PSObject.Properties.Count -gt 0) { $VarName = $Property.Name $VarValue = $Property.Value - # Save original value - $OriginalEnvVars[$VarName] = [System.Environment]::GetEnvironmentVariable($VarName, 'Process') + try { + # Save original value + $OriginalEnvVars[$VarName] = [System.Environment]::GetEnvironmentVariable($VarName, [System.EnvironmentVariableTarget]::Process) - # Set new value for this process only - [System.Environment]::SetEnvironmentVariable($VarName, $VarValue, 'Process') + # Set new value for this process only + [System.Environment]::SetEnvironmentVariable($VarName, $VarValue, [System.EnvironmentVariableTarget]::Process) + } catch { + Write-Host "Warning: Could not set environment variable '$VarName'" -ForegroundColor Yellow + Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Yellow + } } } @@ -164,12 +169,16 @@ try { # Restore original environment variables foreach ($VarName in $OriginalEnvVars.Keys) { $OriginalValue = $OriginalEnvVars[$VarName] - if ($null -eq $OriginalValue) { - # Variable didn't exist before, remove it - [System.Environment]::SetEnvironmentVariable($VarName, $null, 'Process') - } else { - # Restore original value - [System.Environment]::SetEnvironmentVariable($VarName, $OriginalValue, 'Process') + try { + if ($null -eq $OriginalValue) { + # Variable didn't exist before, remove it + [System.Environment]::SetEnvironmentVariable($VarName, $null, [System.EnvironmentVariableTarget]::Process) + } else { + # Restore original value + [System.Environment]::SetEnvironmentVariable($VarName, $OriginalValue, [System.EnvironmentVariableTarget]::Process) + } + } catch { + Write-Host "Warning: Could not restore environment variable '$VarName'" -ForegroundColor Yellow } } } diff --git a/install.ps1 b/install.ps1 index b7fcc93f..39c5e551 100644 --- a/install.ps1 +++ b/install.ps1 @@ -235,7 +235,7 @@ Write-Host "└─" Write-Host "" # Check and update PATH -$UserPath = [Environment]::GetEnvironmentVariable("Path", "User") +$UserPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) if ($UserPath -notlike "*$CcsDir*") { Write-Host "[!] PATH Configuration Required" Write-Host "" @@ -243,7 +243,7 @@ if ($UserPath -notlike "*$CcsDir*") { try { $NewPath = if ($UserPath) { "$UserPath;$CcsDir" } else { $CcsDir } - [Environment]::SetEnvironmentVariable("Path", $NewPath, "User") + [Environment]::SetEnvironmentVariable("Path", $NewPath, [System.EnvironmentVariableTarget]::User) Write-Host " [OK] PATH updated. Restart your terminal for changes to take effect." Write-Host "" diff --git a/uninstall.ps1 b/uninstall.ps1 index da3c3e49..7642999b 100644 --- a/uninstall.ps1 +++ b/uninstall.ps1 @@ -20,11 +20,11 @@ if (Test-Path "$CcsDir\ccs.ps1") { $UninstallScript = "$CcsDir\uninstall.ps1" # Remove from PATH -$UserPath = [Environment]::GetEnvironmentVariable("Path", "User") +$UserPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) if ($UserPath -like "*$CcsDir*") { try { $NewPath = ($UserPath -split ';' | Where-Object { $_ -ne $CcsDir }) -join ';' - [Environment]::SetEnvironmentVariable("Path", $NewPath, "User") + [Environment]::SetEnvironmentVariable("Path", $NewPath, [System.EnvironmentVariableTarget]::User) Write-Host "[OK] Removed from PATH: $CcsDir" Write-Host " Restart your terminal for changes to take effect." } catch {