66# Summary
77[ summary ] : #summary
88
9- Introduce shared base directories in Cargo configuration files that in
10- turn enable base-relative ` path ` dependencies.
9+ Introduce a table of path "bases" in Cargo configuration files that can be used
10+ to prefix the path of ` path ` dependencies and ` patch ` entries.
11+
12+ This feature will not support declaring path bases in manifest files to avoid
13+ additional design complexity, though this may be added in the future.
1114
1215# Motivation
1316[ motivation ] : #motivation
@@ -30,14 +33,13 @@ relative paths (which makes refactoring and moving sub-projects very difficult)
3033and don't work at all if the mono-repo requires publishing and consuming from an
3134intermediate directory (as this may very per host, or per target being built).
3235
33- This RFC proposes a mechanism to specify path bases 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.
36+ This RFC proposes a mechanism to specify path bases in ` config.toml ` files which
37+ can be used to prepend ` path ` dependencies. This allows mono-repos to specify
38+ dependencies relative to their root directory, which allows the consuming
39+ project to be moved freely (no relative paths to update) and a simple
40+ find-and-replace to handle a producing project being moved. Additionally, a
41+ host-specific or target-specific intermediate directory may be specified as a
42+ ` base ` , allowing code to be consumed from there using ` path ` dependencies.
4143
4244### Example
4345
@@ -105,7 +107,7 @@ root directory. Instead of repeating the same path fragment many times in their
105107` Cargo.toml ` , they can instead specify it once in a ` config.toml ` as a path
106108base, then use that path base in each of their ` path ` dependencies.
107109
108- Cargo can also provide built-in base paths, for example ` workspace ` to point to
110+ Cargo will also provide built-in base paths, for example ` workspace ` to point to
109111the root directory of the workspace. This allows workspace members to reference
110112each other without first needing to ` ../ ` their way back to the workspace root.
111113
@@ -141,6 +143,10 @@ Like with other path dependencies, keep in mind that both the base _and_
141143the path must exist on any other host where you want to use the same
142144` Cargo.toml ` to build your project.
143145
146+ You can also use ` base ` along with ` path ` when specifying a ` [patch] ` .
147+ Specifying a ` path ` and ` base ` on a ` [patch] ` is equivalent to specifying just a
148+ ` path ` containing the full path including the prepended base.
149+
144150# Reference-level explanation
145151[ reference-level-explanation ] : #reference-level-explanation
146152
@@ -152,10 +158,11 @@ A `path` dependency may optionally specify a base by setting the `base` key to
152158the name of a path base from the ` [path-bases] ` table in either the
153159[ configuration] ( https://doc.rust-lang.org/cargo/reference/config.html#path-bases )
154160or one of the [ built-in path bases] ( #built-in-path-bases ) . The value of that
155- path base is prepended to the ` path ` value to produce the actual location where
156- Cargo will look for the dependency.
161+ path base is prepended to the ` path ` value (along with a path separator if
162+ necessary) to produce the actual location where Cargo will look for the
163+ dependency.
157164
158- For example, if the Cargo.toml contains:
165+ For example, if the ` Cargo.toml ` contains:
159166
160167``` toml
161168[dependencies ]
@@ -188,8 +195,8 @@ Cargo provides implicit path bases that can be used without the need to specify
188195them in a ` [path-bases] ` table.
189196
190197* ` workspace ` - If a project is [ a workspace or workspace member] ( https://doc.rust-lang.org/cargo/reference/workspaces.html )
191- then this path base is defined as the parent directory of the root Cargo.toml of
192- the workspace.
198+ then this path base is defined as the parent directory of the root ` Cargo.toml `
199+ of the workspace.
193200
194201If a built-in path base name is also declared in the configuration, then Cargo
195202will prefer the value in the configuration. The allows Cargo to add new built-in
@@ -209,6 +216,30 @@ prepend the locations of `path` dependencies. See the
209216[ specifying dependencies] ( https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#path-bases )
210217documentation for more information.
211218
219+ ## cargo add
220+
221+ ### Synopsis
222+
223+ ` cargo add ` * [ options] * ` --path ` * path* [ ` --base ` * base* ]
224+
225+ ### Options
226+
227+ #### Source options
228+
229+ ` --base ` * base*
230+
231+ The [ path base] ( https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#path-bases )
232+ to use when adding from a local crate.
233+
234+ ## Workspaces
235+
236+ Path bases can be used in a workspace's ` [dependencies] ` table.
237+
238+ If a member is inheriting a dependency (i.e., using ` workspace = true ` ) then the
239+ ` base ` key cannot also be specified for that dependency in the member manifest.
240+ That is, the member will use the ` path ` dependency as specified in the workspace
241+ manifest and has no ability to override the base path being used (if any).
242+
212243# Drawbacks
213244[ drawbacks ] : #drawbacks
214245
@@ -382,9 +413,35 @@ could help.
382413# Unresolved questions
383414[ unresolved-questions ] : #unresolved-questions
384415
416+ * What exact names we should use for the table (` path-bases ` ) and field names
417+ (` base ` )?
418+ * What other built-in base paths could be useful?
419+ * ` package ` or ` current-dir ` for the directory of the current project?
420+ * ` home ` or ` user_home ` for the user's home directory?
421+ * ` sysroot ` for the current rustc sysroot?
422+
385423# Future possibilities
386424[ future-possibilities ] : #future-possibilities
387425
426+ ## Add support for declaring path bases in the manifest
427+
428+ As mentioned [ above] ( #support-for-declaring-path-bases-in-the-manifest ) ,
429+ declaring path bases is only supported in the configuration.
430+
431+ Support could be added to declare path bases in the manifest, but the following
432+ design questions need to be answered:
433+
434+ * Is ` [path-bases] ` a package or a workspace field?
435+ * If it is a package field, would it support workspace inheritance? Or would we
436+ introduce a new mechanism (e.g., one version of the RFC introduced a "search
437+ order" such that Cargo would search for a path base in the package manifest,
438+ then the workspace manifest, then the configuration and finally the built-in
439+ list).
440+ * Would a relative path base in the workspace manifest be relative to that
441+ manifest, or to the package that uses it?
442+ * If using inheritance, should path bases be implicitly or explicitly inherited?
443+ (e.g., requiring ` [base-paths] workspace = true ` )
444+
388445## Path bases relative to other path bases
389446
390447We could allow defining a path base relative to another path base:
@@ -420,7 +477,3 @@ foo = { git = "foo.git", base = "gh" }
420477
421478However, this may get complicated if someone specifies ` git ` , ` path ` , _ and_
422479` base ` .
423-
424- ## Support patches
425-
426- It may also be useful to be able to use path bases in ` patch ` and ` path ` tables.
0 commit comments