Skip to content

Commit b04de54

Browse files
IanButterworthKristofferC
authored andcommitted
REPL: fix projname when project_file is missing (#54795)
1 parent 31dac53 commit b04de54

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

stdlib/REPL/src/Pkg_beforeload.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ function relative_project_path(project_file::String, path::String)
6969
end
7070

7171
function projname(project_file::String)
72-
p = Base.TOML.Parser()
73-
Base.TOML.reinit!(p, read(project_file, String); filepath=project_file)
74-
proj = Base.TOML.parse(p)
75-
name = get(proj, "name", nothing)
72+
if isfile(project_file)
73+
name = try
74+
p = Base.TOML.Parser()
75+
Base.TOML.reinit!(p, read(project_file, String); filepath=project_file)
76+
proj = Base.TOML.parse(p)
77+
get(proj, "name", nothing)
78+
catch
79+
nothing
80+
end
81+
else
82+
name = nothing
83+
end
7684
if name === nothing
7785
name = basename(dirname(project_file))
7886
end

stdlib/REPL/test/repl.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,3 +1778,25 @@ end
17781778
@test_broken isempty(undoc)
17791779
@test undoc == [:AbstractREPL, :BasicREPL, :LineEditREPL, :StreamREPL]
17801780
end
1781+
1782+
@testset "Dummy Pkg prompt" begin
1783+
# do this in an empty depot to test default for new users
1784+
withenv("JULIA_DEPOT_PATH" => mktempdir(), "JULIA_LOAD_PATH" => nothing) do
1785+
prompt = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no -e "using REPL; print(REPL.Pkg_promptf())"`)
1786+
@test prompt == "(@v$(VERSION.major).$(VERSION.minor)) pkg> "
1787+
end
1788+
1789+
get_prompt(proj::String) = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no $(proj) -e "using REPL; print(REPL.Pkg_promptf())"`)
1790+
1791+
@test get_prompt("--project=$(pkgdir(REPL))") == "(REPL) pkg> "
1792+
1793+
tdir = mkpath(joinpath(mktempdir(), "foo"))
1794+
@test get_prompt("--project=$tdir") == "(foo) pkg> "
1795+
1796+
proj_file = joinpath(tdir, "Project.toml")
1797+
touch(proj_file) # make a bad Project.toml
1798+
@test get_prompt("--project=$proj_file") == "(foo) pkg> "
1799+
1800+
write(proj_file, "name = \"Bar\"\n")
1801+
@test get_prompt("--project=$proj_file") == "(Bar) pkg> "
1802+
end

0 commit comments

Comments
 (0)