Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refreshenv not working with p4 #1600

Closed
anne-101 opened this issue Jun 22, 2018 · 8 comments
Closed

Refreshenv not working with p4 #1600

anne-101 opened this issue Jun 22, 2018 · 8 comments

Comments

@anne-101
Copy link

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

@AdmiringWorm
Copy link
Member

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"
}
'@

@anne-101
Copy link
Author

anne-101 commented Jun 22, 2018

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

@anne-101
Copy link
Author

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?

@anne-101
Copy link
Author

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

@ferventcoder
Copy link
Member

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.

@ferventcoder
Copy link
Member

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

@anne-101
Copy link
Author

anne-101 commented Jul 2, 2018

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)

jmcker added a commit to jmcker/ChocolateyPackages that referenced this issue Apr 20, 2021
Use `Update-SessionEnvironment` to refresh the current shell's environment. The `refreshenv` command used earlier doesn't seem to do the trick based on the `cmd.exe` line seen in the [logs](https://github.com/jmcker/ChocolateyPackages/runs/2392035005?check_suite_focus=true#step:2:916) and [this issue](chocolatey/choco#1600)
@hrishikesh-kadam
Copy link

Official explanation has been added to Troubleshooting - https://docs.chocolatey.org/en-us/troubleshooting#refreshenv-has-no-effect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants