From b51b8094b95f446b5b9bff37adc80e1d041302b7 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Sat, 23 Dec 2023 22:10:19 +0100 Subject: [PATCH] Warn if an already loaded package is attempted to be loaded from a different path (#44329) --- base/loading.jl | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/base/loading.jl b/base/loading.jl index 4e7ca940d15a1..81778d41893c0 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -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) @@ -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) @@ -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}