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

Chocolately installs and then fails to find choco.exe but RC is 0 #170

Open
6 tasks done
UNiXMIT opened this issue Dec 5, 2024 · 7 comments
Open
6 tasks done

Chocolately installs and then fails to find choco.exe but RC is 0 #170

UNiXMIT opened this issue Dec 5, 2024 · 7 comments
Labels
0 - Waiting on User Insufficient information for issue or PR, issue may be closed if no response from user

Comments

@UNiXMIT
Copy link

UNiXMIT commented Dec 5, 2024

Checklist

  • I confirm there are no unresolved issues reported on the Chocolatey Status page.
  • I have verified this is the correct repository for opening this issue.
  • I have verified no other issues exist related to my problem.
  • I have verified this is not an issue for a specific package.
  • I have verified this issue is not security related.
  • I confirm I am using official, and not unofficial, or modified, Chocolatey products.

What You Are Seeing?

When the following task is run

- name: Install Chocolatey
  chocolatey.chocolatey.win_chocolatey:
    name: chocolatey
    state: present
  retries: 3
  delay: 5
  register: result
  until: result.rc == 0

I get this error even though the return code is 0

TASK [Install Chocolatey] ******************************************************
11:15:20 AM
[WARNING]: Chocolatey was missing from this system, so it was installed during
11:15:20 AM
this task run.
11:15:20 AM
fatal: [3.8.152.126]: FAILED! => changed=true 
11:15:20 AM
  attempts: 1
11:15:20 AM
  command: ''
11:15:20 AM
  msg: Failed to find choco.exe, make sure it is added to the PATH or the env var 'ChocolateyInstall' is set
11:15:20 AM
  rc: 0

Does the installer not set choco.exe on the PATH?

What is Expected?

I expect the install of choco via the ansible task to work and not to fail my whole play.

How Did You Get This To Happen?

Task to use:

- name: Install Chocolatey
  chocolatey.chocolatey.win_chocolatey:
    name: chocolatey
    state: present
  retries: 3
  delay: 5
  register: result
  until: result.rc == 0

System Details

RHEL 9.4

Installed Packages

Nothing yet as I am initially trying to install choco.

Output Log

TASK [Install Chocolatey] ******************************************************
11:15:20 AM
[WARNING]: Chocolatey was missing from this system, so it was installed during
11:15:20 AM
this task run.
11:15:20 AM
fatal: [3.8.152.126]: FAILED! => changed=true 
11:15:20 AM
  attempts: 1
11:15:20 AM
  command: ''
11:15:20 AM
  msg: Failed to find choco.exe, make sure it is added to the PATH or the env var 'ChocolateyInstall' is set
11:15:20 AM
  rc: 0

Additional Context

No response

@UNiXMIT UNiXMIT added the Bug Issues where something has happened which was not expected or intended label Dec 5, 2024
@pauby
Copy link
Member

pauby commented Dec 5, 2024

win_chocolatey uses Chocolatey CLI to "manage packages". If it's not there, you can't expect it to install anything, including itself.

@pauby pauby added 0 - Waiting on User Insufficient information for issue or PR, issue may be closed if no response from user and removed Bug Issues where something has happened which was not expected or intended labels Dec 5, 2024
@UNiXMIT
Copy link
Author

UNiXMIT commented Dec 5, 2024

win_chocolatey uses Chocolatey CLI to "manage packages". If it's not there, you can't expect it to install anything, including itself.

I was following the advice of #40 which was closed with the solution I am trying. I'll use the powershell script instead.

@pauby
Copy link
Member

pauby commented Dec 5, 2024

I don't see that documented (I could be missing it). Apologies @un@UNiXMIT.

@vexx32 you left this comment which indicates this should work?

@UNiXMIT
Copy link
Author

UNiXMIT commented Dec 5, 2024

I changed it to run the powershell script at instance creation and that works OK anyway.

@pauby
Copy link
Member

pauby commented Dec 5, 2024

I can see one of the first things this does is call the Install-Chocolatey function which will install Chocolatey CLI.

I wonder if this is the PATH being set but not being pulled into subsequent tasks. I'll leave this for @vexx32 or @Windos to look at.

Again, apologies @UNiXMIT I was too quick off the bat to respond.

This looks to be related to #118

@vexx32
Copy link
Member

vexx32 commented Dec 5, 2024

@UNiXMIT hi! what is the client machine running? and just to make sure, what version of the collection are you currently using?

The only way this could happen that I can see right now is:

  • Chocolatey's installer completes
  • Chocolatey is not installed to a proper location at all or something deletes it immediately
  • Then the task tries to verify that the installation worked and fails out

Because... even if it's not on PATH, as long as it's in the usual location, we will find it -- see here:

function Get-ChocolateyCommand {
<#
.SYNOPSIS
Retrieves a CommandInfo object for `choco.exe` if it is present on the system.
.DESCRIPTION
Returns either a CommandInfo object which contains the path the `choco.exe`
or registers a task failure and exits if it cannot be found.
#>
[CmdletBinding()]
param(
# If provided, does not terminate the task run when choco.exe is not found.
[Parameter()]
[switch]
$IgnoreMissing
)
$command = Get-Command -Name choco.exe -CommandType Application -ErrorAction SilentlyContinue -TotalCount 1
if (-not $command) {
$installDir = if ($env:ChocolateyInstall) {
$env:ChocolateyInstall
}
else {
"$env:SYSTEMDRIVE\ProgramData\Chocolatey"
}
$command = Get-Command -Name "$installDir\bin\choco.exe" -CommandType Application -ErrorAction SilentlyContinue
if (-not ($command -or $IgnoreMissing)) {
$message = "Failed to find Chocolatey installation, make sure choco.exe is in the PATH env value"
Assert-TaskFailed -Message $message
}
}
$command
}

This is called from here, after running the installation script for choco:

if (-not $Module.CheckMode) {
$scriptFile = New-Item -Path (Join-Path $Module.TmpDir -ChildPath 'chocolateyInstall.ps1') -ItemType File
$installScript | Set-Content -Path $scriptFile
# These commands will be sent over stdin for the PowerShell process, and will be read line by line,
# so we must join them on \r\n line-feeds to have them read as separate commands.
$commands = @(
'$ProgressPreference = "SilentlyContinue"'
'& "{0}"' -f $scriptFile
) -join "`r`n"
$result = Run-Command -Command "powershell.exe -" -Stdin $commands -Environment $environment
if ($result.rc -ne 0) {
$message = "Chocolatey bootstrap installation failed."
Assert-TaskFailed -Message $message -CommandResult $result
}
if (-not $SkipWarning) {
$Module.Warn("Chocolatey was missing from this system, so it was installed during this task run.")
}
}
Set-TaskResultChanged
# locate the newly installed choco.exe
$chocoCommand = Get-ChocolateyCommand -IgnoreMissing

@vexx32
Copy link
Member

vexx32 commented Dec 5, 2024

Okay, did a little more digging as well... I think the conclusion in the other issue is that the .NET 4.8 install was attempted and possibly failed. That failure case could potentially result in our installation process not registering an error exit code due to how PowerShell handles a throw and the way we run things. That would plausibly result in this situation as well.

... There might be an argument to be made that the .NET 4.8 installation failing during the choco install should just ensure that an error exit code be set, rather than just throwing, and we should be careful to make sure the error is rethrown all the way to the top. That would be an issue to resolve in the chocolateyInstaller.psm1 that's part of choco's installation process itself. I.e., it should make careful use of $PSCmdlet.ThrowTerminatingError() instead of just throw, and it should use $host.SetShouldExit() with an exit code as well, so that we can detect from this collection that that installation actually failed.

In the meantime the best solution is likely to follow this pattern and ensure .NET 4.8 is installed and a reboot is performed before installing Chocolatey. We can handle that better in future, I think, but that's gonna take some additional careful scripting in the choco installer itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0 - Waiting on User Insufficient information for issue or PR, issue may be closed if no response from user
Projects
None yet
Development

No branches or pull requests

3 participants