Skip to content

Commit f53de73

Browse files
authored
Fix (add|set)env to keep currently set dir for the command, fixes #42131. (#43276)
1 parent e2940cb commit f53de73

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

base/cmd.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ byteenv(env::Union{AbstractVector{Pair{T,V}}, Tuple{Vararg{Pair{T,V}}}}) where {
233233
String[cstr(k*"="*string(v)) for (k,v) in env]
234234

235235
"""
236-
setenv(command::Cmd, env; dir="")
236+
setenv(command::Cmd, env; dir)
237237
238238
Set environment variables to use when running the given `command`. `env` is either a
239239
dictionary mapping strings to strings, an array of strings of the form `"var=val"`, or
@@ -242,13 +242,15 @@ existing environment, create `env` through `copy(ENV)` and then setting `env["va
242242
as desired, or use [`addenv`](@ref).
243243
244244
The `dir` keyword argument can be used to specify a working directory for the command.
245+
`dir` defaults to the currently set `dir` for `command` (which is the current working
246+
directory if not specified already).
245247
246248
See also [`Cmd`](@ref), [`addenv`](@ref), [`ENV`](@ref), [`pwd`](@ref).
247249
"""
248-
setenv(cmd::Cmd, env; dir="") = Cmd(cmd; env=byteenv(env), dir=dir)
249-
setenv(cmd::Cmd, env::Pair{<:AbstractString}...; dir="") =
250+
setenv(cmd::Cmd, env; dir=cmd.dir) = Cmd(cmd; env=byteenv(env), dir=dir)
251+
setenv(cmd::Cmd, env::Pair{<:AbstractString}...; dir=cmd.dir) =
250252
setenv(cmd, env; dir=dir)
251-
setenv(cmd::Cmd; dir="") = Cmd(cmd; dir=dir)
253+
setenv(cmd::Cmd; dir=cmd.dir) = Cmd(cmd; dir=dir)
252254

253255
"""
254256
addenv(command::Cmd, env...; inherit::Bool = true)

test/spawn.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,29 @@ end
821821
cmd = Cmd(`$shcmd -c "echo \$FOO \$BAR"`, env=Dict("FOO" => "foo", "BAR" => "bar"))
822822
cmd2 = addenv(cmd, "FOO" => nothing)
823823
@test strip(String(read(cmd2))) == "bar"
824+
# addenv keeps the cmd's dir (#42131)
825+
dir = joinpath(pwd(), "dir")
826+
cmd = addenv(setenv(`julia`; dir=dir), Dict())
827+
@test cmd.dir == dir
828+
end
829+
830+
@testset "setenv with dir (with tests for #42131)" begin
831+
dir1 = joinpath(pwd(), "dir1")
832+
dir2 = joinpath(pwd(), "dir2")
833+
cmd = Cmd(`julia`; dir=dir1)
834+
@test cmd.dir == dir1
835+
@test Cmd(cmd).dir == dir1
836+
@test Cmd(cmd; dir=dir2).dir == dir2
837+
@test Cmd(cmd; dir="").dir == ""
838+
@test setenv(cmd).dir == dir1
839+
@test setenv(cmd; dir=dir2).dir == dir2
840+
@test setenv(cmd; dir="").dir == ""
841+
@test setenv(cmd, "FOO"=>"foo").dir == dir1
842+
@test setenv(cmd, "FOO"=>"foo"; dir=dir2).dir == dir2
843+
@test setenv(cmd, "FOO"=>"foo"; dir="").dir == ""
844+
@test setenv(cmd, Dict("FOO"=>"foo")).dir == dir1
845+
@test setenv(cmd, Dict("FOO"=>"foo"); dir=dir2).dir == dir2
846+
@test setenv(cmd, Dict("FOO"=>"foo"); dir="").dir == ""
824847
end
825848

826849

0 commit comments

Comments
 (0)