# Understanding package extension dependency constraints

**URL:** <https://discourse.julialang.org/t/understanding-package-extension-dependency-constraints/124542>\
**Category:** General Usage\
**Tags:** question, package-extensions\
**Created:** [January 8, 2025, 4:52pm UTC](https://discourse.julialang.org/t/understanding-package-extension-dependency-constraints/124542 "2025-01-08T16:52:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [January 8, 2025, 4:52pm UTC](https://discourse.julialang.org/t/understanding-package-extension-dependency-constraints/124542/1 "2025-01-08T16:52:22Z")

</div>

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

```julia
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

```julia
module TestPkgLinearAlgebraExt

using TestPkg, LinearAlgebra
import TestPkg: foo

function foo(m::AbstractMatrix)
    return 1
end

end

```

`ext/TestPkgBlockArraysExt.jl` contents

```julia
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

```julia
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](https://github.com/JuliaTesting/TestEnv.jl) and checked the proper loading of the extensions with `Base.get_extension`

```julia
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 ?
