Choco: Refreshenv not working with p4

Created on 22 Jun 2018  路  7Comments  路  Source: chocolatey/choco

What You Are Seeing?

I'm using chocolatey to install the p4 cmd line tools. After the install it talks about running refreshenv to load the PATH changes but even after I run refreshenv neither powershell or cmd.exe recognize p4

What is Expected?

For p4 to be a recognized cmd after refreshenv is run.

How Did You Get This To Happen? (Steps to Reproduce)

I ran "choco install p4 --ignore-checksums"
(the checksums are wrong currently cause perforce did an update this morning)
I then ran "refreshenv"
I then tried both "p4 set" and "cmd /c 'p4 set'" and neither could find p4 as a cmd
when I opened a fresh powershell window both "p4 set" and "cmd /c 'p4 set'" worked just fine

Output Log

I'm attaching the log info pulled from the chocolatey log file, it's only the p4 install cause refreshenv doesn't seem to log anything :/ I'm also attaching a screen shot of my powershell window

I'm on an aws instance running Windows Server 2016 version 10.0.14393.0, my powershell version is 5.1.14393.2248 and my chocolatey is version 0.10.11.0

Please let me know if you need any other info.
choco_log.txt
powershell

Most helpful comment

did you notice the message that says:
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..

^^ Would be the cause of why it doesn't work for you. Most likely you don't have the necessary alias in your powershell profile, which causes it to use the wrong refreshenv function/program.

If you add the following to your powershell profile, it should then work for you

# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
  Import-Module "$ChocolateyProfile"
}

You can add this without editing the file manually by running:

Add-Content -Path "$profile" -Value @'

# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
  Import-Module "$ChocolateyProfile"
}
'@

All 7 comments

did you notice the message that says:
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..

^^ Would be the cause of why it doesn't work for you. Most likely you don't have the necessary alias in your powershell profile, which causes it to use the wrong refreshenv function/program.

If you add the following to your powershell profile, it should then work for you

# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
  Import-Module "$ChocolateyProfile"
}

You can add this without editing the file manually by running:

Add-Content -Path "$profile" -Value @'

# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
  Import-Module "$ChocolateyProfile"
}
'@

I was expecting it to refresh the env vars in either cmd.exe or powershell and it did neither..... I fail to see how that message should have informed me it wasn't refreshing the cmd.exe env vars. I tested within cmd.exe for the most part if you look at my image.

I only also checked to see if p4 worked in powershell cause it wouldn't be the first time the print message wasn't exactly right.

Your script will refresh the env vars in powershell, but it also doesn't work for me.
power

Actually your script seems to work sometimes??? Is very strange, sometimes the env vars don't seem to actually update and other times they do....
Is very strange....
And nothing seems to work to update them for cmd.exe, is there just not a way to do that?

meh, it seems to at least work on the first try via powershell this way. Doesn't really need to be mroe reliable then that for this use and I don't need to use cmd /c anyway

Cmd.exe /c Is equivalent of run a subprocess and when you are done, exit.

Any changes to path or other environment variables won't come back up to the parent process and the parent process passes it's environment variables to a subprocess.

So powershell.exe -> cmd.exe /c "refreshenv"

  1. Let's say we started powershell.exe when bob=1 (an environment variable).
  2. Another place we persisted a change for bob=2. That is now in the registry.
  3. Due to the way Windows works, shells don't automatically get environment vars updated. There is an API call for broadcasting the change, so applications and GUIs can go make updates, but shells ignore it.
  4. So powershell.exe still has env var bob=1 since it has not been restarted since the change was made
  5. When cmd.exe sub process is started, it inherits bob=1 from the parent process (powershell.exe). If you were to start it from run or wherever, it's likely it would inherit from explorer.exe, which most often would see that change already and it would have bob=2. But we are not doing that here - we are running a subprocess cmd from powershell.exe, so bob=1 because of inheriting the parent process's environment
  6. So now we call refreshenv. cmd.exe has bob=2.
  7. And /c, we exit back to the parent process powershell.exe where bob=1.
  8. Then we call cmd /c "something" again - we are starting up another subprocess so guess what bob equals? That's right - bob=1

HTH explain the craziness here. So any call to refresh environment variables must be run in process.

And the above is all Windows - refreshenv is how we have something that deals with that limitation. But it's a straight call, no shelling out to a subprocess (aka cmd /c).

I was suspicious the subprocess thing might be the case which is why I tried to do both commands in the same call, but that didn't work either
EX cmd /c "refreshenv & p4 set"
I tried both single ampersand & and double && and even gave pipe | a try but they all only seemed to call the refreshenv and not the p4 cmd (you can see this in the first image I uploaded)

Was this page helpful?
0 / 5 - 0 ratings