From e9cf7eaf45b54b41fcc1a63576d3e74f1b6daeb8 Mon Sep 17 00:00:00 2001 From: Grant Bruer Date: Thu, 12 Sep 2024 15:30:07 -0400 Subject: [PATCH] Set source file for sandbox module --- src/Literate.jl | 14 ++++++++++---- test/runtests.jl | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Literate.jl b/src/Literate.jl index fd724c4..fa1e3c7 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -582,7 +582,7 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:md) # create the markdown file - sb = sandbox() + sb = sandbox(config["literate_outputfile"]) iomd = IOBuffer() for (chunknum, chunk) in enumerate(chunks) if isa(chunk, MDChunk) @@ -789,7 +789,7 @@ function jupyter_notebook(chunks, config) end function execute_notebook(nb; inputfile::String, fake_source::String, softscope::Bool) - sb = sandbox() + sb = sandbox(fake_source) execution_count = 0 for cell in nb["cells"] cell["cell_type"] == "code" || continue @@ -851,8 +851,14 @@ function sandbox() # eval(expr) is available in the REPL (i.e. Main) so we emulate that for the sandbox Core.eval(m, :(eval(x) = Core.eval($m, x))) # modules created with Module() does not have include defined - # abspath is needed since this will call `include_relative` - Core.eval(m, :(include(x) = Base.include($m, abspath(x)))) + Core.eval(m, :(include(x) = Base.include($m, x))) + return m +end + +function sandbox(source_file) + m = sandbox() + # For `include` to work, the source file must be set. + Core.eval(m, :(current_task().storage[:SOURCE_PATH] = $source_file)) return m end diff --git a/test/runtests.jl b/test/runtests.jl index ee1e221..ebf46ae 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1310,6 +1310,15 @@ end end notebook = read(joinpath(outdir, "inputfile.ipynb"), String) @test occursin("\"data\": {\n \"text/plain\": \"31\"\n }", notebook) + # issue #251 + write(inputfile, "include(joinpath(\"issue251\", \"A.jl\"))") + mkdir(joinpath(outdir, "issue251")) + write(joinpath(outdir, "issue251", "A.jl"), "include(\"B.jl\")") + write(joinpath(outdir, "issue251", "B.jl"), "230 + 21") + Literate.notebook(inputfile, outdir) + notebook = read(joinpath(outdir, "inputfile.ipynb"), String) + @test occursin("\"data\": {\n \"text/plain\": \"251\"\n }", notebook) + # test error when executing notebook write(inputfile, "for i in 1:10\n println(i)") r = @test_logs((:error, r"error when executing notebook based on input file: "), match_mode=:any,