diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 6b6c37425e70bb..2c75bc5c74e4ee 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -334,6 +334,26 @@ end outstream(r::BasicREPL) = r.terminal hascolor(r::BasicREPL) = hascolor(r.terminal) +function try_pkg_add(mod::Symbol) + resp = try + Base.prompt(" \033[32m\033[1m└\033[22m\033[0m Package $(repr(String(mod))) not found, with active project $(repr(Base.active_project())) + Try adding $(repr(String(mod))) to the active environment? (y/n)", + default = "n") + catch err + if err isa InterruptException + println() + return false + end + end + if lowercase(resp) in ["y", "yes"] + let Pkg = Base.require(Base.PkgId(Base.UUID((0x44cfe95a_1eb2_52ea,0xb672_e2afdf69b78f)), "Pkg")) + Pkg.add(string(mod)) + return true + end + end + return false +end + function run_frontend(repl::BasicREPL, backend::REPLBackendRef) d = REPLDisplay(repl) dopushdisplay = !in(d,Base.Multimedia.displays) @@ -776,7 +796,14 @@ backend(r::AbstractREPL) = r.backendref function eval_with_backend(ast, backend::REPLBackendRef) put!(backend.repl_channel, (ast, 1)) - return take!(backend.response_channel) # (val, iserr) + resp = take!(backend.response_channel) # (val, iserr) + if last(resp) + err = first(first(first(resp))) + if err isa ModuleNotFoundError + try_pkg_add(err.mod) && return Pair{Any, Bool}(nothing, false) + end + end + return resp end function respond(f, repl, main; pass_empty::Bool = false, suppress_on_semicolon::Bool = true)