Skip to content

Commit 707870d

Browse files
committed
Remove support for manifest, rename to path-base
1 parent 247ff65 commit 707870d

File tree

1 file changed

+93
-76
lines changed

1 file changed

+93
-76
lines changed

text/3529-cargo-path-bases.md

Lines changed: 93 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ relative paths (which makes refactoring and moving sub-projects very difficult)
3030
and don't work at all if the mono-repo requires publishing and consuming from an
3131
intermediate directory (as this may very per host, or per target being built).
3232

33-
This RFC proposes a mechanism to specify `base` directories in `Config.toml` or
33+
This RFC proposes a mechanism to specify path bases in `Config.toml` or
3434
`Cargo.toml` files which can be used to prepend `path` dependencies. This allows
3535
mono-repos to specify dependencies relative to their root directory, which
3636
allows the consuming project to be moved freely (no relative paths to update)
@@ -69,16 +69,16 @@ drive for performance reasons).
6969
path (e.g., `../../../other_layer/foo` and `../foo`) and may be error prone if
7070
there is some other sub-project in directory with the same name.
7171

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):
72+
Instead, if we could specify these common paths as path bases in a `Config.toml`
73+
(which may be generated by an external build system which in turn invokes Cargo):
7474

7575
```toml
76-
[base-paths]
76+
[path-bases]
7777
sources = "/home/user/dev/src"
7878
intermediates = "/home/user/dev/intermediates/x86_64/Debug"
7979
```
8080

81-
Then the `Cargo.toml` can use those `base` directories and avoid relative paths:
81+
Then the `Cargo.toml` can use those path bases and avoid relative paths:
8282

8383
```toml
8484
[dependencies]
@@ -94,15 +94,15 @@ Which resolves the issues we previously had:
9494
generated).
9595
* The `intermediates` directory can be placed anywhere.
9696
* Moving `foo` or `baz` only requires searching for the canonical form relative
97-
to the `base` directory.
97+
to the path base.
9898

9999
## Other uses
100100

101-
The ability to use `base` directories for `path` dependencies is convenient for
101+
The ability to use path bases for `path` dependencies is convenient for
102102
developers who are using a large number of `path` dependencies within the same
103103
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.
104+
`Cargo.toml`, they can instead specify it once in a `Config.toml` as a path
105+
base, then use that path base in each of their `path` dependencies.
106106

107107
Cargo can also provide built-in base paths, for example `workspace` to point to
108108
the root directory of the workspace. This allows workspace members to reference
@@ -111,20 +111,20 @@ each other without first needing to `../` their way back to the workspace root.
111111
# Guide-level explanation
112112
[guide-level-explanation]: #guide-level-explanation
113113

114-
If you often use path dependencies that live in a particular location,
114+
If you often use multiple path dependencies that have a common parent directory,
115115
or if you want to avoid putting long paths in your `Cargo.toml`, you can
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).
118-
Your path dependencies can then be specified relative to those
116+
define path _base directories_ in your
117+
[configuration](https://doc.rust-lang.org/cargo/reference/config.html).
118+
Your path dependencies can then be specified relative to those base
119119
directories.
120120

121-
For example, say you have a number of projects checked out in
121+
For example, say you have a number of projects checked out in
122122
`/home/user/dev/rust/libraries/`. Rather than use that path in your
123123
`Cargo.toml` files, you can define it as a "base" path in
124124
`~/.cargo/config.toml`:
125125

126126
```toml
127-
[base-paths]
127+
[path-bases]
128128
dev = "/home/user/dev/rust/libraries/"
129129
```
130130

@@ -145,14 +145,13 @@ the path must exist on any other host where you want to use the same
145145

146146
## Specifying Dependencies
147147

148-
### Base Paths
148+
### Path Bases
149149

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
150+
A `path` dependency may optionally specify a base by setting the `base` key to
151+
the name of a path base from the `[path-bases]` table in either the
152+
[configuration](https://doc.rust-lang.org/cargo/reference/config.html#path-bases)
153+
or one of the [built-in path bases](#built-in-path-bases). The value of that
154+
path base is prepended to the `path` value to produce the actual location where
156155
Cargo will look for the dependency.
157156

158157
For example, if the Cargo.toml contains:
@@ -162,67 +161,51 @@ For example, if the Cargo.toml contains:
162161
foo = { path = "foo", base = "dev" }
163162
```
164163

165-
Given a `[base-paths]` table in the configuration that contains:
164+
Given a `[path-bases]` table in the configuration that contains:
166165

167166
```toml
168-
[base-paths]
167+
[path-bases]
169168
dev = "/home/user/dev/rust/libraries/"
170169
```
171170

172171
This will produce a `path` dependency `foo` located at
173172
`/home/user/dev/rust/libraries/foo`.
174173

175-
Base paths can be either absolute or relative. Relative base paths are relative
176-
to the parent directory of the configuration or manifest file that declared that
177-
base path.
174+
Path bases can be either absolute or relative. Relative path bases are relative
175+
to the parent directory of the configuration file that declared that path base.
178176

179-
The name of a base path must use only [alphanumeric](https://doc.rust-lang.org/std/primitive.char.html#method.is_alphanumeric)
177+
The name of a path base must use only [alphanumeric](https://doc.rust-lang.org/std/primitive.char.html#method.is_alphanumeric)
180178
characters or `-` or `_`, must start with an [alphabetic](https://doc.rust-lang.org/std/primitive.char.html#method.is_alphabetic)
181179
character, and must not be empty.
182180

183-
#### Built-in base paths
181+
If the name of path base used in a dependency is neither in the configuration
182+
nor one of the built-in path base, then Cargo will raise an error.
184183

185-
Cargo provides implicit base paths that can be used without the need to specify
186-
them in a `[base-paths]` table.
184+
#### Built-in path base
185+
186+
Cargo provides implicit path bases that can be used without the need to specify
187+
them in a `[path-bases]` table.
187188

188189
* `workspace` - If a project is [a workspace or workspace member](https://doc.rust-lang.org/cargo/reference/workspaces.html)
189-
then this base path is defined as the parent directory of the root Cargo.toml of
190+
then this path base is defined as the parent directory of the root Cargo.toml of
190191
the workspace.
191192

192-
#### Base paths resolution order
193-
194-
Cargo will search for base paths in the following order:
195-
196-
1. In the [configuration](https://doc.rust-lang.org/cargo/reference/config.html).
197-
1. In the [manifest](https://doc.rust-lang.org/cargo/reference/manifest.html)
198-
that is using the base path.
199-
1. In the workspace's [manifest](https://doc.rust-lang.org/cargo/reference/manifest.html).
200-
1. In the list of built-in base paths.
201-
202-
If the base path is not found in during this search then Cargo will generate an
203-
error.
204-
205-
Once a base path is found Cargo will stop searching, thus it is possible to
206-
shadow a base path by declaring it earlier in the search order. This allows the
207-
user's configuration to override the base path in a package, or for Cargo to add
208-
new built-in base paths without compatibility issues (as existing uses will
209-
shadow the built-in name).
210-
211-
## The Manifest Format
212-
213-
* Dependency tables:
214-
* [`[base-paths]`](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#base-paths) - Base paths for path dependencies.
193+
If a built-in path base name is also declared in the configuration, then Cargo
194+
will prefer the value in the configuration. The allows Cargo to add new built-in
195+
path bases without compatibility issues (as existing uses will shadow the
196+
built-in name).
215197

216198
## Configuration
217199

218-
`[base-paths]`
200+
`[path-bases]`
219201

220202
* Type: string
221203
* Default: see below
222-
* Environment: `CARGO_BASE_PATHS_<name>`
204+
* Environment: `CARGO_PATH_BASES_<name>`
223205

224-
The `[base-paths]` table defines a set of path prefixes that can be used to
225-
prepend the locations of `path` dependencies. See the [specifying dependencies](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#base-paths)
206+
The `[path-bases]` table defines a set of path prefixes that can be used to
207+
prepend the locations of `path` dependencies. See the
208+
[specifying dependencies](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#path-bases)
226209
documentation for more information.
227210

228211
# Drawbacks
@@ -232,7 +215,7 @@ documentation for more information.
232215
`Cargo.toml` that may not be accessible when others try to build the
233216
same project. Specifically, it may now be that the other host has a
234217
`path` dependency available at the same relative path to `Cargo.toml`
235-
as the author of the `Cargo.toml` entry, but does not have the `base`
218+
as the author of the `Cargo.toml` entry, but does not have the path base
236219
defined (or has it defined as some other value).
237220

238221
At the same time, this might make path dependencies _more_ re-usable
@@ -325,6 +308,28 @@ in strategic locations, cluttering their directories. The proposed
325308
mechanism is simple to understand and to use, and still covers a wide
326309
variety of use-cases.
327310

311+
## Support for declaring path bases in the manifest
312+
313+
Currently path bases only support being declared in the configuration, and not
314+
the manifest. While it would be possible to add support for declaring path bases
315+
in the manifest in the future (which would require specifying if the declaration
316+
in the manifest or configuration is prefered, and how workspace versus members
317+
declarations work), it is hard to justify the additional complexity of adding of
318+
adding this capability to the initial implementation of the feature.
319+
320+
An argument could be made that specifying path bases in the manifest is a
321+
convenience feature, allowing a common path where multiple local dependencies
322+
exist to be specified as a path base so that the individual path dependencies
323+
would be shorter. However, it would be just as easy to add a configuration file
324+
to some parent directory of the dependent and this would be more useful as it is
325+
likely that those dependencies will also be used in other local packages thus
326+
saving the path bases table being duplicated in multiple manifests.
327+
328+
It could also be argued that specifying path bases in the manifest would be a
329+
way to set "default values" for path dependencies (e.g., to a submodule) that a
330+
developer could override in their local configuration file. While this may be
331+
useful, this scenario is already taken care of by the `patch` feature in Cargo.
332+
328333
# Prior art
329334
[prior-art]: #prior-art
330335

@@ -376,24 +381,34 @@ could help.
376381
# Unresolved questions
377382
[unresolved-questions]: #unresolved-questions
378383

379-
- What should the Cargo configuration table and dependency key be called? This
380-
RFC calls the configuration table `base-paths` to be explicit that it is
381-
dealing with paths (as `base` would be ambiguous) but calls the key `base` to
382-
keep it concise.
383-
- Is there other reasonable behavior we could fall back to if a `base`
384-
is specified for a dependency, but no base by that name exists in the
385-
current Cargo configuration? This RFC suggests that this should be an
386-
error, but perhaps there is a reasonable thing to try _first_ prior to
387-
yielding an error.
388-
389384
# Future possibilities
390385
[future-possibilities]: #future-possibilities
391386

392-
It seems reasonable to extend `base` to `git` dependencies, with
393-
something like:
387+
## Path bases relative to other path bases
388+
389+
We could allow defining a path base relative to another path base:
390+
391+
```toml
392+
[path-bases]
393+
base1 = "/dev/me"
394+
base2 = { base = "base1", path = "some_subdir" } # /dev/me/some_subdir
395+
```
396+
397+
## Path dependency with just a base
398+
399+
We could allow defining a path dependency with *just* `base`, making
400+
`cratename = { base = "thebase" }` equivalent to
401+
`cratename = { base = "thebase", path = "cratename" }`. This would simplify many
402+
common cases, where crates appear within the base in a directory named for the
403+
crate.
404+
405+
## Git dependencies
406+
407+
It seems reasonable to extend path bases to `git` dependencies, with something
408+
like:
394409

395410
```toml
396-
[base-paths]
411+
[path-bases]
397412
gh = "https://github.com/jonhoo"
398413
```
399414

@@ -402,7 +417,9 @@ gh = "https://github.com/jonhoo"
402417
foo = { git = "foo.git", base = "gh" }
403418
```
404419

405-
However, this may get complicated if someone specifies `git`, `path`,
406-
_and_ `base`.
420+
However, this may get complicated if someone specifies `git`, `path`, _and_
421+
`base`.
422+
423+
## Support patches
407424

408-
It may also be useful to be able to use `base` for `patch` and `path`.
425+
It may also be useful to be able to use path bases in `patch` and `path` tables.

0 commit comments

Comments
 (0)