Skip to content

Commit 2e10986

Browse files
committed
guide: add section about cargo home
1 parent 8f5bdc4 commit 2e10986

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

src/doc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [Cargo.toml vs Cargo.lock](guide/cargo-toml-vs-cargo-lock.md)
1616
* [Tests](guide/tests.md)
1717
* [Continuous Integration](guide/continuous-integration.md)
18+
* [Cargo home](guide/cargo-home.md)
1819
* [Build Cache](guide/build-cache.md)
1920

2021
* [Cargo Reference](reference/index.md)

src/doc/src/guide/cargo-home.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Cargo Home
2+
3+
The cargo home functions as a download and source cache.
4+
When building a crate, cargo stores downloaded build dependencies in the cargo home.
5+
You can alter the location of the cargo home by setting the `CARGO_HOME` [environmental variable](../reference/environment-variables.html).
6+
The [home](https://crates.io/crates/home) crate provides an api for getting this location if you need this information inside your rust crate.
7+
By default, the cargo home is located in `${HOME}/.cargo/`.
8+
9+
Please note that the internal structure of the cargo home is not stabilized and may be subject to change at any time.
10+
11+
The cargo home consists of following components:
12+
13+
## Files:
14+
15+
* `config`
16+
Cargos global configuration file, see the [config entry in the reference](../reference/config.html).
17+
18+
* `credentials`
19+
Private login credentials from [`cargo login`](../commands/cargo-login.html) in order to login into a registry.
20+
21+
* `.crates.toml`
22+
This hidden file contains package information of crates installed via [cargo install](../commands/cargo-install.html). Do NOT edit by hand!
23+
24+
## Directories:
25+
26+
* `bin`
27+
The bin directory contains executables of crates that were installed via "cargo install" or `rustup`.
28+
To be able to make these binaries accessible, add the path of the directory to your `${PATH}`.
29+
30+
* `git`
31+
Git sources are stored here:
32+
33+
* `git/db`
34+
When a crate depends on a git repository, cargo clones the repo as a bare repo into this directory and updates it if neccessary.
35+
36+
* `git/checkouts`
37+
If a git source is used, the required commit of the repo is checked out from the bare repo inside `git/db` into this directory.
38+
This provides the compiler with the actual files contained in the repo of the commit specified for that dependency.
39+
Multiple checkouts of different commits of the same repo are possible.
40+
41+
* `registry`
42+
Packages and metadata of crate registries (such as crates.io) are located here.
43+
44+
* `registry/index`
45+
The index is a bare git repository which contains the metadata (versions, dependencies etc) of all available crates of a registry.
46+
47+
* `registry/cache`
48+
Downloaded dependencies are stored in the cache. The crates are compressed gzip archives named with a `.crate` extension.
49+
50+
* `registry/src`
51+
If a downloaded `.crate` archive is required by a package, it is unpacked into `registry/src` folder where rustc will find the `.rs` files.
52+
53+
54+
## Caching the cargo home in CI
55+
56+
To avoid redownloading all crate dependencies during continuous integration, you can cache the `$CARGO_HOME` directory.
57+
However, caching the entire directory as is is often inefficient as it will contain downloaded sources twice.
58+
If we depend on `cargo 0.38.0` and cache the entire `$CARGO_HOME` we would actually cache the sources twice, the `cargo-0.38.0.crate` inside `registry/cache` and the extracted `.rs` files of cargoinside `registry/src`.
59+
The can unneccessarily slow down the build as downloading, extracting, recompressing and reuploading the cache ot the CI servers can take some time.
60+
61+
It should be sufficient to only cache the following directories across builds:
62+
63+
* `bin/`
64+
* `registry/index/`
65+
* `registry/cache/`
66+
* `git/db/`
67+
68+
69+
70+
## Vendoring all dependencies of a project
71+
72+
See the [cargo vendor](commands/cargo-vendor.md) subcommand.
73+
74+
75+
76+
## Clearing the cache
77+
78+
In theory, you can always remove any part of the cache and cargo will do its best to restore sources if a crate needs them either by reextracting an archive or checking out a bare repo or by simply redownloading the sources from the web.
79+
80+
Alternatively, the [cargo-cache](https://crates.io/crates/cargo-cache) crate provides a simple CLI tool to only clear selected parts of the cache or show sizes of its components in your commandline.

src/doc/src/guide/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ develop Rust packages.
1111
* [Cargo.toml vs Cargo.lock](cargo-toml-vs-cargo-lock.md)
1212
* [Tests](tests.md)
1313
* [Continuous Integration](continuous-integration.md)
14+
* [Cargo Home](cargo-home.md)
1415
* [Build Cache](build-cache.md)

src/doc/src/reference/environment-variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ system:
1313
checkouts of crates. By default these are stored under `$HOME/.cargo`, but
1414
this variable overrides the location of this directory. Once a crate is cached
1515
it is not removed by the clean command.
16+
For more details refer to the [guide](../guide/cargo-home.md).
1617
* `CARGO_TARGET_DIR` — Location of where to place all generated artifacts,
1718
relative to the current working directory.
1819
* `RUSTC` — Instead of running `rustc`, Cargo will execute this specified

src/etc/man/cargo.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ downloaded dependencies.
376376
.RS 4
377377
This directory contains cached downloads of git dependencies.
378378
.RE
379+
.sp
380+
Please note that the internal structure of the $CARGO_HOME is not stable yet and may be subject to change.
381+
.sp
379382
.SH "EXAMPLES"
380383
.sp
381384
.RS 4

0 commit comments

Comments
 (0)