aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-11-11 12:58:24 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-11-11 12:58:24 -0500
commit4c3c5a27eb591fa3fcb4267f27390133b0dd5ba0 (patch)
tree5d33f1010c49f6939b5817b374665f7d62a977cf
parentfab09092031b440c752d54f2e4fea467ed5bff2d (diff)
downloadstasis-4c3c5a27eb591fa3fcb4267f27390133b0dd5ba0.tar.gz
Revert activation procedure, with tweaks
* Convert CONDA_SHLVL to an integer. If this variable is non-zero then deactivate the current environment before activating a new one. * Conda's initialization scripts don't handle shebang lines longer than 127 bytes on Linux. I'm aware they handle it in conda itself, but the "bug" is such that you cannot use conda to reap the benefits of their workaround(s). * This redefines the __conda_exe shell function so that conda is always executed by the base environment's Python interpreter via CONDA_PYTHON_EXE.
-rw-r--r--src/lib/core/conda.c30
1 files changed, 17 insertions, 13 deletions
diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c
index 0e0161d..48ed6c4 100644
--- a/src/lib/core/conda.c
+++ b/src/lib/core/conda.c
@@ -266,24 +266,28 @@ int conda_activate(const char *root, const char *env_name) {
return -1;
}
+ // Fully activate conda and record its effect on the runtime environment
+ char command[PATH_MAX * 3];
+ const char *conda_shlvl_str = getenv("CONDA_SHLVL");
+ unsigned long conda_shlvl = 0;
+ if (conda_shlvl_str) {
+ conda_shlvl = strtol(conda_shlvl_str, NULL, 10);
+ }
+
if (conda_prepend_bin(root)) {
remove(logfile);
return -1;
}
- // Fully activate conda and record its effect on the runtime environment
- char command[PATH_MAX * 3];
- const char *conda_shlvl = getenv("CONDA_SHLVL");
- if (conda_shlvl == NULL || strcmp(conda_shlvl, "0") == 0) {
- // First-run initialization
- snprintf(command, sizeof(command) - 1, "source %s; source %s; conda activate %s &>/dev/null; env -0", path_conda, path_mamba, env_name);
- } else {
- // Conda is already available and configured.
- // Make calls directly to conda using conda's base interpreter.
- // The shell functions generated by sourcing path_conda and path_mamba are extremely inconsistent
- // in this environment. DO NOT USE THEM.
- snprintf(command, sizeof(command) - 1, "$CONDA_PYTHON_EXE $CONDA_EXE activate %s &>/dev/null; env -0", env_name);
- }
+ snprintf(command, sizeof(command) - 1,
+ "set -a\n"
+ "source %s\n"
+ "function __conda_exe (\n\t\"$CONDA_PYTHON_EXE\" \"$CONDA_EXE\" $_CE_M $_CE_CONDA \"$@\"\n)\n"
+ "source %s\n"
+ "%s\n"
+ "conda activate %s 1>&2\n"
+ "env -0\n", path_conda, path_mamba, conda_shlvl ? "conda deactivate" : ":", env_name);
+
int retval = shell(&proc, command);
if (retval) {
// it didn't work; drop out for cleanup