Understanding package extension dependency constraints

I’m currently working on extensions for a package where my extensions add methods to an existing function in my main package.
I have multiple extensions that add such methods. Some need only one additional package, some more.
While the ones requiring only one additional dependency work fine, the ones with multiple dependencies fail to load properly. Could anyone explain this behavior ?

To illustrate my problem, I created a package TestPkg, defining a function foo as such

module TestPkg

function foo(i::Integer) 
    return i
end

end # module TestPkg

TestPkg has two extensions

  • TestPkgLinearAlgebraExt loading only LinearAlgebra
  • TestPkgBlockArraysExt loading BlockArrays and SparseArrays

each containing a dummy foo implementation.

ext/TestPkgLinearAlgebraExt.jl contents

module TestPkgLinearAlgebraExt

using TestPkg, LinearAlgebra
import TestPkg: foo


function foo(m::AbstractMatrix)
    return 1
end

end

ext/TestPkgBlockArraysExt.jl contents

module TestPkgBlockArraysExt

using TestPkg, BlockArrays, SparseArrays
import TestPkg: foo


function foo(m::AbstractBlockArray)
    # Some operations combining BlockArrays and SparseArrays
    # ...
    # ...
    return 2
end

end

Project.toml contents

name = "TestPkg"
uuid = "c40810ae-ad71-4c5b-b2ee-a1c4692374cc"
version = "0.1.0"

[deps]

[weakdeps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[extensions]
TestPkgBlockArraysExt = ["SparseArrays", "BlockArrays"]
TestPkgLinearAlgebraExt = "LinearAlgebra"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "LinearAlgebra", "BlockArrays"]

I tried to verify what worked and what didn’t by using GitHub - JuliaTesting/TestEnv.jl: Activate your test enviroment, so you can use your test dependencies in the REPL and checked the proper loading of the extensions with Base.get_extension

using TestEnv;TestEnv.activate()
using TestPkg
using LinearAlgebra
Base.get_extension(TestPkg,:TestPkgLinearAlgebraExt) #--> returns module TestPkgLinearAlgebraExt
Base.get_extension(TestPkg,:TestPkgBlockArraysExt) #--> returns nothing so the extension is not loaded

The only way to trigger the proper loading of the extension was to add SparseArrays to the test environment which then triggered the extension.

Given that remark, what would be the correct way to specify and trigger an extension having multiple dependencies, by only loading one package from the users perspective ?