Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shell cd error when working dir has been deleted #41244

Merged
merged 8 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function repl_cmd(cmd, out)
if isempty(cmd.exec)
throw(ArgumentError("no cmd to execute"))
elseif cmd.exec[1] == "cd"
new_oldpwd = pwd()
if length(cmd.exec) > 2
throw(ArgumentError("cd method only takes one argument"))
elseif length(cmd.exec) == 2
Expand All @@ -52,11 +51,17 @@ function repl_cmd(cmd, out)
end
dir = ENV["OLDPWD"]
end
cd(dir)
else
cd()
dir = homedir()
end
ENV["OLDPWD"] = new_oldpwd
try
ENV["OLDPWD"] = pwd()
catch ex
ex isa IOError || rethrow()
# if current dir has been deleted, then pwd() will throw an IOError: pwd(): no such file or directory (ENOENT)
delete!(ENV, "OLDPWD")
end
cd(dir)
println(out, pwd())
else
@static if !Sys.iswindows()
Expand Down
20 changes: 20 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,26 @@ end
end
end

@testset "pwd tests" begin
mktempdir() do dir
cd(dir) do
withenv("OLDPWD" => nothing) do
io = IOBuffer()
Base.repl_cmd(@cmd("cd"), io)
Base.repl_cmd(@cmd("cd -"), io)
@test realpath(pwd()) == realpath(dir)
if !Sys.iswindows()
# Delete the working directory and check we can cd out of it
# Cannot delete the working directory on Windows
rm(dir)
@test_throws Base._UVError("pwd()", Base.UV_ENOENT) pwd()
Base.repl_cmd(@cmd("cd \\~"), io)
end
end
end
end
end

@testset "readdir tests" begin
≛(a, b) = sort(a) == sort(b)
mktempdir() do dir
Expand Down