prefix-dev / pixi

Package management made easy
https://pixi.sh
BSD 3-Clause "New" or "Revised" License
4.29k stars 272 forks source link

`pixi shell-hook` on powershell does not inject package activation scripts #3354

Open danx12 opened 1 month ago

danx12 commented 1 month ago

Checks

Reproducible example

pixi.toml

[project]
authors = [john]
channels = ["conda-forge"]
name = "pixi-issue-shell"
platforms = ["win-64"]
version = "0.1.0"

[dependencies]
go = ">=1.23.6,<2"
$ pixi shell-hook --shell=powershell
$OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.Encoding]::UTF8
${Env:Path} = "C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Library/mingw-w64/bin;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Library/usr/bin;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Library/bin;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Scripts;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\wbem;C:\WINDOWS\system32\windowspowershell\v1.0\;C:\WINDOWS\system32\openssh\;c:\program files\dotnet\;c:\program files (x86)\webex\plugins;c:\program files\git\cmd;c:\program files\powershell\7\;c:\program files\go\bin;c:\program files\docker engine\docker-25.0.3\docker;C:\Program Files\RedHat\Podman\;C:\Users\john\scoop\apps\gpg\current\bin;C:\Users\john\scoop\apps\python312\current\Scripts;C:\Users\john\scoop\apps\python312\current;C:\Users\john\.pixi\bin;C:\Users\john\scoop\apps\nvm\current;C:\Users\john\scoop\apps\nvm\current\nodejs\nodejs;C:\Users\john\scoop\apps\openjdk11\current\bin;C:\Users\john\scoop\shims;C:\Users\john\AppData\Local\Microsoft\WindowsApps;C:\Users\john\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\john\Infineon\LauncherService;C:\it-servs\tools\Chocolatey\bin;C:\Users\john\go\bin"
${Env:CONDA_PREFIX} = "C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default"
${Env:PIXI_EXE} = "C:\Users\john\.pixi\bin\pixi.exe"
${Env:PIXI_PROJECT_ROOT} = "C:\Users\john\Documents\Repos\pixi-issue-shell"
${Env:PIXI_PROJECT_VERSION} = "0.1.0"
${Env:PIXI_IN_SHELL} = "1"
${Env:PIXI_PROJECT_NAME} = "pixi-issue-shell"
${Env:PIXI_PROJECT_MANIFEST} = "C:\Users\john\Documents\Repos\pixi-issue-shell\pixi.toml"
${Env:CONDA_DEFAULT_ENV} = "pixi-issue-shell"
${Env:PIXI_ENVIRONMENT_NAME} = "default"
${Env:PIXI_ENVIRONMENT_PLATFORMS} = "win-64"
${Env:PIXI_PROMPT} = "(pixi-issue-shell) "
$ pixi info
System
------------
       Pixi version: 0.42.1
           Platform: win-64
   Virtual packages: __win=10.0.22631=0
                   : __cuda=12.5=0
                   : __archspec=1=skylake
          Cache dir: C:\Users\john\AppData\Local\rattler\cache
       Auth storage: C:\Users\john\.rattler\credentials.json
   Config locations: C:\Users\john\.pixi\config.toml

Global
------------
            Bin dir: C:\Users\john\.pixi\bin
    Environment dir: C:\Users\john\.pixi\envs
       Manifest dir: C:\Users\john\.pixi\manifests\pixi-global.toml

Project
------------
               Name: pixi-issue-shell
            Version: 0.1.0
      Manifest file: C:\Users\john\Documents\Repos\pixi-issue-shell\pixi.toml
       Last updated: 13-03-2025 18:18:01

Environments
------------
        Environment: default
           Features: default
           Channels: conda-forge
   Dependency count: 1
       Dependencies: go
   Target platforms: win-64

Issue description

pixi shell-hook --shell=powershell does not inject the package activation scripts contained in the etc/conda/activate.d directory.

Note that I originally reproduced this issue with a privately built package that also uses activation scripts, not with go.

Compared to using cmd, where the @CALL at the end of the script will call the activation script.

$ pixi shell-hook --shell=cmd
@chcp 65001 > nul
@SET "Path=C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Library/mingw-w64/bin;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Library/usr/bin;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Library/bin;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\Scripts;C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\wbem;C:\WINDOWS\system32\windowspowershell\v1.0\;C:\WINDOWS\system32\openssh\;c:\program files\dotnet\;c:\program files (x86)\webex\plugins;c:\program files\git\cmd;c:\program files\powershell\7\;c:\program files\go\bin;c:\program files\docker engine\docker-25.0.3\docker;C:\Program Files\RedHat\Podman\;C:\Users\john\scoop\apps\gpg\current\bin;C:\Users\john\scoop\apps\python312\current\Scripts;C:\Users\john\scoop\apps\python312\current;C:\Users\john\.pixi\bin;C:\Users\john\scoop\apps\nvm\current;C:\Users\john\scoop\apps\nvm\current\nodejs\nodejs;C:\Users\john\scoop\apps\openjdk11\current\bin;C:\Users\john\scoop\shims;C:\Users\john\AppData\Local\Microsoft\WindowsApps;C:\Users\john\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\john\Infineon\LauncherService;C:\it-servs\tools\Chocolatey\bin;C:\Users\john\go\bin"
@SET "CONDA_PREFIX=C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default"
@SET "PIXI_PROJECT_MANIFEST=C:\Users\john\Documents\Repos\pixi-issue-shell\pixi.toml"
@SET "PIXI_EXE=C:\Users\john\.pixi\bin\pixi.exe"
@SET "PIXI_PROJECT_ROOT=C:\Users\john\Documents\Repos\pixi-issue-shell"
@SET "PIXI_PROJECT_NAME=pixi-issue-shell"
@SET "PIXI_PROJECT_VERSION=0.1.0"
@SET "PIXI_IN_SHELL=1"
@SET "CONDA_DEFAULT_ENV=pixi-issue-shell"
@SET "PIXI_ENVIRONMENT_NAME=default"
@SET "PIXI_ENVIRONMENT_PLATFORMS=win-64"
@SET "PIXI_PROMPT=(pixi-issue-shell) "
@CALL "C:\Users\john\Documents\Repos\pixi-issue-shell\.pixi\envs\default\etc/conda/activate.d\activate_z60-go.bat"

@PROMPT (pixi-issue-shell) $P$G

Somewhat related to https://github.com/prefix-dev/pixi/issues/1527 but in this case for powershell.

Expected behavior

Running pixi shell-hook --shell=powershell also contains calls to run the package activation scripts.

wolfv commented 1 month ago

Hey @danx12 - the problem is that most packages on conda-forge or in the conda ecosystem only ship activation scripts for bash or CMD.EXE (.bat) files. PowerShell does not know how to source them (afaik). The trick that we are doing in pixi shell is that we execute the scripts, record the new environment variables (from cmd.exe or bash), and export the new env vars.

shell-hook on the other hand does just straight-forward activate the env.

One way you could work around it might be to do pixi run powershell - which would execute powershell in the context of the activated environment.

We could also add a flag to shell-hook that would just export variables in the same way as we do for run or shell.

If you would like to give a PR a try, it could be done quite easily and I would be happy to guide you!