Cargo for the average Off the grid person

2024-05-17 @rrobin

cargo (the rust build tool) is extremely eager to use the internet. It routinely checks for updates to the crates registry and updates dependencies, which in turn also bumps dependencies in your Cargo.lock.

This is a terrible experience if you are offline most of the time, and even worse if you are sitting behind a very slow connection.

cargo in offline mode

There is a global setting to disable network access in cargo. Place the following into your ~/.cargo/config.toml.

[net]
offline = true

This is the global config similar to --offline. From this point on attempting to run any cargo tool will not use the network. This may fail with an error such as:

$ cargo fetch
(...)

Caused by:
  can't checkout from '...': you are in the offline mode (--offline)

Defining some aliases for fetch/update

Attempting to run commands that explicitly require network access will of course fail. So you need to override the global option when you explicitly need network access to fetch or update dependencies. I use two aliases in the cargo config to enable network access:

[alias]
ofetch = "fetch --config net.offline=false"
oupdate = "update --config net.offline=false"

Cargo does NOT allow aliases to replace its main commands, which is why I use new names.

Relation to cargo.lock, and --frozen

One side effect of this is that it minimizes the changes to Cargo.lock, because no automatic updates happen, changes need to be explicit with my aliases.

However the intent behind --frozen is to require an existing Cargo.lock to be up to date, while running in offline mode does not even require Cargo.lock to be in place (it just gets recreated).

References

=> https://github.com/rust-lang/cargo/issues/8207 | https://doc.rust-lang.org/cargo/faq.html#how-can-cargo-work-offline

Proxy Information
Original URL
gemini://tilde.pink/~rrobin/2024-05-17-cargo-for-the-average-off-the-grid-person.gmi
Status Code
Success (20)
Meta
text/gemini;
Capsule Response Time
7.441335 milliseconds
Gemini-to-HTML Time
0.584153 milliseconds

This content has been proxied by September (3851b).