Skip to content

Commit

Permalink
Add documentation for source replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Aug 1, 2016
1 parent 4814a84 commit 63ac9e1
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ clean:
# === Documentation

DOCS := index faq config guide manifest build-script pkgid-spec crates-io \
environment-variables specifying-dependencies
environment-variables specifying-dependencies source-replacement
DOC_DIR := target/doc
DOC_OPTS := --markdown-no-toc \
--markdown-css stylesheets/normalize.css \
Expand Down
6 changes: 4 additions & 2 deletions src/doc/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,7 @@ that this flag *does not change the behavior of Cargo*, it simply asserts that
Cargo shouldn't touch the network as a previous command has been run to ensure
that network activity shouldn't be necessary.

Note that Cargo does not yet support vendoring in a first-class fashion, but
this is a hotly desired feature and coming soon!
For more information about vendoring, see documentation on [source
replacement][replace].

[replace]: source-replacement.html
1 change: 1 addition & 0 deletions src/doc/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ <h1>CARGO</h1>
<li><a href='config.html'>Configuration</a></li>
<li><a href='pkgid-spec.html'>Package ID specs</a></li>
<li><a href='environment-variables.html'>Environment Variables</a></li>
<li><a href='source-replacement.html'>Source Replacement</a></li>
</ul>
</div>
</div>
Expand Down
128 changes: 128 additions & 0 deletions src/doc/source-replacement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
% Replacing sources

Cargo supports the ability to **replace one source with another** to express
strategies along the lines of mirrors or vendoering dependencies. Configuration
is currently done through the [`.cargo/config` configuration][config] mechanism,
like so:

[config]: config.html

```toml
# The `source` table is where all keys related to source-replacement
# are store.
[source]

# Under the `source` table are a number of other tables whose keys are a
# name for the relevant source. For example this section defines a new
# source, called `my-awesome-source`, which comes from a directory
# located at `vendor` relative to the directory containing this `.cargo/config`
# file
[source.my-awesome-source]
directory = "vendor"

# The crates.io default source for crates is available under the name
# "crates-io", and here we use the `replace-with` key to indicate that it's
# replaced with our source above.
[source.crates-io]
replace-with = "my-awesome-source"
```

With this configuration Cargo attempt to look up all crates in the directory
"vendor" rather than querying the online registry at crates.io. Using source
replacement Cargo can express:

* Vendoring - custom sources can be defined which represent crates on the local
filesystem. These sources are subsets of the source that they're replacing and
can be checked into projects if necessary.

* Mirroring - sources can be replaced with an equivalent version which acts as a
cache for crates.io itself.

Cargo has a core assumption about source replacement that the source code is
exactly the same from both sources. In our above example Cargo assumes that all
of the crates coming from `my-awesome-source` are the exact same as the copies
from `crates-io`. Note that this also means that `my-awesome-source` is not
allowed to have crates which are not present in the `crates-io` source.

As a consequence, source replacement is not appropriate for situations such as
patching a dependency or a private registry. Cargo supports patching
dependencies through the usage of [the `[replace]` key][replace-section], and
private registry support is planned for a future version of Cargo.

[replace-section]: manifest.html#the-replace-section

## Configuration

Configuration of replacement sources is done through [`.cargo/config`][config]
and the full set of available keys are:

```toml
# Each source has its own table where the key is the name of the source
[source.the-source-name]

# Indicate that `the-source-name` will be replaced with `another-source`,
# defined elsewhere
replace-with = "another-source"

# Available kinds of sources that can be specified (described below)
registry = "https://example.com/path/to/index"
local-registry = "path/to/registry"
directory = "path/to/vendor"
```

The `crates-io` represents the crates.io online registry (default source of
crates) and can be replaced with:

```toml
[source.crates-io]
replace-with = 'another-source'
```

## Registry Sources

A "registry source" is one that is the same as crates.io itself. That is, it has
an index served in a git repository which matches the format of the
[crates.io index](https://github.com/rust-lang/crates.io-index). That repository
then has configuration indicating where to download crates from.

Currently there is not an already-available project for setting up a mirror of
crates.io. Stay tuned though!

## Local Registry Sources

A "local registry source" is intended to be a subset of another registry
source, but available on the local filesystem (aka vendoring). Local registries
are downloaded ahead of time, typically sync'd with a `Cargo.lock`, and are
made up of a set of `*.crate` files and an index like the normal registry is.

The primary way to manage and crate local registry sources is through the
[`cargo-local-registry`][cargo-local-registry] subcommand, available on
crates.io and can be installed with `cargo install cargo-local-registry`.

[cargo-local-registry]: https://crates.io/crates/cargo-local-registry

Local registries are contained within one directory and contain a number of
`*.crate` files downloaded from crates.io as well as an `index` directory with
the same format as the crates.io-index project (populated with just entries for
the crates that are present).

## Directory Sources

A "directory source" is similar to a local registry source where it contains a
number of crates available on the local filesystem, suitable for vendoring
dependencies. Also like local registries, directory sources can primarily be
managed by an external subcommand, [`cargo-vendor`][cargo-vendor], which can be
installed with `cargo install cargo-vendor`.

[cargo-vendor]: https://crates.io/crates/cargo-vendor

Directory sources are distinct from local registries though in that they contain
the unpacked version of `*.crate` files, making it more suitable in some
situations to check everything into source control. A directory source is just a
directory containing a number of other directories which contain the source code
for crates (the unpacked version of `*.crate` files). Currently no restriction
is placed on the name of each directory.

Each crate in a directory source also has an associated metadata file indicating
the checksum of each file in the crate to protect against accidental
modifications.

0 comments on commit 63ac9e1

Please sign in to comment.