Skip to content

Commit f4b81af

Browse files
REPL: fix projname when project_file is missing (#54795)
1 parent d9a0c25 commit f4b81af

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
@@ -1836,3 +1836,25 @@ end
18361836
@test_broken isempty(undoc)
18371837
@test undoc == [:AbstractREPL, :BasicREPL, :LineEditREPL, :StreamREPL]
18381838
end
1839+
1840+
@testset "Dummy Pkg prompt" begin
1841+
# do this in an empty depot to test default for new users
1842+
withenv("JULIA_DEPOT_PATH" => mktempdir(), "JULIA_LOAD_PATH" => nothing) do
1843+
prompt = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no -e "using REPL; print(REPL.Pkg_promptf())"`)
1844+
@test prompt == "(@v$(VERSION.major).$(VERSION.minor)) pkg> "
1845+
end
1846+
1847+
get_prompt(proj::String) = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no $(proj) -e "using REPL; print(REPL.Pkg_promptf())"`)
1848+
1849+
@test get_prompt("--project=$(pkgdir(REPL))") == "(REPL) pkg> "
1850+
1851+
tdir = mkpath(joinpath(mktempdir(), "foo"))
1852+
@test get_prompt("--project=$tdir") == "(foo) pkg> "
1853+
1854+
proj_file = joinpath(tdir, "Project.toml")
1855+
touch(proj_file) # make a bad Project.toml
1856+
@test get_prompt("--project=$proj_file") == "(foo) pkg> "
1857+
1858+
write(proj_file, "name = \"Bar\"\n")
1859+
@test get_prompt("--project=$proj_file") == "(Bar) pkg> "
1860+
end

0 commit comments

Comments
 (0)