Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Use dotnet tool to find out if a specific tool is installed on an environment
# Preface:
# dotnet tool install -g will return an error code when the tool is already installed in the system (at the same location)
# adding a test like below, will prevent the error
# this is mostly needed in a CI/CD environment where you don't want to break your pipeline if the tool was installed already.
# find if stryker is installed
$list = (dotnet tool list -g)
# echo the list
# $list
$strykerFound = ($list | Where-Object {$_.Split(' ')[0] -eq "dotnet-stryker"})
if ($null -ne $StrykerFound) {
Write-Host "Installing Stryker"
dotnet tool install dotnet-stryker -g
else {
Write-Host "Stryker is already installed"
# stop powershell from reporting an error code
$global:LASTEXITCODE = 0
Copy link

benmccallum commented Jul 8, 2021

This is painful; I've also run into it!

I'm thinking about just installing the tool in a local path:

dotnet tool install dotnet-stryker --tool-path .
./dotnet-stryker x y z

I tried to see if this still was fast and used the package cache, but the output wasn't clear when I used --verbosity d. So instead I turned off my Wi-Fi and it still installed, so I think I'm just gonna do this in my parallel builds on the same machine and save myself having to do what you've done above.

Copy link

rajbos commented Jul 8, 2021

The thing is, if it is already installed, installing it again returns an error, which is not helpful in a CI/CD pipeline.
That's why I'm checking it first.

Added that reasoning to the gist. Thanks!

Copy link

benmccallum commented Jul 26, 2021

Yea 100% understood, I came from a similar problem and your blog post.

To be clear, I meant install it into a local path that is unique (e.g. created/destroyed by your CI/CD pipeline) each run, therefore avoiding all the additional check you're doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment