Skip to content

Warn if an already loaded package is attempted to be loaded from a different path #44329

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

Merged
merged 4 commits into from
Dec 23, 2023
Merged
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
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