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 0477a8b commit 70c0240
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ 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)]
# Once we've raised compat to `VERSION >= v"1.8"`, use this version:
#
# @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
#
# Until then, we can't have a multiline string :'(
@warn "A base directory is being computed during precompilation.\nThis is dangerous, as results depend on the live system configuration.\n\nIt is recommended that you invoke BaseDirs as required in\nfunction bodies, rather than at the top level. Calls are very\ncheap, 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 @@ -108,8 +130,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 70c0240

Please sign in to comment.