Skip to content

Commit dc0f6f9

Browse files
authored
Handle cancelled and errored runs better (#1423)
1 parent 3763521 commit dc0f6f9

File tree

3 files changed

+63
-29
lines changed

3 files changed

+63
-29
lines changed

src/cli/mod.rs

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -90,37 +90,65 @@ impl CommandExecute for NixInstallerCli {
9090
where
9191
T: crate::feedback::Feedback,
9292
{
93-
match self.subcommand {
94-
NixInstallerSubcommand::Plan(plan) => plan.execute(feedback).await,
95-
NixInstallerSubcommand::SelfTest(self_test) => self_test.execute(feedback).await,
96-
NixInstallerSubcommand::Install(install) => {
97-
let ret = install.execute(feedback.clone()).await;
98-
99-
if matches!(
100-
target_lexicon::OperatingSystem::host(),
101-
target_lexicon::OperatingSystem::MacOSX { .. }
102-
| target_lexicon::OperatingSystem::Darwin
103-
) {
104-
#[allow(clippy::collapsible_if)]
105-
if ret.is_err() || ret.as_ref().is_ok_and(|code| code == &ExitCode::FAILURE) {
106-
let msg = feedback
107-
.get_feature_ptr_payload::<String>("dni-det-msg-fail-pkg-ptr")
108-
.await
109-
.unwrap_or(FAIL_PKG_SUGGEST.into());
110-
tracing::warn!("{}\n", msg.trim());
111-
112-
return Ok(ExitCode::FAILURE);
113-
}
114-
}
93+
let feedback_clone = feedback.clone();
11594

116-
ret
117-
},
118-
NixInstallerSubcommand::Repair(repair) => repair.execute(feedback).await,
119-
NixInstallerSubcommand::Uninstall(revert) => revert.execute(feedback).await,
95+
let is_install_subcommand = matches!(self.subcommand, NixInstallerSubcommand::Install(_));
96+
97+
let ret = match self.subcommand {
98+
NixInstallerSubcommand::Plan(plan) => plan.execute(feedback_clone).await,
99+
NixInstallerSubcommand::SelfTest(self_test) => self_test.execute(feedback_clone).await,
100+
NixInstallerSubcommand::Install(install) => install.execute(feedback_clone).await,
101+
NixInstallerSubcommand::Repair(repair) => repair.execute(feedback_clone).await,
102+
NixInstallerSubcommand::Uninstall(revert) => revert.execute(feedback_clone).await,
120103
NixInstallerSubcommand::SplitReceipt(split_receipt) => {
121-
split_receipt.execute(feedback).await
104+
split_receipt.execute(feedback_clone).await
122105
},
106+
};
107+
108+
let maybe_cancelled = ret.as_ref().err().and_then(|err| {
109+
err.root_cause()
110+
.downcast_ref::<crate::NixInstallerError>()
111+
.and_then(|err| {
112+
if matches!(err, crate::NixInstallerError::Cancelled) {
113+
return Some(err);
114+
}
115+
None
116+
})
117+
});
118+
119+
if let Some(cancelled) = maybe_cancelled {
120+
eprintln!("{}", cancelled.red());
121+
return Ok(ExitCode::FAILURE);
122+
}
123+
124+
let is_macos = matches!(
125+
target_lexicon::OperatingSystem::host(),
126+
target_lexicon::OperatingSystem::MacOSX { .. }
127+
| target_lexicon::OperatingSystem::Darwin
128+
);
129+
130+
if is_install_subcommand && is_macos {
131+
let is_ok_but_failed = ret.as_ref().is_ok_and(|code| code == &ExitCode::FAILURE);
132+
let is_error = ret.as_ref().is_err();
133+
134+
if is_error || is_ok_but_failed {
135+
let msg = feedback
136+
.get_feature_ptr_payload::<String>("dni-det-msg-fail-pkg-ptr")
137+
.await
138+
.unwrap_or(FAIL_PKG_SUGGEST.into());
139+
140+
// NOTE: If the error bubbled up, print it before we log the pkg suggestion
141+
if let Err(ref err) = ret {
142+
eprintln!("{err:?}\n");
143+
}
144+
145+
tracing::warn!("{}\n", msg.trim());
146+
147+
return Ok(ExitCode::FAILURE);
148+
}
123149
}
150+
151+
ret
124152
}
125153
}
126154

src/cli/subcommand/install.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,13 @@ impl CommandExecute for Install {
306306
was_expected = true;
307307
eprintln!("{}", expected.red())
308308
}
309-
if !was_expected {
309+
310+
let was_cancelled = matches!(err, NixInstallerError::Cancelled);
311+
if was_cancelled {
312+
eprintln!("{}", err.red());
313+
}
314+
315+
if !was_expected && !was_cancelled {
310316
let error = eyre!(err).wrap_err("Install failure");
311317
tracing::error!("{:?}", error);
312318
};

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl HasExpectedErrors for NixInstallerError {
106106
NixInstallerError::RecordingReceipt(_, _) => None,
107107
NixInstallerError::CopyingSelf(_) => None,
108108
NixInstallerError::SerializingReceipt(_) => None,
109-
this @ NixInstallerError::Cancelled => Some(Box::new(this)),
109+
NixInstallerError::Cancelled => None,
110110
NixInstallerError::SemVer(_) => None,
111111
NixInstallerError::Planner(planner_error) => planner_error.expected(),
112112
NixInstallerError::InstallSettings(_) => None,

0 commit comments

Comments
 (0)