Skip to content

Commit 69cb6a8

Browse files
committed
[REPL] improve quality of precompile script
Launching a subprocess is buggy and error-prone, as partially evidenced by the variety of bugs that needed to be fixed.
1 parent 189d94d commit 69cb6a8

File tree

11 files changed

+137
-198
lines changed

11 files changed

+137
-198
lines changed

base/client.jl

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ function load_REPL()
416416
end
417417

418418
global active_repl
419+
global active_repl_backend = nothing
419420

420421
function run_fallback_repl(interactive::Bool)
421422
let input = stdin
@@ -455,6 +456,37 @@ function run_fallback_repl(interactive::Bool)
455456
end
456457
end
457458
end
459+
nothing
460+
end
461+
462+
function run_std_repl(REPL::Module, quiet::Bool, banner::Symbol, history_file::Bool)
463+
term_env = get(ENV, "TERM", @static Sys.iswindows() ? "" : "dumb")
464+
term = REPL.Terminals.TTYTerminal(term_env, stdin, stdout, stderr)
465+
banner == :no || REPL.banner(term, short=banner==:short)
466+
if term.term_type == "dumb"
467+
repl = REPL.BasicREPL(term)
468+
quiet || @warn "Terminal not fully functional"
469+
else
470+
repl = REPL.LineEditREPL(term, get(stdout, :color, false), true)
471+
repl.history_file = history_file
472+
end
473+
# Make sure any displays pushed in .julia/config/startup.jl ends up above the
474+
# REPLDisplay
475+
d = REPL.REPLDisplay(repl)
476+
last_active_repl = @isdefined(active_repl) ? active_repl : nothing
477+
last_active_repl_backend = active_repl_backend
478+
global active_repl = repl
479+
pushdisplay(d)
480+
try
481+
global active_repl = repl
482+
_atreplinit(repl)
483+
REPL.run_repl(repl, backend->(global active_repl_backend = backend))
484+
finally
485+
popdisplay(d)
486+
active_repl = last_active_repl
487+
active_repl_backend = last_active_repl_backend
488+
end
489+
nothing
458490
end
459491

460492
# run the requested sort of evaluation loop on stdio
@@ -469,24 +501,7 @@ function run_main_repl(interactive::Bool, quiet::Bool, banner::Symbol, history_f
469501
end
470502
REPL = REPL_MODULE_REF[]
471503
if !fallback_repl && interactive && REPL !== Base
472-
invokelatest(REPL) do REPL
473-
term_env = get(ENV, "TERM", @static Sys.iswindows() ? "" : "dumb")
474-
term = REPL.Terminals.TTYTerminal(term_env, stdin, stdout, stderr)
475-
banner == :no || REPL.banner(term, short=banner==:short)
476-
if term.term_type == "dumb"
477-
repl = REPL.BasicREPL(term)
478-
quiet || @warn "Terminal not fully functional"
479-
else
480-
repl = REPL.LineEditREPL(term, get(stdout, :color, false), true)
481-
repl.history_file = history_file
482-
end
483-
global active_repl = repl
484-
# Make sure any displays pushed in .julia/config/startup.jl ends up above the
485-
# REPLDisplay
486-
pushdisplay(REPL.REPLDisplay(repl))
487-
_atreplinit(repl)
488-
REPL.run_repl(repl, backend->(global active_repl_backend = backend))
489-
end
504+
invokelatest(run_std_repl, REPL, quiet, banner, history_file)
490505
else
491506
if !fallback_repl && interactive && !quiet
492507
@warn "REPL provider not available: using basic fallback" LOAD_PATH=join(Base.LOAD_PATH, Sys.iswindows() ? ';' : ':')

base/loading.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,13 +1121,8 @@ function cache_file_entry(pkg::PkgId)
11211121
uuid === nothing ? pkg.name : package_slug(uuid)
11221122
end
11231123

1124-
# for use during running the REPL precompilation subprocess script, given we don't
1125-
# want it to pick up caches that already exist for other optimization levels
1126-
const ignore_compiled_cache = PkgId[]
1127-
11281124
function find_all_in_cache_path(pkg::PkgId, DEPOT_PATH::typeof(DEPOT_PATH)=DEPOT_PATH)
11291125
paths = String[]
1130-
pkg in ignore_compiled_cache && return paths
11311126
entrypath, entryfile = cache_file_entry(pkg)
11321127
for path in DEPOT_PATH
11331128
path = joinpath(path, entrypath)

base/task.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ function task_done_hook(t::Task)
834834
end
835835

836836
if err && !handled && Threads.threadid() == 1
837-
if isa(result, InterruptException) && isdefined(Base, :active_repl_backend) &&
837+
if isa(result, InterruptException) && active_repl_backend !== nothing &&
838838
active_repl_backend.backend_task._state === task_state_runnable && isempty(Workqueue) &&
839839
active_repl_backend.in_eval
840840
throwto(active_repl_backend.backend_task, result) # this terminates the task
@@ -849,7 +849,7 @@ function task_done_hook(t::Task)
849849
# the exception to the REPL task since the current task is done.
850850
# issue #19467
851851
if Threads.threadid() == 1 &&
852-
isa(e, InterruptException) && isdefined(Base, :active_repl_backend) &&
852+
isa(e, InterruptException) && active_repl_backend !== nothing &&
853853
active_repl_backend.backend_task._state === task_state_runnable && isempty(Workqueue) &&
854854
active_repl_backend.in_eval
855855
throwto(active_repl_backend.backend_task, e)

stdlib/REPL/src/LineEdit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module LineEdit
44

55
import ..REPL
6-
using REPL: AbstractREPL, Options
6+
using ..REPL: AbstractREPL, Options
77

88
using ..Terminals
99
import ..Terminals: raw!, width, height, clear_line, beep

stdlib/REPL/src/REPL.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ include("options.jl")
127127

128128
include("LineEdit.jl")
129129
using .LineEdit
130-
import ..LineEdit:
130+
import .LineEdit:
131131
CompletionProvider,
132132
HistoryProvider,
133133
add_history,
@@ -752,6 +752,7 @@ struct LatexCompletions <: CompletionProvider end
752752

753753
function active_module() # this method is also called from Base
754754
isdefined(Base, :active_repl) || return Main
755+
Base.active_repl === nothing && return Main
755756
return active_module(Base.active_repl::AbstractREPL)
756757
end
757758
active_module((; mistate)::LineEditREPL) = mistate === nothing ? Main : mistate.active_module
@@ -1793,7 +1794,7 @@ module Numbered
17931794

17941795
using ..REPL
17951796

1796-
__current_ast_transforms() = isdefined(Base, :active_repl_backend) ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
1797+
__current_ast_transforms() = Base.active_repl_backend !== nothing ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
17971798

17981799
function repl_eval_counter(hp)
17991800
return length(hp.history) - hp.start_idx
@@ -1855,13 +1856,13 @@ end
18551856

18561857
function __current_ast_transforms(backend)
18571858
if backend === nothing
1858-
isdefined(Base, :active_repl_backend) ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
1859+
Base.active_repl_backend !== nothing ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
18591860
else
18601861
backend.ast_transforms
18611862
end
18621863
end
18631864

1864-
function numbered_prompt!(repl::LineEditREPL=Base.active_repl, backend=nothing)
1865+
function numbered_prompt!(repl::LineEditREPL=Base.active_repl::LineEditREPL, backend=nothing)
18651866
n = Ref{Int}(0)
18661867
set_prompt(repl, n)
18671868
set_output_prefix(repl, n)

stdlib/REPL/src/REPLCompletions.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ function maybe_spawn_cache_PATH()
295295
@lock PATH_cache_lock begin
296296
PATH_cache_task isa Task && !istaskdone(PATH_cache_task) && return
297297
time() < next_cache_update && return
298-
PATH_cache_task = Threads.@spawn REPLCompletions.cache_PATH()
298+
PATH_cache_task = Threads.@spawn begin
299+
REPLCompletions.cache_PATH()
300+
@lock PATH_cache_lock PATH_cache_task = nothing # release memory when done
301+
end
299302
Base.errormonitor(PATH_cache_task)
300303
end
301304
end

stdlib/REPL/src/TerminalMenus/TerminalMenus.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module TerminalMenus
44

5-
using REPL: REPL
5+
using ..REPL: REPL
66

77
function default_terminal(; in::IO=stdin, out::IO=stdout, err::IO=stderr)
88
return REPL.Terminals.TTYTerminal(

stdlib/REPL/src/docview.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ using Base: with_output_color, mapany, isdeprecated, isexported
1313

1414
using Base.Filesystem: _readdirx
1515

16-
import REPL
17-
1816
using InteractiveUtils: subtypes
1917

2018
using Unicode: normalize

0 commit comments

Comments
 (0)