Skip to content

Commit 38f52ea

Browse files
committed
Auto merge of #3621 - matklad:fix-job-leakage, r=alexcrichton
Don't leak job This fixes that problem with diverging `handle_error`. Now the return types of all subcommands match, and we can return `CliResult` up to main.
2 parents adaba36 + ef04357 commit 38f52ea

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/bin/cargo.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn main() {
7373
Ok(cfg) => cfg,
7474
Err(e) => {
7575
let mut shell = cargo::shell(Verbosity::Verbose, ColorConfig::Auto);
76-
cargo::handle_cli_error(e.into(), &mut shell)
76+
cargo::exit_with_error(e.into(), &mut shell)
7777
}
7878
};
7979

@@ -88,7 +88,7 @@ fn main() {
8888
})();
8989

9090
match result {
91-
Err(e) => cargo::handle_cli_error(e, &mut *config.shell()),
91+
Err(e) => cargo::exit_with_error(e, &mut *config.shell()),
9292
Ok(()) => {},
9393
}
9494
}
@@ -185,10 +185,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult {
185185
"" | "help" if flags.arg_args.is_empty() => {
186186
config.shell().set_verbosity(Verbosity::Verbose);
187187
let args = &["cargo".to_string(), "-h".to_string()];
188-
let r = cargo::call_main_without_stdin(execute, config, USAGE, args,
189-
false);
190-
cargo::process_executed(r, &mut config.shell());
191-
return Ok(())
188+
return cargo::call_main_without_stdin(execute, config, USAGE, args, false);
192189
}
193190

194191
// For `cargo help -h` and `cargo help --help`, print out the help
@@ -219,8 +216,8 @@ fn execute(flags: Flags, config: &Config) -> CliResult {
219216
}
220217
};
221218

222-
if try_execute(&config, &args) {
223-
return Ok(())
219+
if let Some(r) = try_execute_builtin_command(&config, &args) {
220+
return r;
224221
}
225222

226223
let alias_list = aliased_command(&config, &args[1])?;
@@ -231,33 +228,32 @@ fn execute(flags: Flags, config: &Config) -> CliResult {
231228
.chain(args.iter().skip(2))
232229
.map(|s| s.to_string())
233230
.collect::<Vec<_>>();
234-
if try_execute(&config, &chain) {
235-
return Ok(())
231+
if let Some(r) = try_execute_builtin_command(&config, &chain) {
232+
return r;
236233
} else {
237234
chain
238235
}
239236
}
240237
None => args,
241238
};
242-
execute_subcommand(config, &args[1], &args)?;
243-
Ok(())
239+
240+
execute_external_subcommand(config, &args[1], &args)
244241
}
245242

246-
fn try_execute(config: &Config, args: &[String]) -> bool {
243+
fn try_execute_builtin_command(config: &Config, args: &[String]) -> Option<CliResult> {
247244
macro_rules! cmd {
248245
($name:ident) => (if args[1] == stringify!($name).replace("_", "-") {
249246
config.shell().set_verbosity(Verbosity::Verbose);
250247
let r = cargo::call_main_without_stdin($name::execute, config,
251248
$name::USAGE,
252249
&args,
253250
false);
254-
cargo::process_executed(r, &mut config.shell());
255-
return true
251+
return Some(r);
256252
})
257253
}
258254
each_subcommand!(cmd);
259255

260-
return false
256+
None
261257
}
262258

263259
fn aliased_command(config: &Config, command: &String) -> CargoResult<Option<Vec<String>>> {
@@ -295,9 +291,9 @@ fn find_closest(config: &Config, cmd: &str) -> Option<String> {
295291
filtered.get(0).map(|slot| slot.1.clone())
296292
}
297293

298-
fn execute_subcommand(config: &Config,
299-
cmd: &str,
300-
args: &[String]) -> CliResult {
294+
fn execute_external_subcommand(config: &Config,
295+
cmd: &str,
296+
args: &[String]) -> CliResult {
301297
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
302298
let path = search_directories(config)
303299
.iter()

src/cargo/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ pub fn call_main_without_stdin<Flags: Decodable>(
125125
exec(flags, config)
126126
}
127127

128-
// This will diverge if `result` is an `Err` and return otherwise.
129-
pub fn process_executed(result: CliResult, shell: &mut MultiShell)
130-
{
131-
match result {
132-
Err(e) => handle_cli_error(e, shell),
133-
Ok(()) => {}
134-
}
135-
}
136-
137128
pub fn print_json<T: Encodable>(obj: &T) {
138129
let encoded = json::encode(&obj).unwrap();
139130
println!("{}", encoded);
@@ -184,8 +175,8 @@ pub fn shell(verbosity: Verbosity, color_config: ColorConfig) -> MultiShell {
184175
}
185176
}
186177

187-
pub fn handle_cli_error(err: CliError, shell: &mut MultiShell) -> ! {
188-
debug!("handle_cli_error; err={:?}", err);
178+
pub fn exit_with_error(err: CliError, shell: &mut MultiShell) -> ! {
179+
debug!("exit_with_error; err={:?}", err);
189180

190181
let CliError { error, exit_code, unknown } = err;
191182
// exit_code == 0 is non-fatal error, e.g. docopt version info

0 commit comments

Comments
 (0)