From 103d05149d5052f03b518d557dd7bd8307a79c5e Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 3 May 2025 17:35:08 -0700 Subject: [PATCH] cli: add "$schema" line when creating new config file It seems like a small usability improvement if users don't need to enter the "$schema" link manually when they create a new config file. This doesn't help existing users. --- cli/tests/test_config_command.rs | 20 +++++++++++++++++--- lib/src/config.rs | 14 +++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/cli/tests/test_config_command.rs b/cli/tests/test_config_command.rs index b0a4c7acc..748b48ba1 100644 --- a/cli/tests/test_config_command.rs +++ b/cli/tests/test_config_command.rs @@ -231,6 +231,7 @@ fn test_config_list_layer() { let output = work_dir.run_jj(["config", "list", "--user"]); insta::assert_snapshot!(output, @r#" + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" test-key = "test-val" test-layered-key = "test-original-val" [EOF] @@ -255,6 +256,7 @@ fn test_config_list_layer() { let output = work_dir.run_jj(["config", "list", "--repo"]); insta::assert_snapshot!(output, @r#" + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" test-layered-key = "test-layered-val" [EOF] "#); @@ -304,6 +306,7 @@ fn test_config_list_origin() { ]); insta::assert_snapshot!(output, @r#" test-key = "test-val" # user $TEST_ENV/config/config.toml + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" # repo $TEST_ENV/repo/.jj/repo/config.toml test-layered-key = "test-layered-val" # repo $TEST_ENV/repo/.jj/repo/config.toml user.name = "Test User" # env user.email = "test.user@example.com" # env @@ -597,6 +600,7 @@ fn test_config_set_for_user() { let user_config_toml = std::fs::read_to_string(&user_config_path) .unwrap_or_else(|_| panic!("Failed to read file {}", user_config_path.display())); insta::assert_snapshot!(user_config_toml, @r#" + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" test-key = "test-val" [test-table] @@ -663,6 +667,7 @@ fn test_config_set_for_repo() { // Ensure test-key successfully written to user config. let repo_config_toml = work_dir.read_file(".jj/repo/config.toml"); insta::assert_snapshot!(repo_config_toml, @r#" + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" test-key = "test-val" [test-table] @@ -691,6 +696,8 @@ fn test_config_set_toml_types() { set_value("test-table.string", r#""foo""#); set_value("test-table.invalid", r"a + b"); insta::assert_snapshot!(std::fs::read_to_string(&user_config_path).unwrap(), @r#" + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" + [test-table] integer = 42 float = 3.14 @@ -784,7 +791,10 @@ fn test_config_unset_inline_table_key() { .run_jj(["config", "unset", "--user", "inline-table.foo"]) .success(); let user_config_toml = std::fs::read_to_string(&user_config_path).unwrap(); - insta::assert_snapshot!(user_config_toml, @"inline-table = {}"); + insta::assert_snapshot!(user_config_toml, @r#" + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" + inline-table = {} + "#); } #[test] @@ -856,7 +866,11 @@ fn test_config_unset_for_user() { .success(); let user_config_toml = std::fs::read_to_string(&user_config_path).unwrap(); - insta::assert_snapshot!(user_config_toml, @"[table]"); + insta::assert_snapshot!(user_config_toml, @r#" + "$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json" + + [table] + "#); } #[test] @@ -873,7 +887,7 @@ fn test_config_unset_for_repo() { .success(); let repo_config_toml = work_dir.read_file(".jj/repo/config.toml"); - insta::assert_snapshot!(repo_config_toml, @""); + insta::assert_snapshot!(repo_config_toml, @r#""$schema" = "https://jj-vcs.github.io/jj/latest/config-schema.json""#); } #[test] diff --git a/lib/src/config.rs b/lib/src/config.rs index 1c86d0b47..210e40ffa 100644 --- a/lib/src/config.rs +++ b/lib/src/config.rs @@ -559,11 +559,19 @@ impl ConfigFile { Err(ConfigLoadError::Read(PathError { path, error })) if error.kind() == io::ErrorKind::NotFound => { - Arc::new(ConfigLayer { + let mut data = DocumentMut::new(); + data.insert( + "$schema", + toml_edit::Item::Value( + "https://jj-vcs.github.io/jj/latest/config-schema.json".into(), + ), + ); + let layer = ConfigLayer { source, path: Some(path), - data: DocumentMut::new(), - }) + data, + }; + Arc::new(layer) } Err(err) => return Err(err), };