From 8c26e5816bd8e36e30882c19628b86652d49802c Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Wed, 6 Nov 2024 17:04:20 +0100 Subject: [PATCH 1/3] Merge user specified exeflags into env specified flags --- src/server.jl | 11 +++++--- test/examples/exeflags_merging.qmd | 14 ++++++++++ test/testsets/exeflags_merging.jl | 41 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/examples/exeflags_merging.qmd create mode 100644 test/testsets/exeflags_merging.jl diff --git a/src/server.jl b/src/server.jl index f1abab6..deb3a35 100644 --- a/src/server.jl +++ b/src/server.jl @@ -64,7 +64,13 @@ function _extract_timeout(merged_options) end function _exeflags_and_env(options) - exeflags = map(String, options["format"]["metadata"]["julia"]["exeflags"]) + env_exeflags = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_EXEFLAGS", "[]"), Vector{String}) + options_exeflags = map(String, options["format"]["metadata"]["julia"]["exeflags"]) + # We want to be able to override exeflags that are defined via environment variable, + # but leave the remaining flags intact (for example override number of threads but leave sysimage). + # We can do this by adding the options exeflags after the env exeflags. + # Julia will ignore earlier uses of the same flag. + exeflags = [env_exeflags; options_exeflags] env = map(String, options["format"]["metadata"]["julia"]["env"]) # Use `--project=@.` if neither `JULIA_PROJECT=...` nor `--project=...` are specified if !any(startswith("JULIA_PROJECT="), env) && !any(startswith("--project="), exeflags) @@ -537,11 +543,10 @@ end function default_frontmatter() D = Dict{String,Any} - exeflags = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_EXEFLAGS", "[]"), Vector{String}) env = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_ENV", "[]"), Vector{String}) return D( "fig-format" => "png", - "julia" => D("exeflags" => exeflags, "env" => env), + "julia" => D("env" => env), "execute" => D("error" => true), ) end diff --git a/test/examples/exeflags_merging.qmd b/test/examples/exeflags_merging.qmd new file mode 100644 index 0000000..a96cba2 --- /dev/null +++ b/test/examples/exeflags_merging.qmd @@ -0,0 +1,14 @@ +--- +title: Exeflags merging +engine: julia +julia: + exeflags: [] +--- + +```{julia} +print(Base.active_project()) +``` + +```{julia} +print(Threads.nthreads()) +``` \ No newline at end of file diff --git a/test/testsets/exeflags_merging.jl b/test/testsets/exeflags_merging.jl new file mode 100644 index 0000000..8fc30b7 --- /dev/null +++ b/test/testsets/exeflags_merging.jl @@ -0,0 +1,41 @@ +include("../utilities/prelude.jl") + +@testset "exeflags merging" begin + + function with_replacement_file(f, file, replacements...) + s = read(file, String) + mktempdir() do dir + tempfile = joinpath(dir, basename(file)) + open(tempfile, "w") do io + write(io, replace(s, replacements...)) + end + f(tempfile) + end + end + + file = joinpath(@__DIR__, "../examples/exeflags_merging.qmd") + + withenv("QUARTONOTEBOOKRUNNER_EXEFLAGS" => "[\"--project=/set_via_env\", \"--threads=3\"]") do + server = QuartoNotebookRunner.Server() + json = QuartoNotebookRunner.run!(server, file; showprogress = false) + @test contains(json.cells[2].outputs[1].text, "set_via_env") + @test json.cells[4].outputs[1].text == "3" + close!(server) + + with_replacement_file(file, "[]" => "[\"--project=/override_via_frontmatter\"]") do newfile + server = QuartoNotebookRunner.Server() + json = QuartoNotebookRunner.run!(server, newfile; showprogress = false) + @test contains(json.cells[2].outputs[1].text, "override_via_frontmatter") + @test json.cells[4].outputs[1].text == "3" + close!(server) + end + + with_replacement_file(file, "[]" => "[\"--threads=5\"]") do newfile + server = QuartoNotebookRunner.Server() + json = QuartoNotebookRunner.run!(server, newfile; showprogress = false) + @test contains(json.cells[2].outputs[1].text, "set_via_env") + @test json.cells[4].outputs[1].text == "5" + close!(server) + end + end +end From f7602e5b454a271f996d99514a507c1975bc869d Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Wed, 6 Nov 2024 17:05:01 +0100 Subject: [PATCH 2/3] formatting --- src/server.jl | 3 ++- test/testsets/exeflags_merging.jl | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/server.jl b/src/server.jl index deb3a35..42646fe 100644 --- a/src/server.jl +++ b/src/server.jl @@ -64,7 +64,8 @@ function _extract_timeout(merged_options) end function _exeflags_and_env(options) - env_exeflags = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_EXEFLAGS", "[]"), Vector{String}) + env_exeflags = + JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_EXEFLAGS", "[]"), Vector{String}) options_exeflags = map(String, options["format"]["metadata"]["julia"]["exeflags"]) # We want to be able to override exeflags that are defined via environment variable, # but leave the remaining flags intact (for example override number of threads but leave sysimage). diff --git a/test/testsets/exeflags_merging.jl b/test/testsets/exeflags_merging.jl index 8fc30b7..3aa51f7 100644 --- a/test/testsets/exeflags_merging.jl +++ b/test/testsets/exeflags_merging.jl @@ -15,14 +15,19 @@ include("../utilities/prelude.jl") file = joinpath(@__DIR__, "../examples/exeflags_merging.qmd") - withenv("QUARTONOTEBOOKRUNNER_EXEFLAGS" => "[\"--project=/set_via_env\", \"--threads=3\"]") do + withenv( + "QUARTONOTEBOOKRUNNER_EXEFLAGS" => "[\"--project=/set_via_env\", \"--threads=3\"]", + ) do server = QuartoNotebookRunner.Server() json = QuartoNotebookRunner.run!(server, file; showprogress = false) @test contains(json.cells[2].outputs[1].text, "set_via_env") @test json.cells[4].outputs[1].text == "3" close!(server) - with_replacement_file(file, "[]" => "[\"--project=/override_via_frontmatter\"]") do newfile + with_replacement_file( + file, + "[]" => "[\"--project=/override_via_frontmatter\"]", + ) do newfile server = QuartoNotebookRunner.Server() json = QuartoNotebookRunner.run!(server, newfile; showprogress = false) @test contains(json.cells[2].outputs[1].text, "override_via_frontmatter") From dedd1362ab9f75c060c2e1eb209d87e26df4f7e3 Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Wed, 6 Nov 2024 18:38:48 +0100 Subject: [PATCH 3/3] need default exeflags after all --- src/server.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.jl b/src/server.jl index 42646fe..d4cc587 100644 --- a/src/server.jl +++ b/src/server.jl @@ -547,7 +547,7 @@ function default_frontmatter() env = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_ENV", "[]"), Vector{String}) return D( "fig-format" => "png", - "julia" => D("env" => env), + "julia" => D("env" => env, "exeflags" => []), "execute" => D("error" => true), ) end