Skip to content

Commit a61e8be

Browse files
author
Christopher Doris
committed
Conda.activate more closely reflects actual conda activate
1 parent 9da3823 commit a61e8be

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
99
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
1010
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1111
IteratorInterfaceExtensions = "82899510-4779-5014-852e-03e436cf321d"
12+
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1213
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
1314
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1415
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"

src/conda.jl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Conda
22

3-
import ..External
3+
import ..External, JSON
44

55
const _env = Ref("")
66

@@ -15,22 +15,20 @@ const ENV_STACK = Vector{Dict{String,String}}()
1515
function activate()
1616
push!(ENV_STACK, copy(ENV))
1717
e = env()
18-
# these steps imitate a minimal "conda activate"
19-
# TODO: run "conda shell.posix activate ..." or "conda shell.cmd.exe activate ...", capture the resulting environment, and set that here
20-
oldlvl = parse(Int, get(ENV, "CONDA_SHLVL", "0"))
21-
if oldlvl > 0
22-
ENV["CONDA_PREFIX_$oldlvl"] = ENV["CONDA_PREFIX"]
18+
shell = Sys.iswindows() ? "cmd.exe" : "posix"
19+
pathsep = Sys.iswindows() ? ";" : ":"
20+
# ask conda for a JSON description of how it would activate the environment
21+
info = JSON.parse(read(External.Conda._set_conda_env(`$(External.Conda.conda) shell.$shell+json activate $e`), String))
22+
# make these changes
23+
# TODO: we currently ignore info["scripts"]["activate"]
24+
# run these in a subshell, print the resulting environment vars, and merge into ENV
25+
# (or just run the full `conda shell.* activate *` script)
26+
ENV["PATH"] = join(info["path"]["PATH"], pathsep)
27+
for k in info["vars"]["unset"]
28+
delete!(ENV, k)
2329
end
24-
ENV["CONDA_SHLVL"] = string(oldlvl+1)
2530
ENV["CONDA_PREFIX"] = e
26-
ENV["CONDA_DEFAULT_ENV"] = e
27-
ENV["CONDA_PROMPT_MODIFIER"] = "($e) "
28-
ENV["_CE_M"] = ""
29-
ENV["_CE_CONDA"] = ""
30-
ENV["CONDA_PYTHON_EXE"] = python_exe()
31-
oldpath = get(ENV, "PATH", "")
32-
pathsep = Sys.iswindows() ? ";" : ":"
33-
ENV["PATH"] = oldpath == "" ? External.Conda.bin_dir(e) : (External.Conda.bin_dir(e) * pathsep * oldpath)
31+
merge!(ENV, info["vars"]["export"])
3432
return
3533
end
3634

0 commit comments

Comments
 (0)