Scoop icon indicating copy to clipboard operation
Scoop copied to clipboard

Environment variables are not refreshed after scoop install

Open thesobercoder opened this issue 6 years ago • 7 comments

I just noticed that some apps like nodejs don't have shims, now I don't have enough knowledge as to why, but these apps put append the path but the path is not refreshed automatically and hence I've to close and re-open the prompt again.

Chocolatey has a script refreshenv script to do the same, would it be possible to incorporate something like that in scoop.

@echo off
::
:: RefreshEnv.cmd
::
:: Batch file to read environment variables from registry and
:: set session variables to these values.
::
:: With this batch file, there should be no need to reload command
:: environment every time you want environment changes to propagate

::echo "RefreshEnv.cmd only works from cmd.exe, please install the Chocolatey Profile to take advantage of refreshenv from PowerShell"
echo | set /p dummy="Refreshing environment variables from registry for cmd.exe. Please wait..."

goto main

:: Set one environment variable from registry key
:SetFromReg
    "%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL
    for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (
        echo/set "%~3=%%B"
    )
    goto :EOF

:: Get a list of environment variables from registry
:GetRegEnv
    "%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp"
    for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do (
        if /I not "%%~A"=="Path" (
            call :SetFromReg "%~1" "%%~A" "%%~A"
        )
    )
    goto :EOF

:main
    echo/@echo off >"%TEMP%\_env.cmd"

    :: Slowly generating final file
    call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd"
    call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd"

    :: Special handling for PATH - mix both User and System
    call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd"
    call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"

    :: Caution: do not insert space-chars before >> redirection sign
    echo/set "Path=%%Path_HKLM%%;%%Path_HKCU%%" >> "%TEMP%\_env.cmd"

    :: Cleanup
    del /f /q "%TEMP%\_envset.tmp" 2>nul
    del /f /q "%TEMP%\_envget.tmp" 2>nul

    :: capture user / architecture
    SET "OriginalUserName=%USERNAME%"
    SET "OriginalArchitecture=%PROCESSOR_ARCHITECTURE%"

    :: Set these variables
    call "%TEMP%\_env.cmd"

    :: reset user / architecture
    SET "USERNAME=%OriginalUserName%"
    SET "PROCESSOR_ARCHITECTURE=%OriginalArchitecture%"

    echo | set /p dummy="Finished."
    echo .

thesobercoder avatar Jan 12 '19 16:01 thesobercoder

+1 scoop reset option will also benefit from this. currently after scoop reset python you need to re-open the command shell, for using the "right" pip version.

hagaigold avatar Sep 14 '19 08:09 hagaigold

yayaya, I need it badly*999999999999

2bon avatar Mar 31 '20 00:03 2bon

Related PR opened by someone else: https://github.com/lukesampson/scoop/pull/2136

Not sure which is the correct approach, but in my brief research, the refreshenv.cmd command from chocolatey is superseded by this in PowerShell: https://github.com/chocolatey/choco/blob/stable/src/chocolatey.resources/helpers/functions/Update-SessionEnvironment.ps1

nkm8 avatar Oct 23 '20 16:10 nkm8

If Scoop, instead of injecting the full path of the apps in the global/user PATHs, includes a reference to the *_HOME variables of each app (which, by the way, Scoop already creates them), that would be fantastic, and it would leave the PATHs global /user much more optimized.

Or, if you include in a third variable, something like SCOOP_APPS_PATH, and then this is included in the global/user PATHs, that would also be nice.

nonatorw avatar Jul 13 '22 09:07 nonatorw