Skip to content

Commit

Permalink
Give a better error on Windows if python isn't installed
Browse files Browse the repository at this point in the history
Before:

```
PS C:\Users\vboxuser\rust> ./x
x.ps1

PS C:\Users\vboxuser\rust>
```

After:
```
PS C:\Users\vboxuser\rust> ./x
x.ps1

C:\Users\vboxuser\rust\x.ps1 : C:\Users\vboxuser\rust\x.ps1: error: did not find python installed
help: consider installing it from https://www.python.org/downloads/windows/
At line:1 char:1
+ ./x
+ ~~~
    + CategoryInfo          : NotInstalled: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,x.ps1
```

The existing message from the shell script is already decent and I decided not to change it:
```
$ ./x
Python was not found but can be installed from the Microsoft Store: ms-windows-store://pdp/?productid=9NJ46SX7X90P
```
  • Loading branch information
jyn514 committed Jun 24, 2023
1 parent dd314f6 commit fa7e965
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions x.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ foreach ($arg in $args) {
}

function Get-Application($app) {
return Get-Command $app -ErrorAction SilentlyContinue -CommandType Application
$cmd = Get-Command $app -ErrorAction SilentlyContinue -CommandType Application | Select-Object -First 1
if ($cmd.source -match '.*AppData\\Local\\Microsoft\\WindowsApps\\.*exe') {
# Windows for some reason puts a `python3.exe` executable in PATH that just opens the windows store.
# Ignore it.
return $false
}
return $cmd
}

function Invoke-Application($application, $arguments) {
Expand Down Expand Up @@ -51,5 +57,7 @@ if (($null -ne $found) -and ($found.Length -ge 1)) {
Invoke-Application $python $xpy_args
}

Write-Error "${PSCommandPath}: error: did not find python installed"
$msg = "${PSCommandPath}: error: did not find python installed`n"
$msg += "help: consider installing it from https://www.python.org/downloads/"
Write-Error $msg -Category NotInstalled
Exit 1

0 comments on commit fa7e965

Please sign in to comment.