choco icon indicating copy to clipboard operation
choco copied to clipboard

$Env variables like e.g. `ChocolateyForce`: Please follow the PowerShell basics!

Open schittli opened this issue 3 years ago • 1 comments

Good evening

What You Are Seeing?

Chocolatey explains in the docs functions for Variables like $Env:ChocolateyForce:

ChocolateyForce - Was --force passed? (0.9.10+)

Actually, choco 1.1.0 behaves like this 😞:

  • If --force was passed, then $Env:ChocolateyForce is set to $True
  • Otherwise, $Env:ChocolateyForce is $Null 😢

What is Expected?

Please, follow the PowerShell module developers' basics and treat the ChocolateyForce variable like the common switch parameter:

  • If --force was passed, then $Env:ChocolateyForce is set to $True
  • Otherwise, $Env:ChocolateyForce should be set to $False

A second, very general suggestion:

Please, please, always follow the PowerShell recommendations for module developers.

For example, choco also constantly displays a lot of information that is normally only displayed in PowerShell when -verbose is activated.

It would make Chocolatey look much more professional if package installations did not produce dozens of lines of output when everything is OK, but simply listed the packages.

If Chocolatey doesn't stick to the really very good guidelines, then it doesn't make Choco cool, it harms the product:

We are surprised and quite disappointed that Chocolatey uses PowerShell as a foundation, but that Chocolatey behaves very often different than the PowerShell basics recommends for module developers 😢

The consequence is three times fatal:

  • Package developers who already know PowerShell have to debug errors over and over again only to find that Chocolatey does not behave the way thousands of other PowerShell modules do.
  • Package developers who don't know PowerShell learn basics they can't reuse in other projects.
  • This makes the choco package development tedious because not only do you have to study how to solve problems with PowerShell, but you also have to do your research and check if Chocolatey does it differently.

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

Just use $Env:ChocolateyForce in your Code like any other [Switch] Parameter. You'll get Exceptions because $Env:ChocolateyForce can be $null, too 😞

System Details

  • OS Build (In PowerShell run: 10.0.19044.0
  • Windows PowerShell version:
Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1682
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
``
* Chocolatey version: 1.1.0

### Output Log

It looks like this and debugging gets annoying of one uses `$Env:ChocolateyForce` as any other PowerShell `[Switch]` Variable 😢: 

ERROR: Cannot convert value "" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. The install of app-chrome-tom was NOT successful. Error while running 'C:\ProgramData\chocolatey\lib\…\tools\ChocolateyInstall.ps1'. See log for details.

schittli avatar Aug 04 '22 20:08 schittli

Otherwise, $Env:ChocolateyForce is $Null 😢

Environment variables are strings, and in PowerShell, empty or null strings translate to false https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_booleans?view=powershell-5.1#converting-from-scalar-types

if ($Env:ChocolateyForce) { ... } and if (-not $Env:ChocolateyForce) { ... } and ($Env:ChocolateyForce -eq $true) { ... } should all work for example.

But for your example, it looks like whatever it is that you are passing in wants a boolean value explicitly, and powershell is having trouble for whatever reason converting the environment variable to a boolean. Potentially, you could try casting it to a bool with [bool]?

Please, please, always follow the PowerShell recommendations for module developers. For example, choco also constantly displays a lot of information that is normally only displayed in PowerShell when -verbose is activated.

Chocolatey CLI is not a powershell module. Chocolatey CLI is an executable program that uses PowerShell internally for package automation scripts.

If you want to use Chocolatey CLI as a PowerShell module, you may want to look into using something like ChocolateyGet or Foil

If you are actually talking about the Chocolatey CLI PowerShell host that runs the package automation scripts, then can you please provide a bit more information about what you are talking about?

It would make Chocolatey look much more professional if package installations did not produce dozens of lines of output when everything is OK, but simply listed the packages.

This is the output I get from reinstall git with apt on Debian, with 12 lines of output:

apt install git --reinstall
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
Need to get 5,527 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 https://deb.debian.org/debian bullseye/main amd64 git amd64 1:2.30.2-1 [5,527 kB]
Fetched 5,527 kB in 1s (4,667 kB/s)
(Reading database ... 154190 files and directories currently installed.)
Preparing to unpack .../git_1%3a2.30.2-1_amd64.deb ...
Unpacking git (1:2.30.2-1) over (1:2.30.2-1) ...
Setting up git (1:2.30.2-1) ...

And here is the output from reinstalling git on Chocolatey CLI, with 16 lines of output:

choco install git -f
Chocolatey v1.1.0
Installing the following packages:
git
By installing, you accept licenses for the packages.
git v2.37.1 already installed. Forcing reinstall of version '2.37.1'.
 Please use upgrade if you meant to upgrade to a new version.
[NuGet] Uninstalling 'git 2.37.1' might cause 'gitextensions 3.5.4' to be broken.
Progress: Downloading git 2.37.1... 100%

git v2.37.1 (forced)
git package files install completed. Performing other installation steps.
 The install of git was successful.
  Software installed to 'C:\ProgramData\chocolatey\lib\git'

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

I wouldn't call the Chocolatey output dozens of lines. Of course, it could get up to dozens lines if multiple packages are being installed/upgraded/uninstalled, but it still would be in the same ballpark as apt

Since Chocolatey is a package manager, it should be compared to other package managers in terms of output. The information that is logged to the screen is there for good reason, it either helps troubleshooting if something goes wrong, or it helps let the user know where it is in the process of completing the actions.

If you have specific things that you think may not need to be logged to the console, perhaps open a discussion about it or jump on the Discord?

  • Package developers who already know PowerShell have to debug errors over and over again only to find that Chocolatey does not behave the way thousands of other PowerShell modules do.
  • Package developers who don't know PowerShell learn basics they can't reuse in other projects.
  • This makes the choco package development tedious because not only do you have to study how to solve problems with PowerShell, but you also have to do your research and check if Chocolatey does it differently.

I'm slightly confused as to what you are referring to.

Admittedly, I'm not a PowerShell expert, but everything I have learned about PowerShell writing package automation scripts (like chocolateyInstall.ps1) has transferred without changes to general PowerShell usage, and vise versa.

TheCakeIsNaOH avatar Aug 04 '22 22:08 TheCakeIsNaOH