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

Persistent manifest path/context when in a pixi shell #1038

Closed
synapticarbors opened this issue Mar 21, 2024 · 13 comments · Fixed by #1080
Closed

Persistent manifest path/context when in a pixi shell #1038

synapticarbors opened this issue Mar 21, 2024 · 13 comments · Fixed by #1080
Labels
enhancement New feature or request

Comments

@synapticarbors
Copy link

Problem description

After activating a pixi shell, pixi should maintain the context of that shell even if you move to another directory. @baszalmstra described the desired behavior nicely in #1021 (reply in thread):

...if you are in a pixi shell you are always in the context of the activated project regardless of where you navigate on your disk. If the project discovery locates a different manifest than the activated one it will show a warning that it is not using the one it discovered. This means using pixi run whilest in an activated shell will also always refer to the original manifest as well.

Activating a shell within in a shell will not be allowed regardless of where you are on disk. You should exit the initial shell first.

This way you can never mix projects unless you exit the shell but it does allow sort of global activation without too much effort. I think this kinda makes sense because the shell is already sort of in the context of the original project. I think this approach could remove a lot of the ambiguity that we are afraid of.

Locking the context of the subshell would allow a user to run commands like pixi list, pixi run, etc from outside of the project directory without having to specify the --manifest-path flag.

It is also desirable that the context should include the specific environment if a non-default env was selected when starting the shell (e.g. pixi shell -e fooenv). This is related to #893.

@synapticarbors synapticarbors added the enhancement New feature or request label Mar 21, 2024
@dhirschfeld
Copy link
Contributor

I think this would enable all the use-cases I care about (which would allow me to switch to pixi as my primary packaging tool).

@dhirschfeld
Copy link
Contributor

It would be nice if it were possible to show (part of) the manifest-path and environment in the prompt of the shell so the user was more aware of which project the shell was associated with.

Not necessary, but a nice-to-have IMHO.

@synapticarbors
Copy link
Author

Currently, my shell (macos running zsh with omz) prompt shows me the (<project name>) if in an activated pixi shell using the default environment and (<project name>:<env name>) if using a non-default env.

@dhirschfeld
Copy link
Contributor

I don't have any pixi info in my prompt but I'm using starship.rs. Maybe I need some extra config? Or maybe it's just not supported (I'll have to look into that...)

@chawyehsu
Copy link
Contributor

chawyehsu commented Mar 22, 2024

I don't have any pixi info in my prompt but I'm using starship.rs.

I'm also using starship, project env name will show after entering the env via pixi shell

DESKTOP in pixi on  win-arm64 is 📦 v0.16.1 via 🦀 v1.75.0 via 🅒 pixi

notice 🅒 pixi. It should be supported out-of-box

@dhirschfeld
Copy link
Contributor

project env name will show after entering the env via pixi shell

I confirm that works for me! 🚀

Makes sense if you think of opening a shell as being like activating an environment (which it would be if this proposal were implemented!)

@tdejager
Copy link
Contributor

The only question I have is what should happen if you want to run tasks from another 'pixi.toml' that's is not the original activated one :)?

@synapticarbors
Copy link
Author

The only question I have is what should happen if you want to run tasks from another 'pixi.toml' that's is not the original activated one :)?

I think in that case, you would just have to explicitly use the --manifest-path flag to point pixi at the correct project. That would be the default behavior if you were not inside that other project currently. If you are in that project's directory and don't override with the flag, then Bas suggested "if the project discovery locates a different manifest than the activated one it will show a warning that it is not using the one it discovered".

@abkfenris
Copy link
Contributor

abkfenris commented Mar 22, 2024

Could something like having pixi read from environment variables for manifest path (PIXI_MANIFEST_PATH?) and other args when they aren't explicitly set work?

Then pixi shell could set those environment variables in the shell to allow persistence, and other commands would pick them up as default, but would allow overriding with explicit use of --manifest-path to point to another project as well.

I quickly tested this by mucking with shell, info, and list.

info for the local environment

pixi on  main is 📦 v0.17.1 via 🦀 v1.76.0 took 55s
❯
cargo run info
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/pixi info`
      Pixi version: 0.17.1
          Platform: osx-arm64
  Virtual packages: __unix=0=0
                  : __osx=14.4=0
                  : __archspec=1=arm64
         Cache dir: /Users/akerney/Library/Caches/rattler/cache
      Auth storage: /Users/akerney/.rattler/credentials.json

Project
------------
     Manifest file: /Users/akerney/Geek/pixi/pixi.toml
  Config locations:
      Last updated: 22-03-2024 13:46:21

Environments
------------
       Environment: default
          Features: default
          Channels: conda-forge
  Dependency count: 11
      Dependencies: pre-commit, rust, openssl, pkg-config, git, pytest, jsonschema, pydantic, pyyaml, cffconvert, tbump
  Target platforms: linux-64, osx-64, osx-arm64, win-64
             Tasks: bump, test-schema, lint, install, build, test, generate-schema, test-all

       Environment: docs
          Features: docs, default
          Channels: conda-forge
  Dependency count: 17
      Dependencies: mkdocs, mkdocs-material, mdx_truly_sane_lists, pillow, cairosvg, mike, pre-commit, rust, openssl, pkg-config, git, pytest, jsonschema, pydantic, pyyaml, cffconvert, tbump
  Target platforms: osx-arm64, osx-64, linux-64, win-64
             Tasks: deploy-dev, build-docs, build, bump, generate-schema, install, test-schema, test-all, test, deploy-latest, docs, lint

shell to set a persistent environment

pixi on  main [!] is 📦 v0.17.1 via 🦀 v1.76.0
❯
cargo run shell --manifest-path /Users/akerney/GMRI/NERACOOS/NERACOOS_ERDDAP_K8S/pixi.toml
   Compiling pixi v0.17.1 (/Users/akerney/Geek/pixi)
    Finished dev [unoptimized + debuginfo] target(s) in 7.21s
     Running `target/debug/pixi shell --manifest-path /Users/akerney/GMRI/NERACOOS/NERACOOS_ERDDAP_K8S/pixi.toml`
 . "/var/folders/n6/hl07fcc55ys7mbybt01gb8r80000gn/T/pixi_env_M3e.sh"
pixi on  main [!] is 📦 v0.17.1 via 🦀 v1.76.0
❯  . "/var/folders/n6/hl07fcc55ys7mbybt01gb8r80000gn/T/pixi_env_M3e.sh"

info to see the persistent environment

(sea-eagle)
pixi on  main [!] is 📦 v0.17.1 via 🦀 v1.76.0 via 🅒 sea-eagle
❯ pixi info
      Pixi version: 0.17.1
          Platform: osx-arm64
  Virtual packages: __unix=0=0
                  : __osx=14.4=0
                  : __archspec=1=arm64
         Cache dir: /Users/akerney/Library/Caches/rattler/cache
      Auth storage: /Users/akerney/.rattler/credentials.json

Project
------------
           Version: 0.1.0
     Manifest file: /Users/akerney/GMRI/NERACOOS/NERACOOS_ERDDAP_K8S/pixi.toml
  Config locations:
      Last updated: 19-03-2024 11:02:21

Environments
------------
       Environment: default
          Features: default
          Channels: conda-forge
  Dependency count: 27
      Dependencies: python, cftime, dagster-postgres, dagster-k8s, dagster, dask, fsspec, ioos_qc, pandas, pydantic, pystac, python-frontmatter, yaml, requests, sentry-sdk, shapely, xarray, netcdf4, pint, ipykernel, matplotlib, erddapy, uvicorn, xpublish-edr, fastapi, xpublish, xpublish-opendap
  Target platforms: osx-arm64
             Tasks: install-kernel

info with an explicit --manifest-path to override the environment variable

(sea-eagle)
pixi on  main [!] is 📦 v0.17.1 via 🦀 v1.76.0 via 🅒 sea-eagle
❯ pixi info --manifest-path .
      Pixi version: 0.17.1
          Platform: osx-arm64
  Virtual packages: __unix=0=0
                  : __osx=14.4=0
                  : __archspec=1=arm64
         Cache dir: /Users/akerney/Library/Caches/rattler/cache
      Auth storage: /Users/akerney/.rattler/credentials.json

(sea-eagle)
pixi on  main [!] is 📦 v0.17.1 via 🦀 v1.76.0 via 🅒 sea-eagle
❯ pixi info --manifest-path ./pixi.toml
      Pixi version: 0.17.1
          Platform: osx-arm64
  Virtual packages: __unix=0=0
                  : __osx=14.4=0
                  : __archspec=1=arm64
         Cache dir: /Users/akerney/Library/Caches/rattler/cache
      Auth storage: /Users/akerney/.rattler/credentials.json

Project
------------
     Manifest file: /Users/akerney/Geek/pixi/pixi.toml
  Config locations:
      Last updated: 22-03-2024 13:46:21

Environments
------------
       Environment: default
          Features: default
          Channels: conda-forge
  Dependency count: 11
      Dependencies: pre-commit, rust, openssl, pkg-config, git, pytest, jsonschema, pydantic, pyyaml, cffconvert, tbump
  Target platforms: win-64, linux-64, osx-arm64, osx-64
             Tasks: lint, generate-schema, test-schema, test-all, bump, install, test, build

       Environment: docs
          Features: docs, default
          Channels: conda-forge
  Dependency count: 17
      Dependencies: mkdocs, mkdocs-material, mdx_truly_sane_lists, pillow, cairosvg, mike, pre-commit, rust, openssl, pkg-config, git, pytest, jsonschema, pydantic, pyyaml, cffconvert, tbump
  Target platforms: linux-64, osx-64, win-64, osx-arm64
             Tasks: build-docs, deploy-latest, test-schema, test, docs, generate-schema, build, deploy-dev, lint, test-all, bump, install

@ruben-arts
Copy link
Contributor

He @abkfenris, this is indeed the simplest version of it, to get some warning however i would like to add some more logic to the discovery code. PR's are welcome!

@abkfenris
Copy link
Contributor

Cool! I'll give it a shot.

Would tracing::warn!() be the right level of communication if an environment variable is set, and that's the manifest being used? Or is there something from miette?

It also looks like there is already a PIXI_PROJECT_MANIFEST environment variable getting set. Should I use/update that?

@ruben-arts
Copy link
Contributor

Nice! @abkfenris

Yeah give it an initial try with tracing::warn.

You can see what pixi is setting in an environment using pixi shell-hook. We should depend on those variables to avoid two states. If you start with PIXI_PROJECT_MANIFEST we can see get a feel for it by testing your branch.

abkfenris added a commit to abkfenris/pixi that referenced this issue Mar 25, 2024
fixes: prefix-dev#1038

Initial attempt to support persistent shell manifest context.

When running `pixi shell` the `PIXI_PROJECT_MANIFEST` environment variable is set, and other commands that take `--manifest_path` will read from there.

If the environment variable is set, and the path hasn't been overridden, Pixi will emit a warning. `WARN pixi::project: Using manifest `/Users/akerney/GMRI/NERACOOS/NERACOOS_ERDDAP_K8S/pixi.toml` from `PIXI_PROJECT_MANIFEST` environment variable`.
@abkfenris
Copy link
Contributor

Ok, I've made a draft PR #1055. It's working for subsequent commands, at least until the manifest is changed, and then the environment variable is getting reset after the change and the context is lost.

abkfenris added a commit to abkfenris/pixi that referenced this issue Mar 28, 2024
fixes: prefix-dev#1038

Using `Project::discover` to detect if Pixi's manifest path has been set by an environment variable in another `pixi shell` instance. `PIXI_IN_SHELL` is also set to be explicit that this should be an activated environment.

A warning is emitted before a command when the project is loaded if the discovered path is different from the one given by the environment variable, along with a warning after most commands in case their outputs are longer and scroll the initial warning off the terminal.

This is an alternate take on prefix-dev#1055
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants