Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing messages when --message-format=json is deeply nested #6032

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ lazycell = "1.0"
libc = "0.2"
log = "0.4"
libgit2-sys = "0.7.5"
miniserde = "0.1.6"
num_cpus = "1.0"
opener = "0.3.0"
rustfix = "0.4.2"
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::io::{self, Write};
use std::path::{self, Path, PathBuf};
use std::sync::Arc;

use miniserde;
use same_file::is_same_file;
use serde_json;

use core::manifest::TargetSourcePath;
use core::profiles::{Lto, Profile};
Expand Down Expand Up @@ -1009,7 +1009,7 @@ fn json_stderr(line: &str, package_id: &PackageId, target: &Target) -> CargoResu
// stderr from rustc/rustdoc can have a mix of JSON and non-JSON output
if line.starts_with('{') {
// Handle JSON lines
let compiler_message = serde_json::from_str(line)
let compiler_message = miniserde::json::from_str(line)
.map_err(|_| internal(&format!("compiler produced invalid json: `{}`", line)))?;

machine_message::emit(&machine_message::FromCompiler {
Expand Down
98 changes: 91 additions & 7 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#![allow(deprecated)] // for SipHasher

use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::hash::{Hash, Hasher, SipHasher};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::slice;

use miniserde;
use semver::Version;
use serde::ser;
use toml;
Expand Down Expand Up @@ -123,6 +126,12 @@ impl LibKind {
}
}

impl miniserde::Serialize for LibKind {
fn begin(&self) -> miniserde::ser::Fragment {
miniserde::ser::Fragment::Str(Cow::Borrowed(self.crate_type()))
}
}

impl fmt::Debug for LibKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.crate_type().fmt(f)
Expand Down Expand Up @@ -169,6 +178,37 @@ impl ser::Serialize for TargetKind {
}
}

impl miniserde::Serialize for TargetKind {
fn begin(&self) -> miniserde::ser::Fragment {
use miniserde::ser::{Fragment, Seq, Serialize};
use self::TargetKind::*;

enum KindsStream<'a> {
Lib(slice::Iter<'a, LibKind>),
// double reference to enable cast to &dyn Serialize
Single(Option<&'static &'static str>),
}

impl<'a> Seq for KindsStream<'a> {
fn next(&mut self) -> Option<&Serialize> {
Some(match self {
KindsStream::Lib(kinds) => kinds.next()?,
KindsStream::Single(single) => single.take()?,
})
}
}

Fragment::Seq(Box::new(match self {
Lib(kinds) => KindsStream::Lib(kinds.iter()),
Bin => KindsStream::Single(Some(&"bin")),
ExampleBin | ExampleLib(_) => KindsStream::Single(Some(&"example")),
Test => KindsStream::Single(Some(&"test")),
CustomBuild => KindsStream::Single(Some(&"custom-build")),
Bench => KindsStream::Single(Some(&"bench")),
}))
}
}

impl fmt::Debug for TargetKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::TargetKind::*;
Expand Down Expand Up @@ -261,25 +301,69 @@ struct SerializedTarget<'a> {
/// See https://doc.rust-lang.org/reference/linkage.html
crate_types: Vec<&'a str>,
name: &'a str,
src_path: &'a PathBuf,
edition: &'a str,
src_path: &'a Path,
edition: String,
#[serde(rename = "required-features", skip_serializing_if = "Option::is_none")]
required_features: Option<Vec<&'a str>>,
}

impl ser::Serialize for Target {
fn serialize<S: ser::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
impl Target {
fn to_serialized_target(&self) -> SerializedTarget {
SerializedTarget {
kind: &self.kind,
crate_types: self.rustc_crate_types(),
name: &self.name,
src_path: &self.src_path.path().to_path_buf(),
edition: &self.edition.to_string(),
src_path: self.src_path.path(),
edition: self.edition.to_string(),
required_features: self
.required_features
.as_ref()
.map(|rf| rf.iter().map(|s| &**s).collect()),
}.serialize(s)
}
}
}

impl ser::Serialize for Target {
fn serialize<S: ser::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
self.to_serialized_target().serialize(s)
}
}

impl miniserde::Serialize for Target {
fn begin(&self) -> miniserde::ser::Fragment {
use miniserde::ser::{Fragment, Map, Serialize};

struct TargetStream<'a> {
data: SerializedTarget<'a>,
src_path: Cow<'a, str>,
state: usize,
}

impl<'a> Map for TargetStream<'a> {
fn next(&mut self) -> Option<(Cow<str>, &Serialize)> {
let state = self.state;
self.state += 1;
match state {
0 => Some((Cow::Borrowed("kind"), self.data.kind)),
1 => Some((Cow::Borrowed("crate_types"), &self.data.crate_types)),
2 => Some((Cow::Borrowed("name"), &self.data.name)),
3 => Some((Cow::Borrowed("src_path"), &self.src_path)),
4 => Some((Cow::Borrowed("edition"), &self.data.edition)),
5 => Some((
// skipped if None
Cow::Borrowed("required-features"),
self.data.required_features.as_ref()?,
)),
_ => None,
}
}
}

Fragment::Map(Box::new(TargetStream {
data: self.to_serialized_target(),
src_path: self.src_path.path().to_string_lossy(),
state: 0,
}))
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt::{self, Formatter};
use std::hash::Hash;
use std::hash;
use std::path::Path;
use std::sync::Arc;

use miniserde;
use semver;
use serde::de;
use serde::ser;
Expand Down Expand Up @@ -40,6 +42,17 @@ impl ser::Serialize for PackageId {
}
}

impl miniserde::Serialize for PackageId {
fn begin(&self) -> miniserde::ser::Fragment {
miniserde::ser::Fragment::Str(Cow::Owned(format!(
"{} {} ({})",
self.inner.name,
self.inner.version,
self.inner.source_id.to_url(),
)))
}
}

impl<'de> de::Deserialize<'de> for PackageId {
fn deserialize<D>(d: D) -> Result<PackageId, D::Error>
where
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ extern crate libc;
extern crate libgit2_sys;
#[macro_use]
extern crate log;
#[macro_use]
extern crate miniserde;
extern crate num_cpus;
extern crate opener;
extern crate rustfix;
Expand Down
57 changes: 46 additions & 11 deletions src/cargo/util/machine_message.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
use serde::ser;
use serde_json::{self, Value};
use std::borrow::Cow;

use miniserde::json;
use miniserde::ser::{self, Fragment, Serialize};

use core::{PackageId, Target};

pub trait Message: ser::Serialize {
pub trait Message: Serialize {
fn reason(&self) -> &str;
}

pub fn emit<T: Message>(t: &T) {
let mut json: Value = serde_json::to_value(t).unwrap();
json["reason"] = json!(t.reason());
println!("{}", json);
struct Wrapper<'a> {
reason: &'a str,
message: &'a Message,
}

impl<'a> Serialize for Wrapper<'a> {
fn begin(&self) -> Fragment {
Fragment::Map(Box::new(StreamMessage {
reason: Some(&self.reason),
value: match Serialize::begin(self.message) {
Fragment::Map(map) => map,
_ => panic!("machine_message::emit expected a JSON map"),
},
}))
}
}

struct StreamMessage<'a> {
// double reference to enable cast to &dyn Serialize
reason: Option<&'a &'a str>,
value: Box<ser::Map + 'a>,
}

impl<'a> ser::Map for StreamMessage<'a> {
fn next(&mut self) -> Option<(Cow<str>, &Serialize)> {
match self.reason.take() {
Some(reason) => Some((Cow::Borrowed("reason"), reason)),
None => self.value.next(),
}
}
}

println!("{}", json::to_string(&Wrapper {
reason: t.reason(),
message: t,
}));
}

#[derive(Serialize)]
#[derive(MiniSerialize)]
pub struct FromCompiler<'a> {
pub package_id: &'a PackageId,
pub target: &'a Target,
pub message: serde_json::Value,
pub message: json::Value,
}

impl<'a> Message for FromCompiler<'a> {
Expand All @@ -26,7 +61,7 @@ impl<'a> Message for FromCompiler<'a> {
}
}

#[derive(Serialize)]
#[derive(MiniSerialize)]
pub struct Artifact<'a> {
pub package_id: &'a PackageId,
pub target: &'a Target,
Expand All @@ -45,7 +80,7 @@ impl<'a> Message for Artifact<'a> {
/// This is different from the regular `Profile` to maintain backwards
/// compatibility (in particular, `test` is no longer in `Profile`, but we
/// still want it to be included here).
#[derive(Serialize)]
#[derive(MiniSerialize)]
pub struct ArtifactProfile {
pub opt_level: &'static str,
pub debuginfo: Option<u32>,
Expand All @@ -54,7 +89,7 @@ pub struct ArtifactProfile {
pub test: bool,
}

#[derive(Serialize)]
#[derive(MiniSerialize)]
pub struct BuildScript<'a> {
pub package_id: &'a PackageId,
pub linked_libs: &'a [String],
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Write};

use glob::glob;
use support::install::exe;
use support::is_nightly;
Expand Down Expand Up @@ -690,3 +692,24 @@ fn does_not_use_empty_rustc_wrapper() {
let p = project().file("src/lib.rs", "").build();
p.cargo("check").env("RUSTC_WRAPPER", "").run();
}

#[test]
fn error_from_deep_recursion() -> Result<(), fmt::Error> {
let mut big_macro = String::new();
writeln!(big_macro, "macro_rules! m {{")?;
for i in 0..130 {
writeln!(big_macro, "({}) => {{ m!({}); }};", i, i + 1)?;
}
writeln!(big_macro, "}}")?;
writeln!(big_macro, "m!(0);")?;

let p = project().file("src/lib.rs", &big_macro).build();
p.cargo("check --message-format=json")
.with_status(101)
.with_stdout_contains(
"[..]\"message\":\"recursion limit reached while expanding the macro `m`\"[..]",
)
.run();

Ok(())
}