Skip to content

Commit 0c006fd

Browse files
authored
Backwards compatibility now says what to change (TraceMachina#1870)
1 parent 47602d1 commit 0c006fd

6 files changed

Lines changed: 92 additions & 72 deletions

File tree

Cargo.lock

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

nativelink-config/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ rust_library(
2626
"@crates//:byte-unit",
2727
"@crates//:humantime",
2828
"@crates//:serde",
29+
"@crates//:serde_json",
2930
"@crates//:serde_json5",
3031
"@crates//:shellexpand",
32+
"@crates//:tracing",
3133
],
3234
)
3335

@@ -60,6 +62,7 @@ rust_test(
6062
deps = [
6163
"@crates//:pretty_assertions",
6264
"@crates//:serde_json",
65+
"@crates//:tracing-test",
6366
],
6467
)
6568

nativelink-config/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ nativelink-error = { path = "../nativelink-error" }
1111
byte-unit = { version = "5.1.6", default-features = false, features = ["byte"] }
1212
humantime = "2.2.0"
1313
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
14+
serde_json = { version = "1.0.140", default-features = false, features = [
15+
"std",
16+
] }
1417
serde_json5 = "0.2.1"
1518
shellexpand = { version = "3.1.0", default-features = false, features = [
1619
"base-0",
1720
] }
21+
tracing = { version = "0.1.41", default-features = false }
1822

1923
[dev-dependencies]
2024
pretty_assertions = { version = "1.4.1", features = ["std"] }
21-
serde_json = { version = "1.0.140", default-features = false, features = [
22-
"std",
25+
tracing-test = { version = "0.2.5", default-features = false, features = [
26+
"no-env-filter",
2327
] }

nativelink-config/src/backcompat.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22

3-
use serde::{Deserialize, Deserializer};
3+
use serde::{Deserialize, Deserializer, Serialize};
4+
use tracing::warn;
45

56
use crate::cas_server::WithInstanceName;
67

@@ -11,23 +12,6 @@ enum WithInstanceNameBackCompat<T> {
1112
Vec(Vec<WithInstanceName<T>>),
1213
}
1314

14-
const DEPRECATION_MESSAGE: &str = r#"
15-
WARNING: Using deprecated map format for services. Please migrate to the new array format:
16-
// Old:
17-
"cas": {
18-
"main": {
19-
"cas_store": "STORE_NAME"
20-
}
21-
}
22-
// New:
23-
"cas": [
24-
{
25-
"instance_name": "main",
26-
"cas_store": "STORE_NAME"
27-
}
28-
]
29-
"#;
30-
3115
/// Use `#[serde(default, deserialize_with = "backcompat::opt_vec_named_config")]` for backwards
3216
/// compatibility with map-based access. A deprecation warning will be written to stderr if the
3317
/// old format is used.
@@ -36,22 +20,36 @@ pub(crate) fn opt_vec_with_instance_name<'de, D, T>(
3620
) -> Result<Option<Vec<WithInstanceName<T>>>, D::Error>
3721
where
3822
D: Deserializer<'de>,
39-
T: Deserialize<'de>,
23+
T: Deserialize<'de> + Serialize,
4024
{
4125
let Some(back_compat) = Option::deserialize(deserializer)? else {
4226
return Ok(None);
4327
};
4428

4529
match back_compat {
4630
WithInstanceNameBackCompat::Map(map) => {
47-
eprintln!("{DEPRECATION_MESSAGE}");
48-
let vec = map
31+
// TODO(palfrey): ideally this would be serde_json5::to_string_pretty but that doesn't exist
32+
// JSON is close enough to be workable for now
33+
let serde_map = serde_json::to_string_pretty(&map).expect("valid map");
34+
let vec: Vec<WithInstanceName<T>> = map
4935
.into_iter()
5036
.map(|(instance_name, config)| WithInstanceName {
5137
instance_name,
5238
config,
5339
})
5440
.collect();
41+
warn!(
42+
r"WARNING: Using deprecated map format for services. Please migrate to the new array format:
43+
// Old:
44+
{}
45+
// New:
46+
{}
47+
",
48+
serde_map,
49+
// TODO(palfrey): ideally this would be serde_json5::to_string_pretty but that doesn't exist
50+
// JSON is close enough to be workable for now
51+
serde_json::to_string_pretty(&vec).expect("valid new map")
52+
);
5553
Ok(Some(vec))
5654
}
5755
WithInstanceNameBackCompat::Vec(vec) => Ok(Some(vec)),
@@ -61,21 +59,23 @@ where
6159
#[cfg(test)]
6260
mod tests {
6361
use serde_json::json;
62+
use tracing_test::traced_test;
6463

6564
use super::*;
6665

67-
#[derive(Debug, Deserialize, PartialEq)]
66+
#[derive(Debug, Deserialize, Serialize, PartialEq)]
6867
struct PartialConfig {
6968
store: String,
7069
}
7170

72-
#[derive(Debug, Deserialize, PartialEq)]
71+
#[derive(Debug, Deserialize, Serialize, PartialEq)]
7372
struct FullConfig {
7473
#[serde(default, deserialize_with = "opt_vec_with_instance_name")]
7574
cas: Option<Vec<WithInstanceName<PartialConfig>>>,
7675
}
7776

7877
#[test]
78+
#[traced_test]
7979
fn test_configs_deserialization() {
8080
let old_format = json!({
8181
"cas": {
@@ -109,6 +109,17 @@ mod tests {
109109
}
110110

111111
assert_eq!(old_format, new_format);
112+
113+
logs_assert(|lines: &[&str]| {
114+
if lines.len() != 1 {
115+
return Err(format!("Expected 1 log line, got: {lines:?}"));
116+
}
117+
let line = lines[0];
118+
// TODO(palfrey): we should be checking the whole thing, but tracing-test is broken with multi-line items
119+
// See https://github.com/dbrgn/tracing-test/issues/48
120+
assert!(line.ends_with("WARNING: Using deprecated map format for services. Please migrate to the new array format:"));
121+
Ok(())
122+
});
112123
}
113124

114125
#[test]

0 commit comments

Comments
 (0)