Skip to content
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

When receiving a package callback, start watching the current environment. #761

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/pkgs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ This function gets called via a callback registered with `Base.require`, at the
of module-loading by `using` or `import`.
"""
function watch_package(id::PkgId)
# we may have switched environments, so make sure we're watching the right manifest
active_project_watcher()

pkgdata = get(pkgdatas, id, nothing)
pkgdata !== nothing && return pkgdata
lock(wplock)
Expand Down
60 changes: 60 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2956,6 +2956,66 @@ do_test("Switching free/dev") && @testset "Switching free/dev" begin
push!(to_remove, depot)
end

do_test("Switching environments") && @testset "Switching environments" begin
old_project = Base.active_project()

function generate_package(path, val)
cd(path) do
pkgpath = normpath(joinpath(path, "TestPackage"))
srcpath = joinpath(pkgpath, "src")
if !isdir(srcpath)
Pkg.generate("TestPackage")
end
filepath = joinpath(srcpath, "TestPackage.jl")
write(filepath, """
module TestPackage
f() = $val
end
""")
return pkgpath
end
end

try
Pkg.activate(; temp=true)

# generate a package
root = mktempdir()
pkg = generate_package(root, 1)
LibGit2.with(LibGit2.init(pkg)) do repo
LibGit2.add!(repo, "Project.toml")
LibGit2.add!(repo, "src/TestPackage.jl")
test_sig = LibGit2.Signature("TEST", "[email protected]", round(time(); digits=0), 0)
LibGit2.commit(repo, "version 1"; author=test_sig, committer=test_sig)
end

# install the package
Pkg.add(url=pkg)
sleep(mtimedelay)

@eval using TestPackage
sleep(mtimedelay)
@test Base.invokelatest(TestPackage.f) == 1

# update the package
generate_package(root, 2)
LibGit2.with(LibGit2.GitRepo(pkg)) do repo
LibGit2.add!(repo, "src/TestPackage.jl")
test_sig = LibGit2.Signature("TEST", "[email protected]", round(time(); digits=0), 0)
LibGit2.commit(repo, "version 2"; author=test_sig, committer=test_sig)
end

# install the update
Pkg.add(url=pkg)
sleep(mtimedelay)

revise()
@test Base.invokelatest(TestPackage.f) == 2
finally
Pkg.activate(old_project)
end
end

# in v1.8 and higher, a package can't be loaded at all when its precompilation failed
@static if Base.VERSION < v"1.8.0-DEV.1451"
do_test("Broken dependencies (issue #371)") && @testset "Broken dependencies (issue #371)" begin
Expand Down
Loading