ScoopInstaller / Scoop

A command-line installer for Windows.
https://scoop.sh
Other
21.29k stars 1.41k forks source link

Environment variables are not refreshed after scoop install #2980

Open thesobercoder opened 5 years ago

thesobercoder commented 5 years ago

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 .
hagaigold commented 5 years ago

+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.

2bon commented 4 years ago

yayaya, I need it badly*999999999999

nkm8 commented 4 years ago

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

nonatorw commented 2 years ago

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.

HUMORCE commented 1 month ago

It has been fixed long time ago.

❯ scoop install nodejs-lts
Installing 'nodejs-lts' (20.17.0) [64bit] from 'main' bucket
Loading node-v20.17.0-win-x64.7z from cache
Checking hash of node-v20.17.0-win-x64.7z ... ok.
Extracting node-v20.17.0-win-x64.7z ... done.
Linking ~\scoop\apps\nodejs-lts\current => ~\scoop\apps\nodejs-lts\20.17.0
Adding ~\scoop\apps\nodejs-lts\current\bin to your path.
Adding ~\scoop\apps\nodejs-lts\current to your path.
Persisting bin
Persisting cache
Running post_install script...done.
'nodejs-lts' (20.17.0) was installed successfully!

❯ node --version
v20.17.0
bersbersbers commented 1 month ago

@HUMORCE while this works in Powershell, it does not work in a Command Prompt:

PS C:\> scoop --version
Current Scoop version:
859d1db5 (HEAD -> master, tag: v0.5.2, origin/master, origin/HEAD) chore(release): Bump to version 0.5.2 (#6080)
...

PS C:\> scoop install nodejs-lts
Installing 'nodejs-lts' (20.17.0) [64bit] from 'main' bucket
Loading node-v20.17.0-win-x64.7z from cache
Checking hash of node-v20.17.0-win-x64.7z ... ok.
Extracting node-v20.17.0-win-x64.7z ... done.
Linking C:\Scoop\apps\nodejs-lts\current => C:\Scoop\apps\nodejs-lts\20.17.0
Adding C:\Scoop\apps\nodejs-lts\current\bin to your path.
Adding C:\Scoop\apps\nodejs-lts\current to your path.
Persisting bin
Persisting cache
Running post_install script...done.
'nodejs-lts' (20.17.0) was installed successfully!

PS C:\> node --version
v20.17.0
C:\>scoop --version
Current Scoop version:
859d1db5 (HEAD -> master, tag: v0.5.2, origin/master, origin/HEAD) chore(release): Bump to version 0.5.2 (#6080)
...

C:\>scoop install nodejs-lts
Installing 'nodejs-lts' (20.17.0) [64bit] from 'main' bucket
Loading node-v20.17.0-win-x64.7z from cache
Checking hash of node-v20.17.0-win-x64.7z ... ok.
Extracting node-v20.17.0-win-x64.7z ... done.
Linking C:\Scoop\apps\nodejs-lts\current => C:\Scoop\apps\nodejs-lts\20.17.0
Adding C:\Scoop\apps\nodejs-lts\current\bin to your path.
Adding C:\Scoop\apps\nodejs-lts\current to your path.
Persisting bin
Persisting cache
Running post_install script...done.
'nodejs-lts' (20.17.0) was installed successfully!

C:\>node --version
'node' is not recognized as an internal or external command,
operable program or batch file.
HUMORCE commented 1 month ago

Thanks for point out