While package extensions make it easy to implement “glue code” between independent packages and can be used for various cases as a workaround, they are not optimal for large packages with optional features. For instance, AbstractOperators.jl is a package that wraps common DSP and image processing operations into matrix-like operators. During development, we faced a scalability issue with the following options:
- Add all dependencies (DSP.jl, FFTW.jl, Wavelets.jl, NFFT.jl) to a single package: This would lead to really long package compilation and loading times.
- Migrate implementations for functions that need extra dependencies into package extensions: Optimal precompilation and loading time, but IMO quite a hack for the use case, as all public structs and functions that need the dependency must be declared in the parent package (because extensions cannot export or expose any new symbols) and be implemented in the package extension. Also, it goes against the original intention of th e package: it aims to provide matrix-like transforms, and the user of the package should be unaware if it wraps e.g. Wavelets.jl, NDWaveletTransforms.jl or a custom implementation.
- Split the package into many subpackages: Cleanest solution, optimal precompilation and loading, but hard to maintain and probably also an unnecessary burden to Julia Registry maintainers as well (or at least useless noise).
SciML maintainers also seem to suffer from this issue, and they chose option 3, if I understand correctly. @ChrisRackauckas opened an issue two years ago on the Julia language repo to start a discussion on the design of subpackages, and @Mason has opened an experimental PR based on the issue. Unfortunately, the PR was not finished and was later closed, and the discussion on the issue went quiet.
Based on the previous discussions and Mason’s work, below you can find my proposal for the syntax and behavior. I also executed a feasibility study with an LLM, and the addition of the feature appears to affect three repos with a moderate amount of changes:
- Julia itself (~1500 lines of addition): loading: Prototype subpackages (explicit loading, autoload, [subdeps], `using A::B`) - Pull Request #1 - hakkelt/julia - GitHub
- Pkg.jl (~1000 lines of addition): Prototype subpackages and `[subdeps]` - Pull Request #1 - hakkelt/Pkg.jl - GitHub
- RegistryTools.jl (~500 lines of addition): Record subpackages and subdeps in the registry - Pull Request #1 - hakkelt/RegistryTools.jl - GitHub
This AI-generated solution seems to work reliably, but I haven’t reviewed the code yet (and hence the PRs are only on my forks) – so it should really be considered only as a measure of the “blast radius”.
The Principles
The name subpackage seems to have been agreed upon, so I’m using it throughout the proposal.
Requirements for a solution:
- Subpackages have no version or
Project.tomlof their own; they share the parent’s UUID, version, compat, and registration. - Subpackages can export new symbols that can be brought into scope by explicitly importing the subpackage.
- Subpackages can have extra dependencies that get loaded together with the subpackage. These extra dependencies must be weak dependencies of the parent.
- Subpackages can depend on each other. This also means that if subpackage-A depends on subpackage-B, then all extra dependencies of subpackage-B become the transitive dependencies of subpackage-A.
- Each subpackage is precompiled separately (and in parallel).
- A project or package that uses a subpackage must list it explicitly; loading a subpackage that is not listed is an error.
- Only the subpackages that are listed somewhere in the environment, and their dependencies, are installed and precompiled. The code of the other subpackages comes with the parent’s source, but their dependencies are not downloaded, and they are not precompiled.
- Loading a subpackage loads the parent only if the subpackage’s code does
using Parent(which will most likely happen). - A subpackage cannot be activated as an environment. The parent package’s environment implicitly includes all subpackages.
- A subpackage might load automatically when the parent and the subpackage’s trigger packages load, similarly to how extensions are loaded now (optional requirement).
Out of scope for subpackages:
- Monorepos with multiple, independently versioned packages.
- (Sub)packages that have artifacts – probably better to register as a normal package.
Proposed API
Declaring subpackages (the parent’s Project.toml)
Subpackages would live in the sub folder and follow the same convention as packages and extensions: the subpackage SubpackageCommons would be loaded from sub/SubpackageCommons.jl or sub/SubpackageCommons/SubpackageCommons.jl.
name = "Parent"
uuid = "…"
version = "1.2.3"
[deps] # always installed; every subpackage may use these
Light = "…"
[weakdeps] # installed only when a selected subpackage needs them
Heavy = "…"
Helper = "…"
Plots = "…"
OtherDep = "…"
[subpackages]
SubpackageCommons = [] # no extra dependencies, only implicit dependency on Parent and its [deps]
Subpackage1 = ["::SubpackageCommons"] # depends on a sibling subpackage
Subpackage2 = ["::SubpackageCommons", "Heavy"] # ...and on a weakdep of the parent
Subpackage3 = { deps = ["Plots"], autoload = true } # loads automatically when Parent and Plots are loaded, like an extension
Subpackage4 = { deps = ["::SubpackageCommons", "Heavy", "Helper"], autoload = ["Heavy"] } # even more generalized extension
[extensions]
PlotsExt = ["::Subpackage1", "Plots"] # triggered by Parent's own subpackage
OtherDepExt = "OtherDep::SubA" # triggered by a subpackage of a dependency
The autoload feature would generalize the package extensions. Subpackage3 behaves like an extension: once Parent and Plots are loaded, Subpackage3 is loaded too. Its exported names come into scope only with an explicit using Parent::Subpackage3. The example with Subpackage4 goes even further, as loading Heavy package would trigger the loading of not only Subpackage4 but also Helper. It may be a powerful feature – but potentially a bit too powerful, making the loading process hard to reason about and even causing a cascade of package loads.
To avoid ambiguity, a sibling is referred to as “::SubpackageCommons” (“Parent::SubpackageCommons” is accepted too), while a plain name always means a dependency. This keeps the two namespaces separate and allows subpackages with a name of an already registered package (e.g., “Plots”).
Extensions triggered by a subdependency of a dependency open an interesting question: Should there be a [weaksubdeps] section? Probably not, as all necessary information is present without it, but that makes the design slightly uneven.
Using subpackages from another package (its Project.toml)
[deps]
Parent = "…"
[subdeps]
Parent = ["Subpackage1", "Subpackage2"]
[weakdeps]
OtherDep = "…"
[extensions]
OtherDepExt = "OtherDep::SubA"
Again, a bit inelegant because OtherDep::SubA is the weak dependency, not the parent, OtherDep, but probably good enough.
Julia syntax
using Parent::Subpackage1 # binds Subpackage1 and brings its exports into scope
import Parent::Subpackage1 # binds Subpackage1 only
import Parent::Subpackage2 as SP # alias should be supported for subpackages
using Parent::Subpackage2: func1 # ... and also importing a single symbol.
The lines above wouldn’t bring Parent’s exported symbols into scope, so a separate using Parent might be needed. I’m not sure if it’s worth the inconvenience, but this design (i.e. not importing the parent automatically) would be useful in some cases.
Some convenience syntax might also be considered:
using Parent::{Subpackage1, Subpackage2} # both subpackages
using Parent, Parent::{Subpackage1} # the parent too
Pkg and the REPL
(MyProject) pkg> add Parent::Subpackage1 # adds Parent to [deps] (if needed) and Subpackage1 to [subdeps]
(MyProject) pkg> rm Parent::Subpackage1 # removes Subpackage1 from [subdeps], keeps Parent
(MyProject) pkg> add Parent::Subpackage2 # adds Subpackage2 to [subdeps] and installs Heavy
(MyProject) pkg> add Parent::Subpackage3 # adds Subpackage3 to [subdeps] and installs Plots
(MyProject) pkg> status
Status `~/MyProject/Project.toml`
[2a1d7b9e] Parent v1.2.3 ::{Subpackage2,Subpackage3}
The functional API would mirror the REPL commands: Pkg.add("Parent::Subpackage1") and Pkg.rm("Parent::Subpackage1"). status shows the subpackages the project uses; status --manifest shows those used anywhere in the environment.
- Resolving: the weak dependencies of the selected subpackages, and of the siblings they need, become requirements. Only those get installed, so selecting
Subpackage2installsHeavy, and removing it removesHeavyagain. - Versions: a registered parent is restricted to the versions that have the selected subpackages. Asking for a subpackage that no version has is an error.
[subdeps]of dependencies:[subdeps]in a registered dependency is honored too, so adding a package that usesParent::Subpackage1installs whatSubpackage1needs.- Developed parents: adding a subpackage of a
deved parent leaves it developed.