Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append user exeflags to env exeflags #209

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ 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)
Expand Down Expand Up @@ -537,11 +544,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, "exeflags" => []),
"execute" => D("error" => true),
)
end
Expand Down
14 changes: 14 additions & 0 deletions test/examples/exeflags_merging.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Exeflags merging
engine: julia
julia:
exeflags: []
---

```{julia}
print(Base.active_project())
```

```{julia}
print(Threads.nthreads())
```
46 changes: 46 additions & 0 deletions test/testsets/exeflags_merging.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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
Loading