forked from rust-lang/cargo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkspace.rs
559 lines (507 loc) · 21 KB
/
workspace.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::collections::hash_map::{HashMap, Entry};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::slice;
use core::{Package, VirtualManifest, EitherManifest, SourceId};
use core::{PackageIdSpec, Dependency, Profile, Profiles};
use ops;
use util::{Config, CargoResult, Filesystem, human};
use util::paths;
/// The core abstraction in Cargo for working with a workspace of crates.
///
/// A workspace is often created very early on and then threaded through all
/// other functions. It's typically through this object that the current
/// package is loaded and/or learned about.
pub struct Workspace<'cfg> {
config: &'cfg Config,
// This path is a path to where the current cargo subcommand was invoked
// from. That is, this is the `--manifest-path` argument to Cargo, and
// points to the "main crate" that we're going to worry about.
current_manifest: PathBuf,
// A list of packages found in this workspace. Always includes at least the
// package mentioned by `current_manifest`.
packages: Packages<'cfg>,
// If this workspace includes more than one crate, this points to the root
// of the workspace. This is `None` in the case that `[workspace]` is
// missing, `package.workspace` is missing, and no `Cargo.toml` above
// `current_manifest` was found on the filesystem with `[workspace]`.
root_manifest: Option<PathBuf>,
// Shared target directory for all the packages of this workspace.
// `None` if the default path of `root/target` should be used.
target_dir: Option<Filesystem>,
// List of members in this workspace with a listing of all their manifest
// paths. The packages themselves can be looked up through the `packages`
// set above.
members: Vec<PathBuf>,
// True, if this is a temporary workspace created for the purposes of
// cargo install or cargo package.
is_ephemeral: bool,
}
// Separate structure for tracking loaded packages (to avoid loading anything
// twice), and this is separate to help appease the borrow checker.
struct Packages<'cfg> {
config: &'cfg Config,
packages: HashMap<PathBuf, MaybePackage>,
}
enum MaybePackage {
Package(Package),
Virtual(VirtualManifest),
}
/// Configuration of a workspace in a manifest.
#[derive(Debug, Clone)]
pub enum WorkspaceConfig {
/// Indicates that `[workspace]` was present and the members were
/// optionally specified as well.
Root { members: Option<Vec<String>> },
/// Indicates that `[workspace]` was present and the `root` field is the
/// optional value of `package.workspace`, if present.
Member { root: Option<String> },
}
/// An iterator over the member packages of a workspace, returned by
/// `Workspace::members`
pub struct Members<'a, 'cfg: 'a> {
ws: &'a Workspace<'cfg>,
iter: slice::Iter<'a, PathBuf>,
}
impl<'cfg> Workspace<'cfg> {
/// Creates a new workspace given the target manifest pointed to by
/// `manifest_path`.
///
/// This function will construct the entire workspace by determining the
/// root and all member packages. It will then validate the workspace
/// before returning it, so `Ok` is only returned for valid workspaces.
pub fn new(manifest_path: &Path, config: &'cfg Config)
-> CargoResult<Workspace<'cfg>> {
let target_dir = config.target_dir()?;
let mut ws = Workspace {
config: config,
current_manifest: manifest_path.to_path_buf(),
packages: Packages {
config: config,
packages: HashMap::new(),
},
root_manifest: None,
target_dir: target_dir,
members: Vec::new(),
is_ephemeral: false,
};
ws.root_manifest = ws.find_root(manifest_path)?;
ws.find_members()?;
ws.validate()?;
Ok(ws)
}
/// Creates a "temporary workspace" from one package which only contains
/// that package.
///
/// This constructor will not touch the filesystem and only creates an
/// in-memory workspace. That is, all configuration is ignored, it's just
/// intended for that one package.
///
/// This is currently only used in niche situations like `cargo install` or
/// `cargo package`.
pub fn ephemeral(package: Package, config: &'cfg Config, target_dir: Option<Filesystem>)
-> CargoResult<Workspace<'cfg>> {
let mut ws = Workspace {
config: config,
current_manifest: package.manifest_path().to_path_buf(),
packages: Packages {
config: config,
packages: HashMap::new(),
},
root_manifest: None,
target_dir: None,
members: Vec::new(),
is_ephemeral: true,
};
{
let key = ws.current_manifest.parent().unwrap();
let package = MaybePackage::Package(package);
ws.packages.packages.insert(key.to_path_buf(), package);
ws.target_dir = if let Some(dir) = target_dir {
Some(dir)
} else {
ws.config.target_dir()?
};
ws.members.push(ws.current_manifest.clone());
}
return Ok(ws)
}
/// Returns the current package of this workspace.
///
/// Note that this can return an error if it the current manifest is
/// actually a "virtual Cargo.toml", in which case an error is returned
/// indicating that something else should be passed.
pub fn current(&self) -> CargoResult<&Package> {
self.current_opt().ok_or_else(||
human(format!("manifest path `{}` is a virtual manifest, but this \
command requires running against an actual package in \
this workspace", self.current_manifest.display()))
)
}
pub fn current_opt(&self) -> Option<&Package> {
match *self.packages.get(&self.current_manifest) {
MaybePackage::Package(ref p) => Some(p),
MaybePackage::Virtual(..) => None
}
}
/// Returns the `Config` this workspace is associated with.
pub fn config(&self) -> &'cfg Config {
self.config
}
pub fn profiles(&self) -> &Profiles {
let root = self.root_manifest.as_ref().unwrap_or(&self.current_manifest);
match *self.packages.get(root) {
MaybePackage::Package(ref p) => p.manifest().profiles(),
MaybePackage::Virtual(ref m) => m.profiles(),
}
}
/// Returns the root path of this workspace.
///
/// That is, this returns the path of the directory containing the
/// `Cargo.toml` which is the root of this workspace.
pub fn root(&self) -> &Path {
match self.root_manifest {
Some(ref p) => p,
None => &self.current_manifest
}.parent().unwrap()
}
pub fn target_dir(&self) -> Filesystem {
self.target_dir.clone().unwrap_or_else(|| {
Filesystem::new(self.root().join("target"))
})
}
/// Returns the root [replace] section of this workspace.
///
/// This may be from a virtual crate or an actual crate.
pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
let path = match self.root_manifest {
Some(ref p) => p,
None => &self.current_manifest,
};
match *self.packages.get(path) {
MaybePackage::Package(ref p) => p.manifest().replace(),
MaybePackage::Virtual(ref v) => v.replace(),
}
}
/// Returns an iterator over all packages in this workspace
pub fn members<'a>(&'a self) -> Members<'a, 'cfg> {
Members {
ws: self,
iter: self.members.iter(),
}
}
pub fn is_ephemeral(&self) -> bool {
self.is_ephemeral
}
/// Finds the root of a workspace for the crate whose manifest is located
/// at `manifest_path`.
///
/// This will parse the `Cargo.toml` at `manifest_path` and then interpret
/// the workspace configuration, optionally walking up the filesystem
/// looking for other workspace roots.
///
/// Returns an error if `manifest_path` isn't actually a valid manifest or
/// if some other transient error happens.
fn find_root(&mut self, manifest_path: &Path)
-> CargoResult<Option<PathBuf>> {
fn read_root_pointer(member_manifest: &Path, root_link: &str) -> CargoResult<PathBuf> {
let path = member_manifest.parent().unwrap()
.join(root_link)
.join("Cargo.toml");
debug!("find_root - pointer {}", path.display());
return Ok(paths::normalize_path(&path))
};
{
let current = self.packages.load(&manifest_path)?;
match *current.workspace_config() {
WorkspaceConfig::Root { .. } => {
debug!("find_root - is root {}", manifest_path.display());
return Ok(Some(manifest_path.to_path_buf()))
}
WorkspaceConfig::Member { root: Some(ref path_to_root) } => {
return Ok(Some(read_root_pointer(manifest_path, path_to_root)?))
}
WorkspaceConfig::Member { root: None } => {}
}
}
let mut cur = manifest_path.parent().and_then(|p| p.parent());
while let Some(path) = cur {
let manifest = path.join("Cargo.toml");
debug!("find_root - trying {}", manifest.display());
if manifest.exists() {
match *self.packages.load(&manifest)?.workspace_config() {
WorkspaceConfig::Root { .. } => {
debug!("find_root - found");
return Ok(Some(manifest))
}
WorkspaceConfig::Member { root: Some(ref path_to_root) } => {
return Ok(Some(read_root_pointer(&manifest, path_to_root)?))
}
WorkspaceConfig::Member { .. } => {}
}
}
cur = path.parent();
}
Ok(None)
}
/// After the root of a workspace has been located, probes for all members
/// of a workspace.
///
/// If the `workspace.members` configuration is present, then this just
/// verifies that those are all valid packages to point to. Otherwise, this
/// will transitively follow all `path` dependencies looking for members of
/// the workspace.
fn find_members(&mut self) -> CargoResult<()> {
let root_manifest = match self.root_manifest {
Some(ref path) => path.clone(),
None => {
debug!("find_members - only me as a member");
self.members.push(self.current_manifest.clone());
return Ok(())
}
};
let members = {
let root = self.packages.load(&root_manifest)?;
match *root.workspace_config() {
WorkspaceConfig::Root { ref members } => members.clone(),
_ => bail!("root of a workspace inferred but wasn't a root: {}",
root_manifest.display()),
}
};
if let Some(list) = members {
for path in list {
let root = root_manifest.parent().unwrap();
let manifest_path = root.join(path).join("Cargo.toml");
self.find_path_deps(&manifest_path, false)?;
}
}
self.find_path_deps(&root_manifest, false)
}
fn find_path_deps(&mut self, manifest_path: &Path, is_path_dep: bool) -> CargoResult<()> {
let manifest_path = paths::normalize_path(manifest_path);
if self.members.iter().any(|p| p == &manifest_path) {
return Ok(())
}
if is_path_dep
&& !manifest_path.parent().unwrap().starts_with(self.root())
&& self.find_root(&manifest_path)? != self.root_manifest {
// If `manifest_path` is a path dependency outside of the workspace,
// don't add it, or any of its dependencies, as a members.
return Ok(())
}
debug!("find_members - {}", manifest_path.display());
self.members.push(manifest_path.clone());
let candidates = {
let pkg = match *self.packages.load(&manifest_path)? {
MaybePackage::Package(ref p) => p,
MaybePackage::Virtual(_) => return Ok(()),
};
pkg.dependencies()
.iter()
.map(|d| d.source_id())
.filter(|d| d.is_path())
.filter_map(|d| d.url().to_file_path().ok())
.map(|p| p.join("Cargo.toml"))
.collect::<Vec<_>>()
};
for candidate in candidates {
self.find_path_deps(&candidate, true)?;
}
Ok(())
}
/// Validates a workspace, ensuring that a number of invariants are upheld:
///
/// 1. A workspace only has one root.
/// 2. All workspace members agree on this one root as the root.
/// 3. The current crate is a member of this workspace.
fn validate(&mut self) -> CargoResult<()> {
if self.root_manifest.is_none() {
return Ok(())
}
let mut roots = Vec::new();
{
let mut names = BTreeMap::new();
for member in self.members.iter() {
let package = self.packages.get(member);
match *package.workspace_config() {
WorkspaceConfig::Root { .. } => {
roots.push(member.parent().unwrap().to_path_buf());
}
WorkspaceConfig::Member { .. } => {}
}
let name = match *package {
MaybePackage::Package(ref p) => p.name(),
MaybePackage::Virtual(_) => continue,
};
if let Some(prev) = names.insert(name, member) {
bail!("two packages named `{}` in this workspace:\n\
- {}\n\
- {}", name, prev.display(), member.display());
}
}
}
match roots.len() {
0 => {
bail!("`package.workspace` configuration points to a crate \
which is not configured with [workspace]: \n\
configuration at: {}\n\
points to: {}",
self.current_manifest.display(),
self.root_manifest.as_ref().unwrap().display())
}
1 => {}
_ => {
bail!("multiple workspace roots found in the same workspace:\n{}",
roots.iter()
.map(|r| format!(" {}", r.display()))
.collect::<Vec<_>>()
.join("\n"));
}
}
for member in self.members.clone() {
let root = self.find_root(&member)?;
if root == self.root_manifest {
continue
}
match root {
Some(root) => {
bail!("package `{}` is a member of the wrong workspace\n\
expected: {}\n\
actual: {}",
member.display(),
self.root_manifest.as_ref().unwrap().display(),
root.display());
}
None => {
bail!("workspace member `{}` is not hierarchically below \
the workspace root `{}`",
member.display(),
self.root_manifest.as_ref().unwrap().display());
}
}
}
if !self.members.contains(&self.current_manifest) {
let root = self.root_manifest.as_ref().unwrap();
let root_dir = root.parent().unwrap();
let current_dir = self.current_manifest.parent().unwrap();
let root_pkg = self.packages.get(root);
let members_msg = match current_dir.strip_prefix(root_dir) {
Ok(rel) => {
format!("this may be fixable by adding `{}` to the \
`workspace.members` array of the manifest \
located at: {}",
rel.display(),
root.display())
}
Err(_) => {
format!("this may be fixable by adding a member to \
the `workspace.members` array of the \
manifest located at: {}", root.display())
}
};
let extra = match *root_pkg {
MaybePackage::Virtual(_) => members_msg,
MaybePackage::Package(ref p) => {
let members = match *p.manifest().workspace_config() {
WorkspaceConfig::Root { ref members } => members,
WorkspaceConfig::Member { .. } => unreachable!(),
};
if members.is_none() {
format!("this may be fixable by ensuring that this \
crate is depended on by the workspace \
root: {}", root.display())
} else {
members_msg
}
}
};
bail!("current package believes it's in a workspace when it's not:\n\
current: {}\n\
workspace: {}\n\n{}",
self.current_manifest.display(),
root.display(),
extra);
}
if let Some(ref root_manifest) = self.root_manifest {
let default_profiles = Profiles {
release: Profile::default_release(),
dev: Profile::default_dev(),
test: Profile::default_test(),
test_deps: Profile::default_dev(),
bench: Profile::default_bench(),
bench_deps: Profile::default_release(),
doc: Profile::default_doc(),
custom_build: Profile::default_custom_build(),
check: Profile::default_check(),
doctest: Profile::default_doctest(),
};
for pkg in self.members().filter(|p| p.manifest_path() != root_manifest) {
if pkg.manifest().profiles() != &default_profiles {
let message = &format!("profiles for the non root package will be ignored, \
specify profiles at the workspace root:\n\
package: {}\n\
workspace: {}",
pkg.manifest_path().display(),
root_manifest.display());
//TODO: remove `Eq` bound from `Profiles` when the warning is removed.
self.config.shell().warn(&message)?;
}
}
}
Ok(())
}
}
impl<'cfg> Packages<'cfg> {
fn get(&self, manifest_path: &Path) -> &MaybePackage {
&self.packages[manifest_path.parent().unwrap()]
}
fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
let key = manifest_path.parent().unwrap();
match self.packages.entry(key.to_path_buf()) {
Entry::Occupied(e) => Ok(e.into_mut()),
Entry::Vacant(v) => {
let source_id = SourceId::for_path(key)?;
let pair = ops::read_manifest(&manifest_path, &source_id,
self.config)?;
let (manifest, _nested_paths) = pair;
Ok(v.insert(match manifest {
EitherManifest::Real(manifest) => {
MaybePackage::Package(Package::new(manifest,
manifest_path))
}
EitherManifest::Virtual(v) => {
MaybePackage::Virtual(v)
}
}))
}
}
}
}
impl<'a, 'cfg> Members<'a, 'cfg> {
pub fn is_empty(self) -> bool {
self.count() == 0
}
}
impl<'a, 'cfg> Iterator for Members<'a, 'cfg> {
type Item = &'a Package;
fn next(&mut self) -> Option<&'a Package> {
loop {
let next = self.iter.next().map(|path| {
self.ws.packages.get(path)
});
match next {
Some(&MaybePackage::Package(ref p)) => return Some(p),
Some(&MaybePackage::Virtual(_)) => {}
None => return None,
}
}
}
}
impl MaybePackage {
fn workspace_config(&self) -> &WorkspaceConfig {
match *self {
MaybePackage::Virtual(ref v) => v.workspace_config(),
MaybePackage::Package(ref v) => v.manifest().workspace_config(),
}
}
}