Skip to content

Commit

Permalink
Warn if an already loaded package is attempted to be loaded from a di…
Browse files Browse the repository at this point in the history
…fferent path (JuliaLang#44329)
  • Loading branch information
KristofferC committed Dec 23, 2023
1 parent 52ff558 commit b51b809
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,7 @@ function _tryrequire_from_serialized(modkey::PkgId, build_id::UInt128)
assert_havelock(require_lock)
loaded = nothing
if root_module_exists(modkey)
warn_if_already_loaded_different(modkey)
loaded = root_module(modkey)
else
loaded = start_loading(modkey)
Expand Down Expand Up @@ -1532,6 +1533,7 @@ function _tryrequire_from_serialized(modkey::PkgId, path::String, ocachepath::Un
assert_havelock(require_lock)
loaded = nothing
if root_module_exists(modkey)
warn_if_already_loaded_different(modkey)
loaded = root_module(modkey)
else
loaded = start_loading(modkey)
Expand Down Expand Up @@ -1975,11 +1977,50 @@ function __require_prelocked(uuidkey::PkgId, env=nothing)
# After successfully loading, notify downstream consumers
run_package_callbacks(uuidkey)
else
warn_if_already_loaded_different(uuidkey)
newm = root_module(uuidkey)
end
return newm
end

const already_warned_path_change_pkgs = Set{UUID}()
# warns if the loaded version of a module is different to the one that locate_package wants to load
function warn_if_already_loaded_different(uuidkey::PkgId)
uuidkey.uuid already_warned_path_change_pkgs && return
pkgorig = get(pkgorigins, uuidkey, nothing)
if pkgorig !== nothing && pkgorig.path !== nothing
new_path = locate_package(uuidkey)
if !samefile(fixup_stdlib_path(pkgorig.path), new_path)
if isnothing(pkgorig.version)
v = get_pkgversion_from_path(dirname(dirname(pkgorig.path)))
cur_vstr = isnothing(v) ? "" : "v$v "
else
cur_vstr = "v$v "
end
new_v = get_pkgversion_from_path(dirname(dirname(new_path)))
new_vstr = isnothing(new_v) ? "" : "v$new_v "
warnstr = """
$uuidkey is already loaded from a different path:
loaded: $cur_vstr$(repr(pkgorig.path))
requested: $new_vstr$(repr(new_path))
"""
if isempty(already_warned_path_change_pkgs)
warnstr *= """
This might indicate a change of environment has happened between package loads and can mean that
incompatible packages have been loaded"""
if JLOptions().startupfile < 2 #(0 = auto, 1 = yes)
warnstr *= """. If this happened due to a startup.jl consider starting julia
directly in the project via the `--project` arg."""
else
warnstr *= "."
end
end
@warn warnstr
push!(already_warned_path_change_pkgs, uuidkey.uuid)
end
end
end

mutable struct PkgOrigin
path::Union{String,Nothing}
cachepath::Union{String,Nothing}
Expand Down

0 comments on commit b51b809

Please sign in to comment.