|
1 | 1 | use crate::command_prelude::*;
|
2 | 2 | use crate::util::restricted_names::is_glob_pattern;
|
3 |
| -use cargo::core::Verbosity; |
4 |
| -use cargo::ops::{self, CompileFilter, Packages}; |
| 3 | +use cargo::core::{Verbosity, Workspace}; |
| 4 | +use cargo::ops::{self, CompileFilter, CompileOptions, Packages}; |
| 5 | +use cargo::util::CargoResult; |
5 | 6 | use cargo_util::ProcessError;
|
6 | 7 |
|
7 | 8 | pub fn cli() -> App {
|
@@ -52,12 +53,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
|
52 | 53 | }
|
53 | 54 |
|
54 | 55 | if !args.is_present("example") && !args.is_present("bin") {
|
55 |
| - let default_runs: Vec<_> = compile_opts |
56 |
| - .spec |
57 |
| - .get_packages(&ws)? |
58 |
| - .iter() |
59 |
| - .filter_map(|pkg| pkg.manifest().default_run()) |
60 |
| - .collect(); |
| 56 | + let default_runs = get_default_runs(&ws, &compile_opts)?; |
61 | 57 | if default_runs.len() == 1 {
|
62 | 58 | compile_opts.filter = CompileFilter::from_raw_arguments(
|
63 | 59 | false,
|
@@ -106,3 +102,41 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
|
106 | 102 | }
|
107 | 103 | })
|
108 | 104 | }
|
| 105 | + |
| 106 | +fn get_default_runs(ws: &Workspace<'_>, compile_opts: &CompileOptions) -> CargoResult<Vec<String>> { |
| 107 | + const ERROR: &'static str = |
| 108 | + "`cargo run` cannot find pkgid either in the workspace or among the workspace dependencies"; |
| 109 | + |
| 110 | + let workspace_packages = compile_opts.spec.get_packages(ws); |
| 111 | + |
| 112 | + let default_runs = if let Ok(packages) = workspace_packages { |
| 113 | + // Package is workspace member |
| 114 | + packages |
| 115 | + .iter() |
| 116 | + .filter_map(|pkg| pkg.manifest().default_run()) |
| 117 | + .map(str::to_owned) |
| 118 | + .collect() |
| 119 | + } else if let Packages::Packages(ref pkg_names) = compile_opts.spec { |
| 120 | + // Search dependencies |
| 121 | + let (package_set, resolver) = ops::resolve_ws(ws)?; |
| 122 | + let deps: Vec<_> = pkg_names |
| 123 | + .iter() |
| 124 | + .flat_map(|name| resolver.query(name)) |
| 125 | + .collect(); |
| 126 | + |
| 127 | + if deps.is_empty() { |
| 128 | + anyhow::bail!(ERROR); |
| 129 | + } |
| 130 | + |
| 131 | + package_set |
| 132 | + .get_many(deps)? |
| 133 | + .iter() |
| 134 | + .filter_map(|pkg| pkg.manifest().default_run()) |
| 135 | + .map(str::to_owned) |
| 136 | + .collect() |
| 137 | + } else { |
| 138 | + anyhow::bail!(ERROR); |
| 139 | + }; |
| 140 | + |
| 141 | + Ok(default_runs) |
| 142 | +} |
0 commit comments