Skip to content

Commit 0547637

Browse files
authored
panic when image does not exist, warnings/errors have been emitted and running on CI or with CROSS_NO_WARNINGS=1 (#661)
2 parents 6aada9d + fd3b8b8 commit 0547637

File tree

9 files changed

+56
-11
lines changed

9 files changed

+56
-11
lines changed

.changes/661.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"description": "When cross has given a warning, it will now quit instead of continuing with `cargo` when run in CI or with `CROSS_NO_WARNINGS=1`",
3+
"breaking": true,
4+
"type": "changed"
5+
}

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ walkdir = { version = "2.3.2", optional = true }
4848
tempfile = "3.3.0"
4949
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
5050
semver = "1.0.16"
51+
is_ci = "1.1.1"
5152

5253
[target.'cfg(not(windows))'.dependencies]
5354
nix = { version = "0.26.2", default-features = false, features = ["user"] }

src/bin/cross.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn main() -> cross::Result<()> {
2121
let mut msg_info = shell::MessageInfo::create(args.verbose, args.quiet, args.color.as_deref())?;
2222
let status = match cross::run(args, target_list, &mut msg_info)? {
2323
Some(status) => status,
24-
None => {
24+
None if !msg_info.should_fail() => {
2525
// if we fallback to the host cargo, use the same invocation that was made to cross
2626
let argv: Vec<String> = env::args().skip(1).collect();
2727
msg_info.note("Falling back to `cargo` on the host.")?;
@@ -42,6 +42,11 @@ pub fn main() -> cross::Result<()> {
4242
_ => cargo::run(&argv, &mut msg_info)?,
4343
}
4444
}
45+
None => {
46+
msg_info.error("Errors encountered before cross compilation, aborting.")?;
47+
msg_info.note("Disable this with `CROSS_NO_WARNINGS=0`")?;
48+
std::process::exit(1);
49+
}
4550
};
4651
let code = status
4752
.code()

src/docker/local.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn run(
3131
paths: DockerPaths,
3232
args: &[String],
3333
msg_info: &mut MessageInfo,
34-
) -> Result<ExitStatus> {
34+
) -> Result<Option<ExitStatus>> {
3535
let engine = &options.engine;
3636
let toolchain_dirs = paths.directories.toolchain_directories();
3737
let package_dirs = paths.directories.package_directories();
@@ -147,6 +147,9 @@ pub(crate) fn run(
147147
}
148148

149149
ChildContainer::create(engine.clone(), container_id)?;
150+
if msg_info.should_fail() {
151+
return Ok(None);
152+
}
150153
let status = docker
151154
.arg(&image_name)
152155
.add_build_command(toolchain_dirs, &cmd)
@@ -162,5 +165,5 @@ pub(crate) fn run(
162165
ChildContainer::exit_static();
163166
}
164167

165-
status
168+
status.map(Some)
166169
}

src/docker/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ pub fn image_name(target: &str, sub: Option<&str>, repository: &str, tag: &str)
4141
}
4242
}
4343

44+
// TODO: The Option here in the result should be removed and Result::Error replaced with a enum to properly signal error
45+
46+
// Ok(None) means that the command failed, due to a warning or error, when `msg_info.should_fail() == true`
4447
pub fn run(
4548
options: DockerOptions,
4649
paths: DockerPaths,
4750
args: &[String],
4851
subcommand: Option<crate::Subcommand>,
4952
msg_info: &mut MessageInfo,
50-
) -> Result<ExitStatus> {
53+
) -> Result<Option<ExitStatus>> {
5154
if cfg!(target_os = "windows") && options.in_docker() {
5255
msg_info.fatal(
5356
"running cross insider a container running windows is currently unsupported",

src/docker/remote.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ pub(crate) fn run(
670670
args: &[String],
671671
subcommand: Option<crate::Subcommand>,
672672
msg_info: &mut MessageInfo,
673-
) -> Result<ExitStatus> {
673+
) -> Result<Option<ExitStatus>> {
674674
let engine = &options.engine;
675675
let target = &options.target;
676676
let toolchain_dirs = paths.directories.toolchain_directories();
@@ -897,6 +897,10 @@ pub(crate) fn run(
897897

898898
let mut cmd = options.command_variant.safe_command();
899899

900+
if msg_info.should_fail() {
901+
return Ok(None);
902+
}
903+
900904
if !options.command_variant.is_shell() {
901905
// `clean` doesn't handle symlinks: it will just unlink the target
902906
// directory, so we should just substitute it our target directory
@@ -1010,5 +1014,5 @@ symlink_recurse \"${{prefix}}\"
10101014

10111015
ChildContainer::finish_static(is_tty, msg_info);
10121016

1013-
status
1017+
status.map(Some)
10141018
}

src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,21 +624,31 @@ pub fn run(
624624
false,
625625
);
626626

627+
if msg_info.should_fail() {
628+
return Ok(None);
629+
}
630+
627631
install_interpreter_if_needed(
628632
&args,
629633
host_version_meta,
630634
&target,
631635
&options,
632636
msg_info,
633637
)?;
634-
let status = docker::run(
638+
let status = if let Some(status) = docker::run(
635639
options,
636640
paths,
637641
&filtered_args,
638642
args.subcommand.clone(),
639643
msg_info,
640644
)
641-
.wrap_err("could not run container")?;
645+
.wrap_err("could not run container")?
646+
{
647+
status
648+
} else {
649+
return Ok(None);
650+
};
651+
642652
let needs_host = args.subcommand.map_or(false, |sc| sc.needs_host(is_remote));
643653
if !status.success() {
644654
warn_on_failure(&target, &toolchain, msg_info)?;
@@ -857,19 +867,19 @@ pub(crate) fn warn_host_version_mismatch(
857867
);
858868
if versions.is_lt() || (versions.is_eq() && dates.is_lt()) {
859869
if cfg!(not(test)) {
860-
msg_info.warn(format_args!("using older {rustc_warning}.\n > Update with `rustup update --force-non-host {toolchain}`"))?;
870+
msg_info.info(format_args!("using older {rustc_warning}.\n > Update with `rustup update --force-non-host {toolchain}`"))?;
861871
}
862872
return Ok(VersionMatch::OlderTarget);
863873
} else if versions.is_gt() || (versions.is_eq() && dates.is_gt()) {
864874
if cfg!(not(test)) {
865-
msg_info.warn(format_args!(
875+
msg_info.info(format_args!(
866876
"using newer {rustc_warning}.\n > Update with `rustup update`"
867877
))?;
868878
}
869879
return Ok(VersionMatch::NewerTarget);
870880
} else {
871881
if cfg!(not(test)) {
872-
msg_info.warn(format_args!("using {rustc_warning}."))?;
882+
msg_info.info(format_args!("using {rustc_warning}."))?;
873883
}
874884
return Ok(VersionMatch::Different);
875885
}

src/shell.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ pub struct MessageInfo {
140140
pub stdout_needs_erase: bool,
141141
pub stderr_needs_erase: bool,
142142
pub cross_debug: bool,
143+
pub has_warned: bool,
143144
}
144145

145146
impl MessageInfo {
@@ -153,6 +154,7 @@ impl MessageInfo {
153154
.as_deref()
154155
.map(bool_from_envvar)
155156
.unwrap_or_default(),
157+
has_warned: false,
156158
}
157159
}
158160

@@ -231,13 +233,15 @@ impl MessageInfo {
231233
/// prints a red 'error' message.
232234
#[track_caller]
233235
pub fn error<T: fmt::Display>(&mut self, message: T) -> Result<()> {
236+
self.has_warned = true;
234237
self.stderr_check_erase()?;
235238
status!(@stderr cross_prefix!("error"), Some(&message), red, self)
236239
}
237240

238241
/// prints an amber 'warning' message.
239242
#[track_caller]
240243
pub fn warn<T: fmt::Display>(&mut self, message: T) -> Result<()> {
244+
self.has_warned = true;
241245
match self.verbosity {
242246
Verbosity::Quiet => Ok(()),
243247
_ => status!(@stderr
@@ -371,6 +375,15 @@ impl MessageInfo {
371375

372376
Ok(())
373377
}
378+
379+
/// Returns true if we've previously warned or errored, and we're in CI or `CROSS_NO_WARNINGS` has been set.
380+
///
381+
/// This is used so that unexpected warnings and errors cause ci to fail.
382+
pub fn should_fail(&self) -> bool {
383+
// FIXME: store env var
384+
env::var("CROSS_NO_WARNINGS").map_or_else(|_| is_ci::cached(), |env| bool_from_envvar(&env))
385+
&& self.has_warned
386+
}
374387
}
375388

376389
impl Default for MessageInfo {

0 commit comments

Comments
 (0)