Skip to content

Commit 5f8bcec

Browse files
committed
fix(bootstrap): deserialize null as f64::NAN
When doing optimized build through opt-dist, I've often run into errors like `invalid type: null, expected f64`. This is likely because some f64 fields might actually bet set null. Unfortunately, serde_json doesn't handle null <-> NaN well. This commit addresses it by having a custom deserialize method, so null is always be deserialized to `f64:NAN`. See: * https://rust-lang.zulipchat.com/#narrow/channel/242791-t-infra/topic/opt-dist.3A.20.60invalid.20type.3A.20null.2C.20expect.20f64.60.20failure * serde-rs/json#202
1 parent 1e9b017 commit 5f8bcec

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Diff for: src/build_helper/src/metrics.rs

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct JsonInvocation {
1616
//
1717
// This is necessary to easily correlate this invocation with logs or other data.
1818
pub start_time: u64,
19+
#[serde(deserialize_with = "null_as_f64_nan")]
1920
pub duration_including_children_sec: f64,
2021
pub children: Vec<JsonNode>,
2122
}
@@ -28,6 +29,7 @@ pub enum JsonNode {
2829
type_: String,
2930
debug_repr: String,
3031

32+
#[serde(deserialize_with = "null_as_f64_nan")]
3133
duration_excluding_children_sec: f64,
3234
system_stats: JsonStepSystemStats,
3335

@@ -88,5 +90,11 @@ pub struct JsonInvocationSystemStats {
8890
#[derive(Serialize, Deserialize)]
8991
#[serde(rename_all = "snake_case")]
9092
pub struct JsonStepSystemStats {
93+
#[serde(deserialize_with = "null_as_f64_nan")]
9194
pub cpu_utilization_percent: f64,
9295
}
96+
97+
fn null_as_f64_nan<'de, D: serde::Deserializer<'de>>(d: D) -> Result<f64, D::Error> {
98+
use serde::Deserialize as _;
99+
Option::<f64>::deserialize(d).map(|f| f.unwrap_or(f64::NAN))
100+
}

0 commit comments

Comments
 (0)