How do I build completely offline?

Hi!

I can build a program (with external crate dependencies) with Cargo with internet connection, as it fetches said dependencies from crates.io. Passing --offline to cargo when doing so (after having built with internet connection) works fine, but as soon as ~/.cargo/registry/index/index.crates.io-xyz/.cache is deleted, the build fails with cannot locate crate 'crate' and it states that it looked in crates.io index. Even if I have the source for the dependencies in ~/.cargo/registry/src.

I'd like to be able to build it completely offline, and I don't see any reason why this can't happen if I've already got the sources.

How can I resolve the error so Cargo looks for the packages/sources locally rather than on crates.io?

Any help is greatly appreciated!

Is cargo vendor an option for you?

The reason for the failure would be that the index (not the package manifests) is used for performing dependency resolution. That said, I don’t know whether there’s a reason it is necessary even when you have an up-to-date lock file.

But I have to ask: Why are you deleting the index cache? It’s a local copy of the relevant parts of the registry, and it can be (and is) used offline. If you’re willing to rely on ~/.cargo/registry/src having the needed contents, what’s wrong with relying on ~/.cargo/registry/index too?

If you want to do hardcore "reimplements all network access" then you'll need to write out your own copies of those registry cache files instead of deleting them.

I think crates.io isn't so big web site. Download it and then change DNS entry.

cargo vendor is exactly what I was looking for. Thanks!

I just found that since the rules for including vendor dependencies are in .cargo/config.toml, they aren't included when building from a different directory with --manifest-path=path, so the vendored dependencies do nothing. Is there a way to circumvent this?

You can pass --config /path/to/.cargo/config.toml.