From 5332bc5491d8a7cfd5fe2a0ae30218d8dd50204e Mon Sep 17 00:00:00 2001 From: Michael Hatherly Date: Wed, 16 Oct 2024 18:28:45 +0100 Subject: [PATCH] Fix Julia 1.11 errors (#191) * Fix Julia 1.11 errors Fixes #190. * Fixup pkg mode * Adjust which versions are run in the matrix * Rewrite comment and move to be more clear --- .github/workflows/CI.yml | 8 ++- src/QuartoNotebookWorker/src/NotebookState.jl | 1 - src/QuartoNotebookWorker/src/render.jl | 52 +++++++++---------- src/worker.jl | 1 + 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 95986f3..d46f391 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -44,7 +44,7 @@ jobs: matrix: version: - "1.6" - - "1.10" + - "1.11" os: - ubuntu-latest - macos-13 @@ -58,11 +58,17 @@ jobs: os: macos-13 - version: "1.6" os: windows-latest + # Performance regressions on Julia 1.11 on Windows. Currently not + # viable to run the test suite there. + - version: "1.11" + os: windows-latest include: - version: "1.7" os: macos-13 - version: "1.7" os: windows-latest + - version: "1.10" + os: windows-latest steps: - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 with: diff --git a/src/QuartoNotebookWorker/src/NotebookState.jl b/src/QuartoNotebookWorker/src/NotebookState.jl index 421711e..065b6f8 100644 --- a/src/QuartoNotebookWorker/src/NotebookState.jl +++ b/src/QuartoNotebookWorker/src/NotebookState.jl @@ -12,7 +12,6 @@ function __init__() if ccall(:jl_generating_output, Cint, ()) == 0 PROJECT[] = Base.active_project() OPTIONS[] = task_local_storage(:QUARTO_NOTEBOOK_WORKER_OPTIONS, Dict{String,Any}()) - define_notebook_module!() end end diff --git a/src/QuartoNotebookWorker/src/render.jl b/src/QuartoNotebookWorker/src/render.jl index 5f1a1c7..0081614 100644 --- a/src/QuartoNotebookWorker/src/render.jl +++ b/src/QuartoNotebookWorker/src/render.jl @@ -119,12 +119,24 @@ function _render_thunk( end end -# Setting the `helpmode` module isn't an option on Julia 1.6 so we need to -# manually replace `Main` with the module we want. function _helpmode(code::AbstractString, mod::Module) - ex = REPL.helpmode(code) - return postwalk(ex) do x - return x == Main ? mod : x + @static if VERSION < v"1.11.0" + # Earlier versions of Julia don't have the `helpmode` method that takes + # `io`, `code`, `mod` and so we have to manually alter the returned + # expression instead. + ex = REPL.helpmode(code) + ex = postwalk(ex) do x + return x == Main ? mod : x + end + # helpmode embeds object references to `stdout` into the expression, but + # since we are capturing the output it refers to a different stream. We + # need to replace the first `stdout` reference with `:stdout` and remove + # the argument from the other call so that it uses the redirected one. + ex.args[2] = :stdout + deleteat!(ex.args[end].args, 3) + return ex + else + return :(Core.eval($(mod), $(REPL).helpmode(stdout, $(code), $(mod)))) end end @@ -138,15 +150,6 @@ function _process_code( if startswith(code, help_regex) code = String(chomp(replace(code, help_regex => ""; count = 1))) ex = _helpmode(code, mod) - - # helpmode embeds object references to `stdout` into the - # expression, but since we are capturing the output it refers to - # a different stream. We need to replace the first `stdout` - # reference with `:stdout` and remove the argument from the - # other call so that it uses the redirected one. - ex.args[2] = :stdout - deleteat!(ex.args[end].args, 3) - return Expr(:toplevel, ex) end @@ -166,19 +169,16 @@ function _process_code( pkg_regex = r"^\s*\]" if startswith(code, pkg_regex) code = String(chomp(replace(code, pkg_regex => ""; count = 1))) - return Expr( - :toplevel, - :( - let printed = $(Pkg).REPLMode.PRINTED_REPL_WARNING[] - $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = true - try - $(Pkg).REPLMode.do_cmd($(Pkg).REPLMode.MiniREPL(), $code) - finally - $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = printed - end + return Expr(:toplevel, :( + let printed = $(Pkg).REPLMode.PRINTED_REPL_WARNING[] + $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = true + try + $(Pkg).REPLMode.@pkg_str $code + finally + $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = printed end - ), - ) + end + )) end return _parseall(code; filename, lineno) diff --git a/src/worker.jl b/src/worker.jl index 68dc44d..463fb84 100644 --- a/src/worker.jl +++ b/src/worker.jl @@ -25,6 +25,7 @@ function worker_init(f::File, options::Dict) ), ) end + QNW.NotebookState.define_notebook_module!(Main) global refresh!(args...) = QNW.refresh!($(f.path), $(options), args...) global render(args...; kwargs...) = QNW.render(args...; kwargs...) end