-
Notifications
You must be signed in to change notification settings - Fork 908
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
When no hook scripts are installed you cannot install/upgrade a package on a system using PowerShell 2 #3088
Comments
You can reproduce this in Windows 10 by enabling the PowerShellV2 feature (in an elevated shell): |
I really wish it was as simple as this. I have not been able to reproduce even when launching chocolatey from PowerShell 2 on Windows 10. |
The issue is caused by the PowerShell feature "ForEach statement does not iterate over $null" introduced in PS v3 (see https://devblogs.microsoft.com/powershell/new-v3-language-features/). This causes the code in chocolateyScriptRunner.ps1 which runs the hook scripts to behave differently depending on whether it's executed in PS v2 or PS v3. See this code in chocolateyScriptRunner.ps1 (comments added by me):
To fix the issue, I suggest to explicitly check for $null before iterating over "$preRunHookScripts)" or "$postRunHookScripts". for example:
I've tested the fix in a Windows 7 / PS v2 enviroment, it works as expected |
@hhonisch thank you for that information. I knew it was a change in behaviour of PowerShell v3, but couldn't find the post about the change. To bring some information that @mymaestro provided in the reddit thread, and in order to try to keep the information up to date in the issue here now that we have it, here is interesting and relevant information from the reddit thread:
The part that I find interesting is that the Host version is correctly detecting that it's Windows 10, but the PowerShell version is dropping to 2.0 which is something that I cannot reproduce. However, when I try to find information on this particular build (21390.2025), it all seems to point to it being an insider build from mid 2021. Which might suggest a bugged Insider build that has since been fixed. I have a fix that is very similar to what @hhonisch suggested, but just forgoes both the PSBoundParameters check and the null check. I'll get it updated and pushed up, but likely won't make it into Chocolatey until 2.0.0 is released. Will also be adding some pester tests to ensure we don't regress on this, and more properly test the chocolateyScriptRunner script. |
PowerShell 2 had the behaviour that foreach would iterate over a null variable (https://devblogs.microsoft.com/powershell/new-v3-language-features/). This has resulted in an issue if no hook scripts are passed in, then chocolateyScriptRunner would cause an error. This update changes this to do a nullcheck like we already had for the packageScript parameter.
PowerShell 2 had the behaviour that foreach would iterate over a null variable (https://devblogs.microsoft.com/powershell/new-v3-language-features/). This has resulted in an issue if no hook scripts are passed in, then chocolateyScriptRunner would cause an error. This update changes this to do a nullcheck like we already had for the packageScript parameter.
PowerShell 2 had the behaviour that foreach would iterate over a null variable (https://devblogs.microsoft.com/powershell/new-v3-language-features/). This has resulted in an issue if no hook scripts are passed in, then chocolateyScriptRunner would cause an error. This update changes this to do a nullcheck like we already had for the packageScript parameter.
PowerShell 2 had the behaviour that foreach would iterate over a null variable (https://devblogs.microsoft.com/powershell/new-v3-language-features/). This has resulted in an issue if no hook scripts are passed in, then chocolateyScriptRunner would cause an error. This update changes this to do a nullcheck like we already had for the packageScript parameter.
Linked to #865c2w28t |
PowerShell 2 had the behaviour that foreach would iterate over a null variable (https://devblogs.microsoft.com/powershell/new-v3-language-features/). This has resulted in an issue if no hook scripts are passed in, then chocolateyScriptRunner would cause an error. This update changes this to do a nullcheck like we already had for the packageScript parameter.
PowerShell 2 had the behaviour that foreach would iterate over a null variable (https://devblogs.microsoft.com/powershell/new-v3-language-features/). This has resulted in an issue if no hook scripts are passed in, then chocolateyScriptRunner would cause an error. This update changes this to do a nullcheck like we already had for the packageScript parameter.
🎉 This issue has been resolved in version 1.4.0 🎉 The release is available on: Your GitReleaseManager bot 📦🚀 |
how can i update version 1.2 to 1.4? because 1.2 gives me the error. |
Hi @zandadoum, that is an excellent question. I have tested the two following methods with success:
|
Checklist
What You Are Seeing?
On a system with just PowerShell 2 (and somehow at least one Windows 10 system with PowerShell 2 enabled), when you try to install or upgrade any package, you receive an error:
What is Expected?
Packages should be able to be installed/upgraded successfully.
How Did You Get This To Happen?
System Details
Installed Packages
Output Log
Additional Context
No response
The text was updated successfully, but these errors were encountered: