Skip to content

Commit e9f7566

Browse files
committed
Rewrote motiviation
1 parent 5ccbe52 commit e9f7566

File tree

1 file changed

+134
-73
lines changed

1 file changed

+134
-73
lines changed

text/3529-cargo-path-bases.md

Lines changed: 134 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,109 @@ turn enable base-relative `path` dependencies.
1212
# Motivation
1313
[motivation]: #motivation
1414

15-
While developing locally, users may wish to specify many `path`
16-
dependencies that all live in the same local directory. If that local
17-
directory is not a short distance from the `Cargo.toml`, this can get
18-
unwieldy. They may end up with a `Cargo.toml` that contains
15+
As a project grows in size, it becomes necessary to split it into smaller
16+
sub-projects, architected into layers with well-defined boundaries.
17+
18+
One way to enforce these boundaries is to use different Git repos (aka
19+
"multi-repo"). Cargo has good support for multi-repo projects using either `git`
20+
dependencies, or developers can use private registries if they want to
21+
explicitly publish code or need to preprocess their sub-projects (e.g.,
22+
generating code) before they can be consumed.
23+
24+
If all of the code is kept in a single Git repo (aka "mono-repo"), then these
25+
boundaries must be enforced a different way: either leveraging tooling during
26+
the build to check layering, or requiring that sub-projects explicitly publish
27+
and consume from some intermediate directory. Cargo has poor support for
28+
mono-repos: the only viable mechanism is `path` dependencies, but these require
29+
relative paths (which makes refactoring and moving sub-projects very difficult)
30+
and don't work at all if the mono-repo requires publishing and consuming from an
31+
intermediate directory (as this may very per host, or per target being built).
32+
33+
This RFC proposes a mechanism to specify `base` directories in `Config.toml` or
34+
`Cargo.toml` files which can be used to prepend `path` dependencies. This allows
35+
mono-repos to specify dependencies relative to their root directory, which
36+
allows the consuming project to be moved freely (no relative paths to update)
37+
and a simple find-and-replace to handle a producing project being moved.
38+
Additionally, a host-specific or target-specific intermediate directory may be
39+
specified as a `base`, allowing code to be consumed from there using `path`
40+
dependencies.
41+
42+
### Example
43+
44+
If we had a sub-project that depends on three others:
45+
46+
* `foo` which is in a different layer of the mono-repo.
47+
* `bar_with_generated` that must be consumed from an intermediate directory
48+
because it contains target-specific generated code.
49+
* `baz` which is in the current layer.
50+
51+
We may have a `Cargo.toml` snippet that looks like this:
1952

2053
```toml
21-
foo = { path = "/home/jon/dev/rust/foo" }
22-
bar = { path = "/home/jon/dev/rust/bar" }
23-
baz = { path = "/home/jon/dev/rust/ws/baz" }
54+
[dependencies]
55+
foo = { path = "../../../other_layer/foo" }
56+
bar_with_generated = { path = "../../../../intermediates/x86_64/Debug/third_layer/bar_with_generated" }
57+
baz = { path = "../baz" }
2458
```
2559

26-
This is not only frustrating to type out, but also requires many changes
27-
should any component of the path change. For example, if `foo`, `bar`,
28-
and `ws/baz` were to move under a sub-directory of `libs`, all the paths
29-
would have to be updated. If they are used in more than one local
30-
project, each project would have to be updated.
60+
This has many issues:
61+
62+
* Moving the current sub-project may require changing all of these relative
63+
paths.
64+
* `bar_with_generated` will only work if we're building x86_64 Debug.
65+
* `bar_with_generated` assumes that the `intermediates` directory is a sibling
66+
to our source directory, and not somewhere else completely (e.g., a different
67+
drive for performance reasons).
68+
* Moving `foo` or `baz` requires searching the code for each possible relative
69+
path (e.g., `../../../other_layer/foo` and `../foo`) and may be error prone if
70+
there is some other sub-project in directory with the same name.
3171

32-
As related issue arises in contexts where an external build system may
33-
make certain dependencies available through vendoring. Such a build
34-
system might place vendored packages under some complex path under a
35-
build-root, like
72+
Instead, if we could specify these `base` directories in a `Config.toml` (which
73+
may be generated by an external build system which in turn invokes Cargo):
3674

75+
```toml
76+
[base-paths]
77+
sources = "/home/user/dev/src"
78+
intermediates = "/home/user/dev/intermediates/x86_64/Debug"
3779
```
38-
/home/user/workplace/feature-1/build/first-party-package/first-party-package-1.0/x86_64/dev/build/private/rust-vendored/
80+
81+
Then the `Cargo.toml` can use those `base` directories and avoid relative paths:
82+
83+
```toml
84+
[dependencies]
85+
foo = { path = "other_layer/foo", base = "sources" }
86+
bar_with_generated = { path = "third_layer/bar_with_generated", base = "intermediates" }
87+
baz = { path = "this_layer/baz", base = "sources" }
3988
```
4089

41-
If a developer wishes to use such an auto-vendored dependency, a
42-
contract must be established with the build system about exactly where
43-
vendred dependencies will end up. And since that path may not be near
44-
the project's `Cargo.toml`, the user's `Cargo.toml` may end up with
45-
either an absolute path or a long relative path, both of which may not
46-
work on other hosts, and thus cannot be checked in (or must be
47-
overwritten in-place by the build system).
48-
49-
The proposed mechanism aims to simplify both of these use-cases by
50-
introducing named "base" paths in the Cargo configuration
51-
(`.cargo/config.toml`). Path dependencies can then be given relative to
52-
those base path names, which can be set either by a local developer in
53-
their user-wide configuration (`~/.cargo/config.toml`), or by an
54-
external build system in a project-wide configuration file.
55-
56-
This effectively makes a "group" of path dependencies available at some
57-
undisclosed location to `Cargo.toml`, which then only has to know the
58-
layout to path dependencies _within_ that directory, and not the path
59-
_to_ that directory.
90+
Which resolves the issues we previously had:
91+
92+
* The current project can be moved without modifying the `Cargo.toml` at all.
93+
* `bar_with_generated` works for all targets (assuming the `Config.toml` is
94+
generated).
95+
* The `intermediates` directory can be placed anywhere.
96+
* Moving `foo` or `baz` only requires searching for the canonical form relative
97+
to the `base` directory.
98+
99+
## Other uses
100+
101+
The ability to use `base` directories for `path` dependencies is convenient for
102+
developers who are using a large number of `path` dependencies within the same
103+
root directory. Instead of repeating the same path fragment many times in their
104+
`Cargo.toml`, they can instead specify it once in a `Config.toml` as a `base`
105+
directory, then use that `base` directory in each of their `path` dependencies.
106+
107+
Cargo can also provide built-in base paths, for example `workspace` to point to
108+
the root directory of the workspace. This allows workspace members to reference
109+
each other without first needing to `../` their way back to the workspace root.
60110

61111
# Guide-level explanation
62112
[guide-level-explanation]: #guide-level-explanation
63113

64114
If you often use path dependencies that live in a particular location,
65115
or if you want to avoid putting long paths in your `Cargo.toml`, you can
66-
define path _base directories_ in your [Cargo
67-
configuration](https://doc.rust-lang.org/cargo/reference/config.html).
116+
define path _base directories_ in your Cargo [manifest](https://doc.rust-lang.org/cargo/reference/manifest.html)
117+
or [configuration](https://doc.rust-lang.org/cargo/reference/config.html).
68118
Your path dependencies can then be specified relative to those
69119
directories.
70120

@@ -74,7 +124,7 @@ For example, say you have a number of projects checked out in
74124
`~/.cargo/config.toml`:
75125

76126
```toml
77-
[base_path]
127+
[base-paths]
78128
dev = "/home/user/dev/rust/libraries/"
79129
```
80130

@@ -93,60 +143,71 @@ the path must exist on any other host where you want to use the same
93143
# Reference-level explanation
94144
[reference-level-explanation]: #reference-level-explanation
95145

96-
## Configuration
146+
## Specifying Dependencies
97147

98-
`[base_path]`
148+
### Base Paths
99149

100-
* Type: string
101-
* Default: see below
102-
* Environment: `CARGO_BASE_PATH_<name>`
150+
A `path` dependency may optionally specify a base path by setting the `base` key
151+
to the name of a base path from the `[base-paths]` table in either the
152+
[manifest](https://doc.rust-lang.org/cargo/reference/manifest.html) or
153+
[configuration](https://doc.rust-lang.org/cargo/reference/config.html#base-paths)
154+
or one of the [built-in base paths](#built-in-base-paths). The value of that
155+
base path is prepended to the `path` value to produce the actual location where
156+
Cargo will look for the dependency.
103157

104-
The `[base_path]` table defines a set of path prefixes that can be used to
105-
prepend the locations of `path` dependencies. Each key in the table is the name
106-
of the base path and the value is the actual file system path. These base paths
107-
can be used in a `path` dependency by setting its `base` key to the name of the
108-
base path to use.
158+
For example, if the Cargo.toml contains:
109159

110160
```toml
111-
[base_path]
112-
dev = "/home/user/dev/rust/libraries/"
161+
[dependencies]
162+
foo = { path = "foo", base = "dev" }
113163
```
114164

115-
The "dev" base path may then be referenced in a `Cargo.toml`:
165+
Given a `[base-paths]` table in the configuration that contains:
116166

117167
```toml
118-
[dependencies]
119-
foo = { path = "foo", base = "dev" }
168+
[base-paths]
169+
dev = "/home/user/dev/rust/libraries/"
120170
```
121171

122-
To produce a `path` dependency `foo` located at
172+
This will produce a `path` dependency `foo` located at
123173
`/home/user/dev/rust/libraries/foo`.
124174

175+
If the base path is not found in any `[base-paths]` table or one of the built-in
176+
base paths then Cargo will generate an error.
125177

126-
## Specifying Dependencies
178+
If the name of a base path is specified in both the manifest and configuration,
179+
then the value in the manifest is preferred.
127180

128-
A `path` dependency may optionally specify a base path by setting the `base` key
129-
to the name of a base path from the `[base_path]` table in the configuration.
130-
The value of that base path in the configuration is prepended to the `path`
131-
value to produce the actual location where Cargo will look for the dependency.
181+
The name of a base path must use only [alphanumeric](https://doc.rust-lang.org/std/primitive.char.html#method.is_alphanumeric)
182+
characters or `-` or `_`, and cannot be empty.
132183

133-
If the base path is not found in the `[base_path]` table then Cargo will
134-
generate an error.
184+
#### Built-in base paths
135185

136-
```toml
137-
[dependencies]
138-
foo = { path = "foo", base = "dev" }
139-
```
186+
Cargo provides implicit base paths that can be used without the need to specify
187+
them in a `[base-paths]` table.
140188

141-
Given a `[base_path]` table in the configuration that contains:
189+
* `workspace` - If a project is [a workspace or workspace member](https://doc.rust-lang.org/cargo/reference/workspaces.html)
190+
then this base path is defined as the path to the directory containing the root
191+
Cargo.toml of the workspace.
142192

143-
```toml
144-
[base_path]
145-
dev = "/home/user/dev/rust/libraries/"
146-
```
193+
If one of these built-in base paths is also specified in the manifest or
194+
configuration, then that value is preferred over the built-in value.
147195

148-
Will then produce a `path` dependency `foo` located at
149-
`/home/user/dev/rust/libraries/foo`.
196+
## The Manifest Format
197+
198+
[`[base-paths]`](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#base-paths) - Base paths for path dependencies.
199+
200+
## Configuration
201+
202+
`[base-paths]`
203+
204+
* Type: string
205+
* Default: see below
206+
* Environment: `CARGO_BASE_PATHS_<name>`
207+
208+
The `[base-paths]` table defines a set of path prefixes that can be used to
209+
prepend the locations of `path` dependencies. See the [specifying dependencies](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#base-paths)
210+
documentation for more information.
150211

151212
# Drawbacks
152213
[drawbacks]: #drawbacks

0 commit comments

Comments
 (0)