Skip to content

Commit

Permalink
Emit warning when called during precompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
tecosaur committed Jul 31, 2024
1 parent 232585f commit 51aed88
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ function ensureexecutable(path::String)
path
end

function warn_if_precompiling()
if ccall(:jl_generating_output, Cint, ()) != 0
@noinline (function ()
st = stacktrace(backtrace())
naughtyline = findfirst(sf -> sf.file != Symbol(@__FILE__), st)
naughtysf = st[something(naughtyline, 1)]
@warn """A base directory is being computed during precompilation.
This is dangerous, as results depend on the live system configuration.
It is recommended that you invoke BaseDirs as required in
function bodies, rather than at the top level. Calls are very
cheap, and you can always pass the result of a live call around.
""" _module="BaseDirs" _file=String(naughtysf.file) _line=naughtysf.line
end)()
end
end

function resolvedirpath(basedir::String, pathcomponents::Union{Tuple, AbstractVector}; create::Bool=false)
create && ensurebasedir(basedir)
if isempty(pathcomponents)
Expand Down Expand Up @@ -112,8 +129,10 @@ macro defaccessor(fnname::Symbol, var::Union{Symbol, Expr})
:resolvedirpath
end
quote
$(esc(fnname))(pathcomponents...; kwargs...) =
function $(esc(fnname))(pathcomponents...; kwargs...)
$warn_if_precompiling()
$resolver($dirvar, pathcomponents; kwargs...)
end
$(esc(fnname))(project::BaseDirs.Project, pathcomponents...; kwargs...) =
$(esc(fnname))(BaseDirs.projectpath(project, $dirvar), pathcomponents...; kwargs...)
end
Expand Down

0 comments on commit 51aed88

Please sign in to comment.