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

Fix powershell 7.5.0-rc.1 hook - Conda.psm1 #14430

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 58 additions & 75 deletions conda/shell/condabin/Conda.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,23 @@ if (-not $CondaModuleArgs.ContainsKey('ChangePs1')) {
#>
function Get-CondaEnvironment {
[CmdletBinding()]
param();

begin {}
param()

process {
# NB: the JSON output of conda env list does not include the names
# of each env, so we need to parse the fragile output instead.
& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA env list | `
Where-Object { -not $_.StartsWith("#") } | `
Where-Object { -not $_.Trim().Length -eq 0 } | `
$condaArgs = @($Env:CONDA_EXE, $Env:_CE_M, $Env:_CE_CONDA, "env", "list") | Where-Object { $_ -ne $null -and $_ -ne '' }
& $condaArgs[0] $condaArgs[1..($condaArgs.Length-1)] |
Where-Object { -not $_.StartsWith("#") } |
Where-Object { -not $_.Trim().Length -eq 0 } |
ForEach-Object {
$envLine = $_ -split "\s+";
$Active = $envLine[1] -eq "*";
$envLine = $_ -split "\s+"
$Active = $envLine[1] -eq "*"
[PSCustomObject] @{
Name = $envLine[0];
Active = $Active;
Path = if ($Active) {$envLine[2]} else {$envLine[1]};
} | Write-Output;
Name = $envLine[0]
Active = $Active
Path = if ($Active) {$envLine[2]} else {$envLine[1]}
} | Write-Output
}
}

end {}
}

<#
Expand All @@ -63,23 +58,25 @@ function Enter-CondaEnvironment {
param(
[Parameter(Mandatory=$false)][switch]$Stack,
[Parameter(Position=0)][string]$Name
);
)

begin {
If ($Stack) {
$activateCommand = (& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA shell.powershell activate --stack $Name | Out-String);
} Else {
$activateCommand = (& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA shell.powershell activate $Name | Out-String);
$condaArgs = @($Env:CONDA_EXE, $Env:_CE_M, $Env:_CE_CONDA, "shell.powershell", "activate") | Where-Object { $_ -ne $null -and $_ -ne '' }

if ($Stack) {
$condaArgs += "--stack"
}

$condaArgs += $Name
$activateCommand = & $condaArgs[0] $condaArgs[1..($condaArgs.Length-1)] | Out-String

Write-Verbose "[conda shell.powershell activate $Name]`n$activateCommand";
Invoke-Expression -Command $activateCommand;
Write-Verbose "[conda shell.powershell activate $Name]`n$activateCommand"
if (-not [string]::IsNullOrWhiteSpace($activateCommand)) {
Invoke-Expression -Command $activateCommand
} else {
Write-Host "Failed to activate environment. The activate command returned empty."
}
}

process {}

end {}

}

<#
Expand All @@ -94,21 +91,18 @@ function Enter-CondaEnvironment {
#>
function Exit-CondaEnvironment {
[CmdletBinding()]
param();
param()

begin {
$deactivateCommand = (& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA shell.powershell deactivate | Out-String);
$condaArgs = @($Env:CONDA_EXE, $Env:_CE_M, $Env:_CE_CONDA, "shell.powershell", "deactivate") | Where-Object { $_ -ne $null -and $_ -ne '' }
$deactivateCommand = (& $condaArgs[0] $condaArgs[1..($condaArgs.Length-1)] | Out-String)

# If deactivate returns an empty string, we have nothing more to do,
# so return early.
if ($deactivateCommand.Trim().Length -eq 0) {
return;
return
}
Write-Verbose "[conda shell.powershell deactivate]`n$deactivateCommand";
Invoke-Expression -Command $deactivateCommand;
Write-Verbose "[conda shell.powershell deactivate]`n$deactivateCommand"
Invoke-Expression -Command $deactivateCommand
}
process {}
end {}
}

## CONDA WRAPPER ###############################################################
Expand All @@ -125,32 +119,23 @@ function Exit-CondaEnvironment {
conda install toolz
#>
function Invoke-Conda() {
# Don't use any explicit args here, we'll use $args and tab completion
# so that we can capture everything, INCLUDING short options (e.g. -n).
if ($Args.Count -eq 0) {
# No args, just call the underlying conda executable.
& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA;
$condaArgs = @($Env:CONDA_EXE, $Env:_CE_M, $Env:_CE_CONDA) | Where-Object { $_ -ne $null -and $_ -ne '' }
& $condaArgs[0] $condaArgs[1..($condaArgs.Length-1)]
}
else {
$Command = $Args[0];
if ($Args.Count -ge 2) {
$OtherArgs = $Args[1..($Args.Count - 1)];
} else {
$OtherArgs = @();
}
$Command = $Args[0]
$OtherArgs = $Args[1..($Args.Count - 1)]
switch ($Command) {
"activate" {
Enter-CondaEnvironment @OtherArgs;
Enter-CondaEnvironment @OtherArgs
}
"deactivate" {
Exit-CondaEnvironment;
Exit-CondaEnvironment
}

default {
# There may be a command we don't know want to handle
# differently in the shell wrapper, pass it through
# verbatim.
& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA $Command @OtherArgs;
$condaArgs = @($Env:CONDA_EXE, $Env:_CE_M, $Env:_CE_CONDA, $Command) + $OtherArgs | Where-Object { $_ -ne $null -and $_ -ne '' }
& $condaArgs[0] $condaArgs[1..($condaArgs.Length-1)]
}
}
}
Expand All @@ -171,29 +156,27 @@ function Expand-CondaEnv() {
param(
[string]
$Filter
);

$ValidEnvs = Get-CondaEnvironment;
$ValidEnvs `
| Where-Object { $_.Name -like "$filter*" } `
| ForEach-Object { $_.Name } `
| Write-Output;
$ValidEnvs `
| Where-Object { $_.Path -like "$filter*" } `
| ForEach-Object { $_.Path } `
| Write-Output;

)

$ValidEnvs = Get-CondaEnvironment
$ValidEnvs |
Where-Object { $_.Name -like "$filter*" } |
ForEach-Object { $_.Name } |
Write-Output
$ValidEnvs |
Where-Object { $_.Path -like "$filter*" } |
ForEach-Object { $_.Path } |
Write-Output
}

function Expand-CondaSubcommands() {
param(
[string]
$Filter
);

# Filter and output applicable subcommands
Invoke-Conda commands | Where-Object { $_ -like "$Filter*" } | Write-Output;
)

$condaArgs = @($Env:CONDA_EXE, $Env:_CE_M, $Env:_CE_CONDA, "shell.powershell", "commands") | Where-Object { $_ -ne $null -and $_ -ne '' }
& $condaArgs[0] $condaArgs[1..($condaArgs.Length-1)] | Where-Object { $_ -like "$Filter*" } | Write-Output
}

function TabExpansion($line, $lastWord) {
Expand All @@ -202,11 +185,11 @@ function TabExpansion($line, $lastWord) {
switch -regex ($lastBlock) {
# Pull out conda commands we recognize first before falling through
# to the general patterns for conda itself.
"^conda activate (.*)" { Expand-CondaEnv $lastWord; break; }
"^etenv (.*)" { Expand-CondaEnv $lastWord; break; }
"^conda activate (.*)" { Expand-CondaEnv $lastWord; break }
"^etenv (.*)" { Expand-CondaEnv $lastWord; break }

# If we got down to here, check arguments to conda itself.
"^conda (.*)" { Expand-CondaSubcommands $lastWord; break; }
"^conda (.*)" { Expand-CondaSubcommands $lastWord; break }

# Finally, fall back on existing tab expansion.
default {
Expand Down Expand Up @@ -236,15 +219,15 @@ if ($CondaModuleArgs.ChangePs1) {
} else {
function CondaPromptBackup() {
# Restore a basic prompt if the definition is missing.
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
}
}

function global:prompt() {
if ($Env:CONDA_PROMPT_MODIFIER) {
$Env:CONDA_PROMPT_MODIFIER | Write-Host -NoNewline
}
CondaPromptBackup;
CondaPromptBackup
}
}

Expand Down
19 changes: 19 additions & 0 deletions news/news/14430-bugfix-for-powershell-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fixed broken `PowerShell 7.5.0-rc.1` hook commands. (#14430)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
Loading