conda-forge / miniforge

A conda-forge distribution.
https://conda-forge.org/miniforge
Other
5.76k stars 306 forks source link

Odd path behaviour when there's a path reset in ~/.bashrc #536

Open jamesowers-cohere opened 7 months ago

jamesowers-cohere commented 7 months ago

Solution to issue cannot be found in the documentation.

Issue

I am on Debian Linux. When inserted the following code into my ~/.bashrc at the top, whilst keeping the conda init section below, conda fails to make it onto my path:

# Check if ORIGINAL_PATH is not set (first run) - this allows us to source this file multiple times
if [ -z "$ORIGINAL_PATH" ]; then
    # Store the initial PATH
    echo "Setting ORIGINAL_PATH to $PATH"
    echo "This should only ever be done once upon shell startup"
    # At time of writing, this was: /usr/local/bin:/usr/bin:/bin:/usr/games
    export ORIGINAL_PATH=$PATH
fi

# Restore the initial PATH
export PATH=$ORIGINAL_PATH

If I remove this code, condabin is correctly added my PATH. If this code is inserted, conda does not update my PATH (although it duplicates some existing dirs in the path). What's going on here?

Note that:

To reproduce this issue

Issue also present on MacOS, so you can reproduce there, making edits to your ~/.zshrc instead.

Installed packages

# packages in environment at $HOME/miniforge3:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
archspec                  0.2.2              pyhd8ed1ab_0    conda-forge
boltons                   23.0.0             pyhd8ed1ab_0    conda-forge
brotli-python             1.1.0           py310hc6cd4ac_0    conda-forge
bzip2                     1.0.8                h7f98852_4    conda-forge
c-ares                    1.19.1               hd590300_0    conda-forge
ca-certificates           2023.11.17           hbcca054_0    conda-forge
certifi                   2023.11.17         pyhd8ed1ab_0    conda-forge
cffi                      1.15.1          py310h255011f_3    conda-forge
charset-normalizer        3.2.0              pyhd8ed1ab_0    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
conda                     23.10.0         py310hff52083_1    conda-forge
conda-libmamba-solver     23.11.1            pyhd8ed1ab_0    conda-forge
conda-package-handling    2.2.0              pyh38be061_0    conda-forge
conda-package-streaming   0.9.0              pyhd8ed1ab_0    conda-forge
cryptography              41.0.3          py310h75e40e8_0    conda-forge
fmt                       10.1.1               h00ab1b0_1    conda-forge
icu                       73.2                 h59595ed_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
jsonpatch                 1.32               pyhd8ed1ab_0    conda-forge
jsonpointer               2.0                        py_0    conda-forge
keyutils                  1.6.1                h166bdaf_0    conda-forge
krb5                      1.21.2               h659d440_0    conda-forge
ld_impl_linux-64          2.40                 h41732ed_0    conda-forge
libarchive                3.7.2                h039dbb9_0    conda-forge
libcurl                   8.4.0                hca28451_0    conda-forge
libedit                   3.1.20191231         he28a2e2_2    conda-forge
libev                     4.33                 h516909a_1    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libgcc-ng                 13.1.0               he5830b7_0    conda-forge
libgomp                   13.1.0               he5830b7_0    conda-forge
libiconv                  1.17                 h166bdaf_0    conda-forge
libmamba                  1.5.3                had39da4_2    conda-forge
libmambapy                1.5.3           py310h39ff949_2    conda-forge
libnghttp2                1.52.0               h61bc06f_0    conda-forge
libnsl                    2.0.0                h7f98852_0    conda-forge
libsolv                   0.7.24               hfc55251_3    conda-forge
libsqlite                 3.43.0               h2797004_0    conda-forge
libssh2                   1.11.0               h0841786_0    conda-forge
libstdcxx-ng              13.1.0               hfd8a6a1_0    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libxml2                   2.11.5               h232c23b_1    conda-forge
libzlib                   1.2.13               hd590300_5    conda-forge
lz4-c                     1.9.4                hcb278e6_0    conda-forge
lzo                       2.10              h516909a_1000    conda-forge
mamba                     1.5.3           py310h51d5547_2    conda-forge
ncurses                   6.4                  hcb278e6_0    conda-forge
openssl                   3.2.0                hd590300_1    conda-forge
packaging                 23.1               pyhd8ed1ab_0    conda-forge
pip                       23.2.1             pyhd8ed1ab_0    conda-forge
pluggy                    1.3.0              pyhd8ed1ab_0    conda-forge
pybind11-abi              4                    hd8ed1ab_3    conda-forge
pycosat                   0.6.4           py310h5764c6d_1    conda-forge
pycparser                 2.21               pyhd8ed1ab_0    conda-forge
pyopenssl                 23.2.0             pyhd8ed1ab_1    conda-forge
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.10.12         hd12c33a_0_cpython    conda-forge
python_abi                3.10                    3_cp310    conda-forge
readline                  8.2                  h8228510_1    conda-forge
reproc                    14.2.4               h0b41bf4_0    conda-forge
reproc-cpp                14.2.4               hcb278e6_0    conda-forge
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
ruamel.yaml               0.17.32         py310h2372a71_0    conda-forge
ruamel.yaml.clib          0.2.7           py310h1fa729e_1    conda-forge
setuptools                68.1.2             pyhd8ed1ab_0    conda-forge
tk                        8.6.12               h27826a3_0    conda-forge
toolz                     0.12.0             pyhd8ed1ab_0    conda-forge
tqdm                      4.66.1             pyhd8ed1ab_0    conda-forge
truststore                0.8.0              pyhd8ed1ab_0    conda-forge
tzdata                    2023c                h71feb2d_0    conda-forge
urllib3                   2.0.4              pyhd8ed1ab_0    conda-forge
wheel                     0.41.2             pyhd8ed1ab_0    conda-forge
xz                        5.2.6                h166bdaf_0    conda-forge
yaml-cpp                  0.8.0                h59595ed_0    conda-forge
zstandard                 0.19.0          py310h5764c6d_0    conda-forge
zstd                      1.5.5                hfc55251_0    conda-forge

Environment info

active environment : None
            shell level : 0
       user config file : $HOME/.condarc
 populated config files : $HOME/miniforge3/.condarc
                          $HOME/.condarc
          conda version : 23.10.0
    conda-build version : not installed
         python version : 3.10.12.final.0
       virtual packages : __archspec=1=cascadelake
                          __glibc=2.31=0
                          __linux=5.10.0=0
                          __unix=0=0
       base environment : $HOME/miniforge3  (writable)
      conda av data dir : $HOME/miniforge3/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/conda-forge/linux-64
                          https://conda.anaconda.org/conda-forge/noarch
          package cache : $HOME/miniforge3/pkgs
                          $HOME/.conda/pkgs
       envs directories : $HOME/miniforge3/envs
                          $HOME/.conda/envs
               platform : linux-64
             user-agent : conda/23.10.0 requests/2.31.0 CPython/3.10.12 Linux/5.10.0-26-cloud-amd64 debian/11 glibc/2.31 solver/libmamba conda-libmamba-solver/23.11.1 libmambapy/1.5.3
                UID:GID : 398893130:398893130
             netrc file : $HOME/.netrc
           offline mode : False
hmaarrfk commented 7 months ago

you are effectively running the conda init section twice. just put the conda init portion in an else guard, or restore your path afterward.

jamesowers-cohere commented 6 months ago

With apologies, I don't understand how I'm running the conda init section twice. I haven't additionally added the conda initialize section - it only appears once in my ~/.bashrc (as expected). I mention it in the description such that you understand where I have added the code:

# Check if ORIGINAL_PATH is not set (first run) - this allows us to source this file multiple times
if [ -z "$ORIGINAL_PATH" ]; then
    # Store the initial PATH
    echo "Setting ORIGINAL_PATH to $PATH"
    echo "This should only ever be done once upon shell startup"
    # At time of writing, this was: /usr/local/bin:/usr/bin:/bin:/usr/games
    export ORIGINAL_PATH=$PATH
fi

# Restore the initial PATH
export PATH=$ORIGINAL_PATH

i.e. above it.

How does my added code run conda init? all i'm doing is exporting the path...

hmaarrfk commented 6 months ago
  1. Assume ORIGINAL_PATH is set have something about conda within it.
  2. You do export PATH=$ORIGINAL_PATH. that in effect is running part of the conda initialization
  3. You are running the conda initialization again.

So basically you are running part of the conda-initialization twice.

This isn't entirely the standard usecase.

What are you trying to preserve in your path?

jamesowers-cohere commented 6 months ago

original_path has nothing to do with conda in (it's as described in the comment) - i should highlight that it is only created once upon machine startup, since the if statement condition is false if original_path already exists. Also the issue I'm describing is that conda fails to get into my path if i do this.

jamesowers-cohere commented 6 months ago

To make this clearer, when i insert the path reset at to top of my bashrc, it has the affect of removing conda from my path (despite the fact that the conda init block is still in my bashrc after this path reset).

I'm trying to highlight that a user can do something valid in their bashrc and it can affect the conda installation.

jamesowers-cohere commented 6 months ago

I have updated the issue description to clarify all this and instructions about how to reproduce.

hmaarrfk commented 6 months ago

why do you think that your code should work in the first place? You are modifying the path in quite an intrusive way.

To debug try adding

env > env_before_conda_init.txt

# The conda init code

env > env_after_conda_init.txt

and inspect the differences if you want to force conda to rerun.

If i do this on my computer, I see:

  1. CONDA_EXE
  2. _CE_M
  3. _CE_CONDA
  4. CONDA_SHLVL
  5. CONDA_PYTHON_EXE
  6. PATH

all 6 are modified.

.bashrc is only meant to be run once, during interactive sessions.

I'm not sure what your original usecase is, but I've never run accross the need to "reset" my original path the way you are doing now.

If you want to get really fancy, you can update the commands above during debugging to

env > env_before_$(date +%Y%m%d_%H%M%S_%N).txt

# ...

env > env_after_$(date +%Y%m%d_%H%M%S_%N).txt
hmaarrfk commented 6 months ago

see https://www.baeldung.com/linux/bashrc-vs-bash-profile-vs-profile

jamesowers-cohere commented 6 months ago

Your debug will not work - it will be run after the first call of bashrc when I open an interactive terminal.

The reason i think it should work is documented in the description of the issue. Namely there's no issue for the other utils that alter my path!

Please may you try my instructions to reproduce the issue which I've provided in the description?