julia

Using Julia packages in `--extra`


When working on a Julia package, we can state that a certain dependency is not user-facing by adding it with ]add --extra Foo. This puts Foo in the [extras] section of the Project.toml.

However, while testing this I encountered a strange problem:

julia --project=.
(Bar) pkg> add --extra JuliaFormatter
julia> using JuliaFormatter
 │ Package JuliaFormatter not found, but a package named JuliaFormatter is available from a registry.
 │ Install package?
 │   (Bar) pkg> add JuliaFormatter
 └ (y/n/o) [y]:

How do I activate my project with it's extras enabled?


Solution

  • [extras] is useless on its own. You also have to specify [targets]. extras is used to specify dependencies for a subdirectory, where the package itself doesn't use these dependencies. In the example TOML below:

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

    Test is specified as a dependency for the subdirectory, target, test/.

    This is documented in https://pkgdocs.julialang.org/v1/creating-packages/#target-based-test-specific-dependencies.

    You can specify arbitrarily many dependencies for the test and build targets:

    [extras]
    Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
    JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
    Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
    XUnit = "3e3c03f2-1a94-11e9-2981-050a4ca824ab"
    
    [targets]
    test = ["Aqua", "JET", "XUnit", "Test"]
    

    but the feature isn't generic enough to support arbitrary targets. Refer to this feature request which describes precisely what you need, and this related feature proposal.