From a550bb805f161b17124d0f4755a21419683222a2 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Thu, 13 Jun 2024 21:00:01 -0400 Subject: [PATCH 1/3] REPL: fix projname when project_file is missing --- stdlib/REPL/src/Pkg_beforeload.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/stdlib/REPL/src/Pkg_beforeload.jl b/stdlib/REPL/src/Pkg_beforeload.jl index 78b76374cf580..3ce2f1c45bfec 100644 --- a/stdlib/REPL/src/Pkg_beforeload.jl +++ b/stdlib/REPL/src/Pkg_beforeload.jl @@ -69,10 +69,14 @@ function relative_project_path(project_file::String, path::String) end function projname(project_file::String) - p = Base.TOML.Parser() - Base.TOML.reinit!(p, read(project_file, String); filepath=project_file) - proj = Base.TOML.parse(p) - name = get(proj, "name", nothing) + if isfile(project_file) + p = Base.TOML.Parser() + Base.TOML.reinit!(p, read(project_file, String); filepath=project_file) + proj = Base.TOML.parse(p) + name = get(proj, "name", nothing) + else + name = nothing + end if name === nothing name = basename(dirname(project_file)) end From f782dde2d481d88d57a2aa68fa1974d216e17f41 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Thu, 13 Jun 2024 21:09:29 -0400 Subject: [PATCH 2/3] also handle parsing errors --- stdlib/REPL/src/Pkg_beforeload.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/stdlib/REPL/src/Pkg_beforeload.jl b/stdlib/REPL/src/Pkg_beforeload.jl index 3ce2f1c45bfec..56c4e2562f7e6 100644 --- a/stdlib/REPL/src/Pkg_beforeload.jl +++ b/stdlib/REPL/src/Pkg_beforeload.jl @@ -70,10 +70,14 @@ end function projname(project_file::String) if isfile(project_file) - p = Base.TOML.Parser() - Base.TOML.reinit!(p, read(project_file, String); filepath=project_file) - proj = Base.TOML.parse(p) - name = get(proj, "name", nothing) + name = try + p = Base.TOML.Parser() + Base.TOML.reinit!(p, read(project_file, String); filepath=project_file) + proj = Base.TOML.parse(p) + get(proj, "name", nothing) + catch + nothing + end else name = nothing end From d8110f1ef2476b0695db488e89654fccf9d0a223 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 14 Jun 2024 08:54:24 -0400 Subject: [PATCH 3/3] add tests --- stdlib/REPL/test/repl.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/stdlib/REPL/test/repl.jl b/stdlib/REPL/test/repl.jl index b3a62dbdb4ee5..4de053695e049 100644 --- a/stdlib/REPL/test/repl.jl +++ b/stdlib/REPL/test/repl.jl @@ -1836,3 +1836,25 @@ end @test_broken isempty(undoc) @test undoc == [:AbstractREPL, :BasicREPL, :LineEditREPL, :StreamREPL] end + +@testset "Dummy Pkg prompt" begin + # do this in an empty depot to test default for new users + withenv("JULIA_DEPOT_PATH" => mktempdir(), "JULIA_LOAD_PATH" => nothing) do + prompt = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no -e "using REPL; print(REPL.Pkg_promptf())"`) + @test prompt == "(@v$(VERSION.major).$(VERSION.minor)) pkg> " + end + + get_prompt(proj::String) = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no $(proj) -e "using REPL; print(REPL.Pkg_promptf())"`) + + @test get_prompt("--project=$(pkgdir(REPL))") == "(REPL) pkg> " + + tdir = mkpath(joinpath(mktempdir(), "foo")) + @test get_prompt("--project=$tdir") == "(foo) pkg> " + + proj_file = joinpath(tdir, "Project.toml") + touch(proj_file) # make a bad Project.toml + @test get_prompt("--project=$proj_file") == "(foo) pkg> " + + write(proj_file, "name = \"Bar\"\n") + @test get_prompt("--project=$proj_file") == "(Bar) pkg> " +end