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 exits the batch script prematurely #1461

Closed
IvanBoyko opened this issue Nov 30, 2017 · 9 comments
Closed

refreshenv exits the batch script prematurely #1461

IvanBoyko opened this issue Nov 30, 2017 · 9 comments

Comments

@IvanBoyko
Copy link

IvanBoyko commented Nov 30, 2017

What You Are Seeing?

When refreshenv is executed as part of the batch script it exits the script, so no further commands are executed.

What is Expected?

I expect batch script to continue after running refreshenv

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

Here is my batch script:

@rem Install Python
choco install -y python3 --version 3.5.4
refreshenv
python --version
@rem further steps like python modules installation
@rem ...

Output Log

Output from running the script:

C:\>install-python.bat

C:\>choco install -y python3 --version 3.5.4
Chocolatey v0.10.8
Installing the following packages:
python3
By installing you accept licenses for the packages.
Progress: Downloading chocolatey-core.extension 1.3.3... 100%
Progress: Downloading python3 3.5.4... 100%

chocolatey-core.extension v1.3.3 [Approved]
chocolatey-core.extension package files install completed. Performing other installation steps.
 Installed/updated chocolatey-core extensions.
 The install of chocolatey-core.extension was successful.
  Software installed to 'C:\ProgramData\chocolatey\extensions\chocolatey-core'

python3 v3.5.4 [Approved]
python3 package files install completed. Performing other installation steps.
Installing 64-bit python3...
python3 has been installed.
Installed to: 'C:\Python35'
  python3 can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).
 The install of python3 was successful.
  Software installed as 'exe', install location is likely default.

Chocolatey installed 2/2 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

C:\>refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..

So nothing after refreshenv is executed

Environment

I test this in the Windows container, but I think it will be the same on any fresh Windows 10 machine:

docker run -it --rm microsoft/windowsservercore:10.0.14393.1884 cmd

(at the moment of writing this is the latest image, but the same problem was seen in older versions)

@KurtTheBerner
Copy link

You need to use call:
call RefreshEnv.cmd

@IvanBoyko
Copy link
Author

IvanBoyko commented Nov 30, 2017

Fantastic! Problem sorted out. Many thanks for the help.

Probably this message printed by choco install can be updated to mention call?

Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).

@ferventcoder
Copy link
Member

That message is for non-automated scenarios and your use of refreshenv is in a batch script where it is typical you would use call in front of all of those commands.

@ferventcoder
Copy link
Member

If you were using refreshenv in PowerShell then you would need to have the profile installed and could call it as well (refreshenv is an alias for Update-SessionEnvironment in PowerShell).

@ferventcoder
Copy link
Member

We could add this to scripting docs - add a comment to #1303.

@gsfakianakis
Copy link

gsfakianakis commented Dec 15, 2018

Hello,

I have a similar problem with choco install nvm and Travis.

nvm is installed but when I run "nvm -v" I have an output:
"The command "nvm -v" failed and exited with 127 during ."

I run the also cmd.exe /c "call RefreshEnv.cmd" before

To Sum up:
choco install nvm
cmd.exe /c "call RefreshEnv.cmd"
nvm -v

Any idea ?

@IvanBoyko
Copy link
Author

@gsfakianakis , you are executing RefreshEnv.cmd within a separate cmd process which exits, and when you run nmv -v - it runs in the main cmd process, not in the one with refreshed env.
How about just:

choco install nvm
call RefreshEnv.cmd
nvm -v

If you shell is PowerShell, not cmd, try as & RefreshEnv.cmd

I haven't tested, as I don't use Windows anymore, but you got the idea.

@gsfakianakis
Copy link

Still same issue.

jpalmerpivotal added a commit to cloudfoundry/cli-ci that referenced this issue Sep 30, 2020
It seems that in `bat` files, we should be using `call RefreshEnv.cmd`
as per the chocolatey team
[recommendation](chocolatey/choco#1461).

This change only affects one file to validate ci works after this
change.

[#174891518](https://www.pivotaltracker.com/story/show/174891518)

Co-authored-by: Sebastian Vidrio <svidrio@vmware.com>
v1v added a commit to v1v/beats that referenced this issue Nov 23, 2020
@Jafari-Amir

This comment was marked as off-topic.

@chocolatey chocolatey locked as resolved and limited conversation to collaborators Nov 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants