Skip to content

Commit

Permalink
Merge pull request #6 from PumasAI/mh/pkg-load-hooks-fix
Browse files Browse the repository at this point in the history
Fix package integration hooks logic
  • Loading branch information
MichaelHatherly authored Jan 12, 2024
2 parents e9a6709 + 3cb8c37 commit ae08179
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function raw_markdown_chunks(path::String)
)
end

frontmatter = _recursive_merge(CommonMark.frontmatter(ast), default_frontmatter())
frontmatter = _recursive_merge(default_frontmatter(), CommonMark.frontmatter(ast))

return raw_chunks, frontmatter
else
Expand Down
26 changes: 18 additions & 8 deletions src/worker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ function worker_init(f::File)
# Rerun the package loading hooks if the frontmatter has changed.
if FRONTMATTER[] != frontmatter
FRONTMATTER[] = frontmatter
for hook in values(PACKAGE_LOADING_HOOKS)
hook()
for (pkgid, hook) in PACKAGE_LOADING_HOOKS
if haskey(Base.loaded_modules, pkgid)
hook()
end
end
else
FRONTMATTER[] = frontmatter
Expand Down Expand Up @@ -291,12 +293,20 @@ function worker_init(f::File)
return (; fig_width, fig_height, fig_format, fig_dpi)
end

const PKG_VERSIONS = Dict{Base.PkgId,VersionNumber}()
function _pkg_version(pkgid::Base.PkgId)
deps = Pkg.dependencies()
if haskey(deps, pkgid.uuid)
return deps[pkgid.uuid].version
# Cache the package versions since once a version of a package is
# loaded we don't really support loading a different version of it,
# so we can just cache the version number.
if haskey(PKG_VERSIONS, pkgid)
return PKG_VERSIONS[pkgid]
else
return nothing
deps = Pkg.dependencies()
if haskey(deps, pkgid.uuid)
return PKG_VERSIONS[pkgid] = deps[pkgid.uuid].version
else
return nothing
end
end
end

Expand Down Expand Up @@ -344,8 +354,8 @@ function worker_init(f::File)
mod = get(Base.loaded_modules, pkgid, nothing)
try
Base.@invokelatest f(pkgid, mod)
catch
@error "hook failed" pkgid mod
catch error
@error "hook failed" pkgid mod error
end
return nothing
end
Expand Down
2 changes: 2 additions & 0 deletions test/examples/integrations/CairoMakie.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
title: CairoMakie integration
fig-width: 4
fig-height: 3
---

```{julia}
Expand Down
31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,35 @@ end
end
end
end

@testset "package integration hooks" begin
env_dir = joinpath(@__DIR__, "examples/integrations/CairoMakie")
content = read(joinpath(@__DIR__, "examples/integrations/CairoMakie.qmd"), String)
mktempdir() do dir
cd(dir) do
server = QuartoNotebookRunner.Server()

cp(env_dir, joinpath(dir, "CairoMakie"))
write("CairoMakie.qmd", content)

json = QuartoNotebookRunner.run!(server, "CairoMakie.qmd")

image_png = json.cells[end].outputs[1].metadata["image/png"]
@test image_png.width == 768
@test image_png.height == 576

content = replace(content, "fig-width: 4" => "fig-width: 8")
content = replace(content, "fig-height: 3" => "fig-height: 6")
write("CairoMakie.qmd", content)

json = QuartoNotebookRunner.run!(server, "CairoMakie.qmd")

image_png = json.cells[end].outputs[1].metadata["image/png"]
@test image_png.width == 1536
@test image_png.height == 1152

close!(server)
end
end
end
end

0 comments on commit ae08179

Please sign in to comment.