Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add information about packaging crate #2408

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ cargo run -- build world
# Builds a local package you have at <SOURCE> and adds it to the database.
# The package does not have to be on crates.io.
# The package must be on the local filesystem, git urls are not allowed.
# Usually this command can be applied directly to a crate root
# In certain scenarios it might be necessary to first package the respective
# crate by using the `cargo package` command.
# See also /docs/build-workspaces.md
cargo run -- build crate --local /path/to/source
```

Expand Down
93 changes: 93 additions & 0 deletions docs/build-workspaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Build Workspaces
## When do we need this?
Many workspace packages do not need manual intervention and can be built simply by executing the commands listed in the main [Readme.md](../Readme.md) file.
However, some workspaces require an additional step.
This is the case when values such as
```toml
version.workspace = true
```
are inherited from the workspaces `Cargo.toml` configuration file.

## Fix
To build documentation, rustdoc requires a fully specified package but rustdoc does not understand workspaces which are only defined in cargo.
Thus our crate needs to be packaged by cargo before running the documentation.
This step will replace all of the `value.workspace = true` statements with their respective values.
```
cargo package
```
This will emit a packaged crate into the `target/package/your_crate_name-version` folder.
Now the commands specified in [Readme.md](../Readme.md) can be executed targeting this folder.
```
cargo run -- build crate --local /path/to/source/target/package/your_crate_name-version/
```

## Full MWE
To showcase when such problems can occur, take a look at the following example.
### Structure
```bash
$ tree
.
├── Cargo.toml
├── my_lib
│   ├── Cargo.toml
│   └── src
│   └── lib.rs
└── README.md

3 directories, 4 files
```
The actual contents of `my_lib` do not matter, only the two configuration files.
```
$ cat Cargo.toml
[workspace]
members = [
"my_lib",
]

[workspace.package]
version = "0.1.0"
```
and
```bash
$ cat my_lib/Cargo.toml
[package]
name = "my_lib"
version.workspace = true

[dependencies]
```

### Building

The build command
```bash
cargo run -- build crate -l path/to/docs_rs_workspace_package/my_lib
```
fails with
```bash
Error: Building documentation failed

Caused by:
Building documentation failed

Caused by:
invalid Cargo.toml syntax
```
which makes sense due to
```toml
version.workspace = true
```

### Fix
However when running the following sequence of commands
```bash
# Run this in the directory of docs_rs_workspace_package
cargo package -p my_lib
```
and then building again
```bash
# Run this from the docs.rs repo
cargo run -- build crate -l path/to/docs_rs_workspace_package/target/package/my_lib-0.1.0
```
then the build succeeds.

Loading