Skip to content

Commit f5d9b86

Browse files
JeffBezansonvtjnashfredrikekre
authored
fix bug in addenv for environment entries with embedded = (#44212)
Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Fredrik Ekre <[email protected]>
1 parent 15dcd5b commit f5d9b86

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

base/cmd.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ setenv(cmd::Cmd, env::Pair{<:AbstractString}...; dir=cmd.dir) =
262262
setenv(cmd, env; dir=dir)
263263
setenv(cmd::Cmd; dir=cmd.dir) = Cmd(cmd; dir=dir)
264264

265+
# split environment entry string into before and after first `=` (key and value)
266+
function splitenv(e::String)
267+
i = findnext('=', e, 2)
268+
if i === nothing
269+
throw(ArgumentError("malformed environment entry"))
270+
end
271+
e[1:prevind(e, i)], e[nextind(e, i):end]
272+
end
273+
265274
"""
266275
addenv(command::Cmd, env...; inherit::Bool = true)
267276
@@ -282,7 +291,7 @@ function addenv(cmd::Cmd, env::Dict; inherit::Bool = true)
282291
merge!(new_env, ENV)
283292
end
284293
else
285-
for (k, v) in eachsplit.(cmd.env, "=")
294+
for (k, v) in splitenv.(cmd.env)
286295
new_env[string(k)::String] = string(v)::String
287296
end
288297
end
@@ -301,7 +310,7 @@ function addenv(cmd::Cmd, pairs::Pair{<:AbstractString}...; inherit::Bool = true
301310
end
302311

303312
function addenv(cmd::Cmd, env::Vector{<:AbstractString}; inherit::Bool = true)
304-
return addenv(cmd, Dict(k => v for (k, v) in eachsplit.(env, "=")); inherit)
313+
return addenv(cmd, Dict(k => v for (k, v) in splitenv.(env)); inherit)
305314
end
306315

307316
"""

test/spawn.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,12 @@ end
826826
dir = joinpath(pwd(), "dir")
827827
cmd = addenv(setenv(`julia`; dir=dir), Dict())
828828
@test cmd.dir == dir
829+
830+
@test addenv(``, ["a=b=c"], inherit=false).env == ["a=b=c"]
831+
cmd = addenv(``, "a"=>"b=c", inherit=false)
832+
@test cmd.env == ["a=b=c"]
833+
cmd = addenv(cmd, "b"=>"b")
834+
@test issetequal(cmd.env, ["b=b", "a=b=c"])
829835
end
830836

831837
@testset "setenv with dir (with tests for #42131)" begin

0 commit comments

Comments
 (0)