Skip to content

Commit 3523760

Browse files
authored
update docs to show how workspaces are used for test deps (#4428)
1 parent b620562 commit 3523760

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

docs/src/creating-packages.md

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -275,58 +275,37 @@ test-specific dependencies, are available, see below.
275275

276276
### Test-specific dependencies
277277

278-
There are two ways of adding test-specific dependencies (dependencies that are not dependencies of the package but will still be available to
279-
load when the package is tested).
278+
Test-specific dependencies are dependencies that are not dependencies of the package itself but are available when the package is tested.
280279

281-
#### `target` based test specific dependencies
280+
#### Recommended approach: Using workspaces with `test/Project.toml`
282281

283-
Using this method of adding test-specific dependencies, the packages are added under an `[extras]` section and to a test target,
284-
e.g. to add `Markdown` and `Test` as test dependencies, add the following to the `Project.toml` file:
285-
286-
```toml
287-
[extras]
288-
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
289-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
290-
291-
[targets]
292-
test = ["Markdown", "Test"]
293-
```
294-
295-
Note that the only supported targets are `test` and `build`, the latter of which (not recommended) can be used
296-
for any `deps/build.jl` scripts.
282+
!!! compat
283+
Workspaces require Julia 1.12+. For older Julia versions, see the legacy approaches below.
297284

298-
#### Alternative approach: `test/Project.toml` file test specific dependencies
285+
The recommended way to add test-specific dependencies is to use workspaces. This is done by:
299286

300-
!!! note
301-
The exact interaction between `Project.toml`, `test/Project.toml` and their corresponding
302-
`Manifest.toml`s are not fully worked out and may be subject to change in future versions.
303-
The older method of adding test-specific dependencies, described in the previous section,
304-
will therefore be supported throughout all Julia 1.X releases.
287+
1. Adding a `[workspace]` section to your package's `Project.toml`:
305288

306-
In Julia 1.2 and later test dependencies can be declared in `test/Project.toml`. When running
307-
tests, Pkg will automatically merge this and the package Projects to create the test environment.
308-
309-
!!! note
310-
If no `test/Project.toml` exists Pkg will use the `target` based test specific dependencies.
289+
```toml
290+
[workspace]
291+
projects = ["test"]
292+
```
311293

312-
To add a test-specific dependency, i.e. a dependency that is available only when testing,
313-
it is thus enough to add this dependency to the `test/Project.toml` project. This can be
314-
done from the Pkg REPL by activating this environment, and then use `add` as one normally
315-
does. Let's add the `Test` standard library as a test dependency:
294+
2. Creating a `test/Project.toml` file with your test dependencies:
316295

317296
```julia-repl
318297
(HelloWorld) pkg> activate ./test
319298
[ Info: activating environment at `~/HelloWorld/test/Project.toml`.
320299
321-
(test) pkg> add Test
300+
(HelloWorld/test) pkg> add Test
322301
Resolving package versions...
323302
Updating `~/HelloWorld/test/Project.toml`
324303
[8dfed614] + Test
325-
Updating `~/HelloWorld/test/Manifest.toml`
326-
[...]
327304
```
328305

329-
We can now use `Test` in the test script and we can see that it gets installed when testing:
306+
When using workspaces, the package manager resolves dependencies for all projects in the workspace together, and creates a single `Manifest.toml` next to the base `Project.toml`. This provides better dependency resolution and makes it easier to manage test-specific dependencies.
307+
308+
You can now use `Test` in the test script:
330309

331310
```julia-repl
332311
julia> write("test/runtests.jl",
@@ -335,19 +314,53 @@ julia> write("test/runtests.jl",
335314
@test 1 == 1
336315
""");
337316
338-
(test) pkg> activate .
317+
(HelloWorld/test) pkg> activate .
339318
340319
(HelloWorld) pkg> test
341320
Testing HelloWorld
342321
Resolving package versions...
343-
Updating `/var/folders/64/76tk_g152sg6c6t0b4nkn1vw0000gn/T/tmpPzUPPw/Project.toml`
344-
[d8327f2a] + HelloWorld v0.1.0 [`~/.julia/dev/Pkg/HelloWorld`]
345-
[8dfed614] + Test
346-
Updating `/var/folders/64/76tk_g152sg6c6t0b4nkn1vw0000gn/T/tmpPzUPPw/Manifest.toml`
347-
[d8327f2a] + HelloWorld v0.1.0 [`~/.julia/dev/Pkg/HelloWorld`]
348-
Testing HelloWorld tests passed```
322+
Testing HelloWorld tests passed
323+
```
324+
325+
Workspaces can also be used for other purposes, such as documentation or benchmarks, by adding additional projects to the workspace:
326+
327+
```toml
328+
[workspace]
329+
projects = ["test", "docs", "benchmarks"]
349330
```
350331

332+
See the section on [Workspaces](@ref) in the `Project.toml` documentation for more details.
333+
334+
#### Legacy approach: `target` based test specific dependencies
335+
336+
!!! warning
337+
This approach is legacy and maintained for compatibility. New packages should use workspaces instead.
338+
339+
Using this method, test-specific dependencies are added under an `[extras]` section and to a test target:
340+
341+
```toml
342+
[extras]
343+
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
344+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
345+
346+
[targets]
347+
test = ["Markdown", "Test"]
348+
```
349+
350+
Note that the only supported targets are `test` and `build`, the latter of which (not recommended) can be used for any `deps/build.jl` scripts.
351+
352+
#### Legacy approach: `test/Project.toml` without workspace
353+
354+
!!! warning
355+
This approach is legacy and maintained for compatibility. New packages should use workspaces instead.
356+
357+
In Julia 1.2 and later, test dependencies can be declared in `test/Project.toml` without using a workspace. When running tests, Pkg will automatically merge the package and test projects to create the test environment.
358+
359+
!!! note
360+
If no `test/Project.toml` exists, Pkg will use the `target` based test specific dependencies.
361+
362+
This approach works similarly to the workspace approach, but without the workspace declaration in the main `Project.toml`.
363+
351364
## Compatibility on dependencies
352365

353366
Every dependency should in general have a compatibility constraint on it.
@@ -450,9 +463,7 @@ Extensions can have arbitrary names (here `ContourExt`), following the format of
450463
In `Pkg` output, extension names are always shown together with their parent package name.
451464

452465
!!! compat
453-
Often you will put the extension dependencies into the `test` target so they are loaded when running e.g. `Pkg.test()`. On earlier Julia versions
454-
this requires you to also put the package in the `[extras]` section. This is unfortunate but the project verifier on older Julia versions will
455-
complain if this is not done.
466+
Often you will want to load extension dependencies when testing your package. The recommended approach is to use workspaces and add the extension dependencies to your `test/Project.toml` (see [Test-specific dependencies](@ref adding-tests-to-packages)). For older Julia versions that don't support workspaces, you can put the extension dependencies into the `test` target, which requires you to also put the package in the `[extras]` section. The project verifier on older Julia versions will complain if this is not done.
456467

457468
!!! note
458469
If you use a manifest generated by a Julia version that does not know about extensions with a Julia version that does
@@ -557,11 +568,13 @@ This is done by making the following changes (using the example above):
557568

558569
In the case where one wants to use an extension (without worrying about the
559570
feature of the extension being available on older Julia versions) while still
560-
supporting older Julia versions the packages under `[weakdeps]` should be
571+
supporting older Julia versions without workspace support, the packages under `[weakdeps]` should be
561572
duplicated into `[extras]`. This is an unfortunate duplication, but without
562573
doing this the project verifier under older Julia versions will throw an error
563574
if it finds packages under `[compat]` that is not listed in `[extras]`.
564575

576+
For Julia 1.13+, using workspaces is recommended and this duplication is not necessary.
577+
565578
## Package naming guidelines
566579

567580
Package names should be sensible to most Julia users, *even to those who are not domain experts*.

docs/src/toml-files.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ constraints in detail. It is also possible to list constraints on `julia` itself
190190
julia = "1.1"
191191
```
192192

193-
### The `[workspace]` section
193+
### [The `[workspace]` section](@id Workspaces)
194194

195195
A project file can define a workspace by giving a set of projects that is part of that workspace.
196196
Each project in a workspace can include their own dependencies, compatibility information, and even function as full packages.
@@ -204,7 +204,7 @@ A workspace is defined in the base project by giving a list of the projects in i
204204
projects = ["test", "docs", "benchmarks", "PrivatePackage"]
205205
```
206206

207-
This structure is particularly beneficial for developers using a monorepo approach, where a large number of unregistered packages may be involved. It's also useful for adding documentation or benchmarks to a package by including additional dependencies beyond those of the package itself.
207+
This structure is particularly beneficial for developers using a monorepo approach, where a large number of unregistered packages may be involved. It's also useful for adding test-specific dependencies to a package by including a `test` project in the workspace (see [Test-specific dependencies](@ref adding-tests-to-packages)), or for adding documentation or benchmarks with their own dependencies.
208208

209209
Workspace can be nested: a project that itself defines a workspace can also be part of another workspace.
210210
In this case, the workspaces are "merged" with a single manifest being stored alongside the "root project" (the project that doesn't have another workspace including it).

0 commit comments

Comments
 (0)