fix: replace setx with reg add to avoid 1024-char PATH truncation (#101)

- Use reg add instead of setx to bypass Windows 1024-character limit
- Add safety check for quotes/exclamation marks in PATH to prevent injection
- Preserve stderr output for better error diagnostics
- Update documentation with warnings about cmd PATH duplication
- Add test script for PATH update validation

Generated with SWE-Agent.ai

Co-Authored-By: SWE-Agent.ai <noreply@swe-agent.ai>
This commit is contained in:
cexll
2025-12-31 14:40:34 +08:00
parent 993249acb1
commit 81e95777a8
4 changed files with 86 additions and 8 deletions

View File

@@ -346,8 +346,10 @@ $Env:PATH = "$HOME\bin;$Env:PATH"
``` ```
```batch ```batch
REM cmd.exe - persistent for current user REM cmd.exe - persistent for current user (use PowerShell method above instead)
setx PATH "%USERPROFILE%\bin;%PATH%" REM WARNING: This expands %PATH% which includes system PATH, causing duplication
REM Note: Using reg add instead of setx to avoid 1024-character truncation limit
reg add "HKCU\Environment" /v Path /t REG_EXPAND_SZ /d "%USERPROFILE%\bin;%PATH%" /f
``` ```
--- ---

View File

@@ -282,8 +282,10 @@ $Env:PATH = "$HOME\bin;$Env:PATH"
``` ```
```batch ```batch
REM cmd.exe - 永久添加(当前用户) REM cmd.exe - 永久添加(当前用户)(建议使用上面的 PowerShell 方法)
setx PATH "%USERPROFILE%\bin;%PATH%" REM 警告:此命令会展开 %PATH% 包含系统 PATH导致重复
REM 注意:使用 reg add 而非 setx 以避免 1024 字符截断限制
reg add "HKCU\Environment" /v Path /t REG_EXPAND_SZ /d "%USERPROFILE%\bin;%PATH%" /f
``` ```
--- ---

View File

@@ -117,13 +117,20 @@ if "!ALREADY_IN_USERPATH!"=="1" (
set "USER_PATH_NEW=!PCT!USERPROFILE!PCT!\bin" set "USER_PATH_NEW=!PCT!USERPROFILE!PCT!\bin"
) )
rem Persist update to HKCU\Environment\Path (user scope) rem Persist update to HKCU\Environment\Path (user scope)
setx Path "!USER_PATH_NEW!" >nul rem Use reg add instead of setx to avoid 1024-character limit
echo(!USER_PATH_NEW! | findstr /C:"\"" /C:"!" >nul
if not errorlevel 1 (
echo WARNING: Your PATH contains quotes or exclamation marks that may cause issues.
echo Skipping automatic PATH update. Please add %%USERPROFILE%%\bin to your PATH manually.
) else (
reg add "HKCU\Environment" /v Path /t REG_EXPAND_SZ /d "!USER_PATH_NEW!" /f >nul
if errorlevel 1 ( if errorlevel 1 (
echo WARNING: Failed to append %%USERPROFILE%%\bin to your user PATH. echo WARNING: Failed to append %%USERPROFILE%%\bin to your user PATH.
) else ( ) else (
echo Added %%USERPROFILE%%\bin to your user PATH. echo Added %%USERPROFILE%%\bin to your user PATH.
) )
) )
)
) )
rem Update current session PATH so codeagent-wrapper is immediately available rem Update current session PATH so codeagent-wrapper is immediately available

67
test_install_path.bat Normal file
View File

@@ -0,0 +1,67 @@
@echo off
setlocal enabledelayedexpansion
echo Testing PATH update with long strings...
echo.
rem Create a very long PATH string (over 1024 characters)
set "LONG_PATH="
for /L %%i in (1,1,30) do (
set "LONG_PATH=!LONG_PATH!C:\VeryLongDirectoryName%%i\SubDirectory\AnotherSubDirectory;"
)
echo Generated PATH length:
echo !LONG_PATH! > temp_path.txt
for %%A in (temp_path.txt) do set "PATH_LENGTH=%%~zA"
del temp_path.txt
echo !PATH_LENGTH! bytes
rem Test 1: Verify reg add can handle long strings
echo.
echo Test 1: Testing reg add with long PATH...
set "TEST_PATH=!LONG_PATH!%%USERPROFILE%%\bin"
reg add "HKCU\Environment" /v TestPath /t REG_EXPAND_SZ /d "!TEST_PATH!" /f >nul 2>nul
if errorlevel 1 (
echo FAIL: reg add failed with long PATH
goto :cleanup
) else (
echo PASS: reg add succeeded with long PATH
)
rem Test 2: Verify the value was stored correctly
echo.
echo Test 2: Verifying stored value length...
for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v TestPath 2^>nul ^| findstr /I "TestPath"') do set "STORED_PATH=%%B"
echo !STORED_PATH! > temp_stored.txt
for %%A in (temp_stored.txt) do set "STORED_LENGTH=%%~zA"
del temp_stored.txt
echo Stored PATH length: !STORED_LENGTH! bytes
if !STORED_LENGTH! LSS 1024 (
echo FAIL: Stored PATH was truncated
goto :cleanup
) else (
echo PASS: Stored PATH was not truncated
)
rem Test 3: Verify %%USERPROFILE%%\bin is present
echo.
echo Test 3: Verifying %%USERPROFILE%%\bin is in stored PATH...
echo !STORED_PATH! | findstr /I "USERPROFILE" >nul
if errorlevel 1 (
echo FAIL: %%USERPROFILE%%\bin not found in stored PATH
goto :cleanup
) else (
echo PASS: %%USERPROFILE%%\bin found in stored PATH
)
echo.
echo ========================================
echo All tests PASSED
echo ========================================
:cleanup
echo.
echo Cleaning up test registry key...
reg delete "HKCU\Environment" /v TestPath /f >nul 2>nul
endlocal