Skip to content

Commit 55c21b9

Browse files
Merge #1015
1015: Improve error diagnostics and other changes r=Alexhuszagh a=Emilgardis - don't hide hostname querying in verbose - output cross images as json - associate `CROSS_DEBUG` with `MessageInfo` <img width="490" alt="image" src="https://user-images.githubusercontent.com/1502855/195405356-1f673155-6be1-4f9f-9cc7-2cbf50604345.png"> - track caller when doing debug or error prints from `MessageInfo` and print the line it comes from Co-authored-by: Emil Gardström <emil.gardstrom@gmail.com>
2 parents cf897a8 + 37f0b8e commit 55c21b9

File tree

10 files changed

+175
-69
lines changed

10 files changed

+175
-69
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Dockerfile.* linguist-language=Dockerfile

ci/test-remote.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ cross_test_cpp() {
4545

4646
pushd "${td}"
4747
retry cargo fetch
48-
"${CROSS}" run --target "${TARGET}"
48+
"${CROSS}" run --target "${TARGET}" | grep "Hello, world!"
49+
sed -i 's/Hello, world/Hello, test/g' hellopp.cc
50+
"${CROSS}" run --target "${TARGET}" | grep "Hello, test!"
4951
popd
5052

5153
rm -rf "${td}"

src/bin/commands/containers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ pub fn create_persistent_volume(
396396
if let Some(channel) = channel {
397397
toolchain.channel = channel.channel.clone();
398398
};
399-
let mount_finder = docker::MountFinder::create(engine)?;
399+
let mount_finder = docker::MountFinder::create(engine, msg_info)?;
400400
let dirs = docker::ToolchainDirectories::assemble(&mount_finder, toolchain.clone())?;
401401
let container_id = dirs.unique_container_identifier(&toolchain.host().target)?;
402402
let volume_id = dirs.unique_toolchain_identifier()?;
@@ -464,7 +464,7 @@ pub fn remove_persistent_volume(
464464
if let Some(channel) = channel {
465465
toolchain.channel = channel.channel.clone();
466466
};
467-
let mount_finder = docker::MountFinder::create(engine)?;
467+
let mount_finder = docker::MountFinder::create(engine, msg_info)?;
468468
let dirs = docker::ToolchainDirectories::assemble(&mount_finder, toolchain)?;
469469
let volume_id = dirs.unique_toolchain_identifier()?;
470470
let volume = docker::DockerVolume::new(engine, &volume_id);

src/bin/commands/images.rs

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::{BTreeMap, BTreeSet};
22

3+
use clap::builder::PossibleValue;
34
use clap::{Args, Subcommand};
45
use cross::docker::{self, CROSS_CUSTOM_DOCKERFILE_IMAGE_PREFIX};
56
use cross::shell::MessageInfo;
@@ -26,6 +27,9 @@ pub struct ListImages {
2627
/// Container engine (such as docker or podman).
2728
#[clap(long)]
2829
pub engine: Option<String>,
30+
/// Output format
31+
#[clap(long, default_value = "human")]
32+
pub format: OutputFormat,
2933
/// Only list images for specific target(s). By default, list all targets.
3034
pub targets: Vec<String>,
3135
}
@@ -36,6 +40,25 @@ impl ListImages {
3640
}
3741
}
3842

43+
#[derive(Clone, Debug)]
44+
pub enum OutputFormat {
45+
Human,
46+
Json,
47+
}
48+
49+
impl clap::ValueEnum for OutputFormat {
50+
fn value_variants<'a>() -> &'a [Self] {
51+
&[Self::Human, Self::Json]
52+
}
53+
54+
fn to_possible_value(&self) -> Option<PossibleValue> {
55+
match self {
56+
OutputFormat::Human => Some(PossibleValue::new("human")),
57+
OutputFormat::Json => Some(PossibleValue::new("json")),
58+
}
59+
}
60+
}
61+
3962
#[derive(Args, Debug)]
4063
pub struct RemoveImages {
4164
/// If not provided, remove all images.
@@ -118,7 +141,7 @@ impl Images {
118141
}
119142
}
120143

121-
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq)]
144+
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, serde::Serialize)]
122145
struct Image {
123146
repository: String,
124147
tag: String,
@@ -258,7 +281,9 @@ fn get_image_target(
258281
}
259282

260283
pub fn list_images(
261-
ListImages { targets, .. }: ListImages,
284+
ListImages {
285+
targets, format, ..
286+
}: ListImages,
262287
engine: &docker::Engine,
263288
msg_info: &mut MessageInfo,
264289
) -> cross::Result<()> {
@@ -281,45 +306,55 @@ pub fn list_images(
281306
let mut keys: Vec<&str> = map.keys().map(|k| k.as_ref()).collect();
282307
keys.sort_unstable();
283308

284-
let print_string =
285-
|col1: &str, col2: &str, fill: char, info: &mut MessageInfo| -> cross::Result<()> {
286-
let mut row = String::new();
287-
row.push('|');
288-
row.push(fill);
289-
row.push_str(col1);
290-
let spaces = max_target_len.max(col1.len()) + 1 - col1.len();
291-
for _ in 0..spaces {
292-
row.push(fill);
293-
}
294-
row.push('|');
295-
row.push(fill);
296-
row.push_str(col2);
297-
let spaces = max_image_len.max(col2.len()) + 1 - col2.len();
298-
for _ in 0..spaces {
299-
row.push(fill);
309+
match format {
310+
OutputFormat::Json => {
311+
msg_info.info(format_args!("{}", serde_json::to_string(&map)?))?;
312+
}
313+
OutputFormat::Human => {
314+
let print_string =
315+
|col1: &str, col2: &str, fill: char, info: &mut MessageInfo| -> cross::Result<()> {
316+
let mut row = String::new();
317+
row.push('|');
318+
row.push(fill);
319+
row.push_str(col1);
320+
let spaces = max_target_len.max(col1.len()) + 1 - col1.len();
321+
for _ in 0..spaces {
322+
row.push(fill);
323+
}
324+
row.push('|');
325+
row.push(fill);
326+
row.push_str(col2);
327+
let spaces = max_image_len.max(col2.len()) + 1 - col2.len();
328+
for _ in 0..spaces {
329+
row.push(fill);
330+
}
331+
row.push('|');
332+
info.print(row)
333+
};
334+
335+
if targets.len() != 1 {
336+
print_string("Targets", "Images", ' ', msg_info)?;
337+
print_string("-------", "------", '-', msg_info)?;
300338
}
301-
row.push('|');
302-
info.print(row)
303-
};
304-
305-
if targets.len() != 1 {
306-
print_string("Targets", "Images", ' ', msg_info)?;
307-
print_string("-------", "------", '-', msg_info)?;
308-
}
309339

310-
let print_single =
311-
|_: &str, image: &Image, info: &mut MessageInfo| -> cross::Result<()> { info.print(image) };
312-
let print_table = |target: &str, image: &Image, info: &mut MessageInfo| -> cross::Result<()> {
313-
let name = image.name();
314-
print_string(target, &name, ' ', info)
315-
};
316-
317-
for target in keys {
318-
for image in map.get(target).expect("map must have key").iter() {
319-
if targets.len() == 1 {
320-
print_single(target, image, msg_info)?;
321-
} else {
322-
print_table(target, image, msg_info)?;
340+
let print_single = |_: &str,
341+
image: &Image,
342+
info: &mut MessageInfo|
343+
-> cross::Result<()> { info.print(image) };
344+
let print_table =
345+
|target: &str, image: &Image, info: &mut MessageInfo| -> cross::Result<()> {
346+
let name = image.name();
347+
print_string(target, &name, ' ', info)
348+
};
349+
350+
for target in keys {
351+
for image in map.get(target).expect("map must have key").iter() {
352+
if targets.len() == 1 {
353+
print_single(target, image, msg_info)?;
354+
} else {
355+
print_table(target, image, msg_info)?;
356+
}
357+
}
323358
}
324359
}
325360
}

src/bin/cross.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ pub fn main() -> cross::Result<()> {
1818
let target_list = rustc::target_list(&mut Verbosity::Quiet.into())?;
1919
let args = cli::parse(&target_list)?;
2020
let subcommand = args.subcommand;
21-
let mut msg_info = shell::MessageInfo::create(
22-
args.verbose
23-
+ cross::config::bool_from_envvar(&std::env::var("CROSS_DEBUG").unwrap_or_default())
24-
as u8,
25-
args.quiet,
26-
args.color.as_deref(),
27-
)?;
21+
let mut msg_info = shell::MessageInfo::create(args.verbose, args.quiet, args.color.as_deref())?;
2822
let status = match cross::run(args, target_list, &mut msg_info)? {
2923
Some(status) => status,
3024
None => {

src/docker/remote.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ macro_rules! bail_container_exited {
2929
}};
3030
}
3131

32+
#[track_caller]
3233
fn subcommand_or_exit(engine: &Engine, cmd: &str) -> Result<Command> {
3334
bail_container_exited!();
3435
Ok(engine.subcommand(cmd))
@@ -42,6 +43,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
4243
// NOTE: `reldir` should be a relative POSIX path to the root directory
4344
// on windows, this should be something like `mnt/c`. that is, all paths
4445
// inside the container should not have the mount prefix.
46+
#[track_caller]
4547
fn create_dir(
4648
&self,
4749
reldir: &str,
@@ -57,6 +59,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
5759

5860
// copy files for a docker volume, for remote host support
5961
// NOTE: `reldst` has the same caveats as `reldir` in `create_dir`.
62+
#[track_caller]
6063
fn copy_files(
6164
&self,
6265
src: &Path,
@@ -72,6 +75,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
7275
}
7376

7477
// copy files for a docker volume, for remote host support
78+
#[track_caller]
7579
fn copy_files_nocache(
7680
&self,
7781
src: &Path,
@@ -92,6 +96,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
9296

9397
// copy files for a docker volume, for remote host support
9498
// provides a list of files relative to src.
99+
#[track_caller]
95100
fn copy_file_list(
96101
&self,
97102
src: &Path,
@@ -116,6 +121,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
116121

117122
// removed files from a docker volume, for remote host support
118123
// provides a list of files relative to src.
124+
#[track_caller]
119125
fn remove_file_list(
120126
&self,
121127
reldst: &str,
@@ -156,6 +162,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
156162
.run_and_get_status(msg_info, true)
157163
}
158164

165+
#[track_caller]
159166
fn container_path_exists(
160167
&self,
161168
relpath: &str,
@@ -173,6 +180,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
173180
.success())
174181
}
175182

183+
#[track_caller]
176184
pub fn copy_xargo(&self, mount_prefix: &str, msg_info: &mut MessageInfo) -> Result<()> {
177185
let dirs = &self.toolchain_dirs;
178186
let reldst = dirs.xargo_mount_path_relative()?;
@@ -194,6 +202,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
194202
Ok(())
195203
}
196204

205+
#[track_caller]
197206
pub fn copy_cargo(
198207
&self,
199208
mount_prefix: &str,
@@ -230,6 +239,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
230239
}
231240

232241
// copy over files needed for all targets in the toolchain that should never change
242+
#[track_caller]
233243
fn copy_rust_base(&self, mount_prefix: &str, msg_info: &mut MessageInfo) -> Result<()> {
234244
let dirs = &self.toolchain_dirs;
235245

@@ -273,6 +283,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
273283
warn_symlinks(had_symlinks, msg_info)
274284
}
275285

286+
#[track_caller]
276287
fn copy_rust_manifest(&self, mount_prefix: &str, msg_info: &mut MessageInfo) -> Result<()> {
277288
let dirs = &self.toolchain_dirs;
278289

@@ -298,6 +309,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
298309
}
299310

300311
// copy over the toolchain for a specific triple
312+
#[track_caller]
301313
fn copy_rust_triple(
302314
&self,
303315
target_triple: &TargetTriple,
@@ -335,6 +347,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
335347
Ok(())
336348
}
337349

350+
#[track_caller]
338351
pub fn copy_rust(
339352
&self,
340353
target_triple: Option<&TargetTriple>,
@@ -355,6 +368,7 @@ impl<'a, 'b, 'c> ContainerDataVolume<'a, 'b, 'c> {
355368
Ok(())
356369
}
357370

371+
#[track_caller]
358372
fn copy_mount(
359373
&self,
360374
src: &Path,

0 commit comments

Comments
 (0)