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

cli: Add --show-origin option to config list #3023

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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#2971](https://github.com/martinvonz/jj/issues/2971)). This may become the
default depending on feedback.

* `jj config list` gained a `--show-origin` flag to display the source of
config values.

### Fixed bugs

* On Windows, symlinks in the repo are now materialized as regular files in the
Expand Down
27 changes: 21 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ chrono = { version = "0.4.34", default-features = false, features = [
"std",
"clock",
] }
config = { version = "0.13.4", default-features = false, features = ["toml"] }
config = { version = "0.14.0", default-features = false, features = ["toml"] }
criterion = "0.5.1"
crossterm = { version = "0.27", default-features = false }
digest = "0.10.7"
Expand Down
29 changes: 28 additions & 1 deletion cli/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::io::Write;
use std::path::Path;

use clap::builder::NonEmptyStringValueParser;
use itertools::Itertools;
Expand Down Expand Up @@ -84,6 +85,11 @@ pub(crate) struct ConfigListArgs {
/// Allow printing overridden values.
#[arg(long)]
pub include_overridden: bool,
/// Display the source of all listed config values with type (default, env,
/// usercfg, repocfg, command line) and the source file path for usercfg
/// and repocfg.
#[arg(long)]
pub show_origin: bool,
/// Target the user-level config
#[arg(long)]
user: bool,
Expand Down Expand Up @@ -204,9 +210,16 @@ pub(crate) fn cmd_config_list(
if !args.include_defaults && *source == ConfigSource::Default {
continue;
}

let origin = if args.show_origin {
format_origin(value.origin(), source)
} else {
String::from("")
};

writeln!(
ui.stdout(),
"{}{}={}",
"{origin}{}{}={}",
if *is_overridden { "# " } else { "" },
path.join("."),
serialize_config_value(value)
Expand Down Expand Up @@ -304,3 +317,17 @@ pub(crate) fn cmd_config_path(
)?;
Ok(())
}

fn format_origin(origin: Option<&str>, source: &ConfigSource) -> String {
let Some(origin) = origin else {
return format!("{}: ", source.description());
};

// `config::FileSourceFile::resolve()` returns a relative path. Try to
// convert them to absolute paths for easier recognition, falling back
// to the original value on failure.
let canon = Path::new(origin).canonicalize();
let path = canon.as_deref().unwrap_or_else(|_| Path::new(origin));

format!("{} {}: ", source.description(), path.display())
}
17 changes: 14 additions & 3 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,23 @@ pub enum ConfigError {
pub enum ConfigSource {
Default,
Env,
// TODO: Track explicit file paths, especially for when user config is a dir.
User,
Repo,
CommandArg,
}

impl ConfigSource {
pub fn description(&self) -> &str {
match self {
ConfigSource::Default => "default",
ConfigSource::Env => "env",
ConfigSource::User => "usercfg",
ConfigSource::Repo => "repocfg",
ConfigSource::CommandArg => "cmdline",
}
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct AnnotatedValue {
pub path: Vec<String>,
Expand Down Expand Up @@ -584,8 +595,8 @@ mod tests {
command_args,
CommandNameAndArgs::Structured {
env: hashmap! {
"KEY1".to_string() => "value1".to_string(),
"KEY2".to_string() => "value2".to_string(),
"key1".to_string() => "value1".to_string(),
"key2".to_string() => "value2".to_string(),
Copy link
Contributor

@yuja yuja Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

environment config variables are no longer case-sensitive

Oops, it probably means the structured command config no longer works. Environment variables are case sensitive on Unix, and we do use the TOML table syntax to override them.
[EDIT] Alias definitions, merge tools, etc. will also get broken.

Maybe it's time to roll our own config module? I think it will help implement #616 and/or handle colors/revset-aliases overrides better.

Sorry for the late reply. I didn't notice this yesterday.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think my summary of what changed in config v0.14.0 is not quite right. Prior to v0.14.0 environment variable keys were already being downcased, see rust-cli/config-rs#340, but the internal keys tracked by config were not. For example, an internal PAGER key would not be set by environment variable PAGER because the latter had been converted to pager.

The change in rust-cli/config-rs#354 was to downcase the internal keys as well. Looking at the config schema I don't see any mixed case settings, do you have an example of what you would expect to break so I can be sure I understand?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior to v0.14.0 environment variable keys were already being downcased

I think you're talking about the config loaded from environment variables, but this CommandNameAndArgs stuff is data configured in a config file, and passed to the external process. So the case matters.

do you have an example of what you would expect to break

In addition to this, [aliases], [revset-aliases], [template-aliases], [merge-tools.<NAME>] would break if keys are downcased. They are free-form dictionary, and users may define revset-aliases.HEAD = "@-" for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wfchandler: To clarify, I think Yuya is talking about https://github.com/martinvonz/jj/blob/26528091e646a79a3724997f06c4af69eaf70297/cli/src/config/misc.toml#L9 (the user can have their own similar config). (Correct me if I'm wrong, @yuja.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, this is the upstream issue. It doesn't describe the exact problem we have, but the root cause would be the same.
rust-cli/config-rs#531

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, I see what you mean. I'll go ahead and close out this PR given the problem with config v0.14.0.

Thank you @yuja @martinvonz and @jonathantanmy for the thorough review, much better to have caught this issue now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope the problem will be addressed at config-rs, and this PR can be revisited later. Thanks anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for trying, and sorry about the trouble. And thanks to Yuya for noticing the problem!

},
command: NonEmptyCommandArgsVec(["emacs", "-nw",].map(|s| s.to_owned()).to_vec())
}
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ List variables set in config file, along with their values
Possible values: `true`, `false`
* `--show-origin` — Display the source of all listed config values with type (default, env, usercfg, repocfg, command line) and the source file path for usercfg and repocfg
Possible values: `true`, `false`
* `--user` — Target the user-level config
Possible values: `true`, `false`
Expand Down
76 changes: 72 additions & 4 deletions cli/tests/test_config_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,72 @@ fn test_config_list_all() {
"###);
}

#[test]
fn test_config_list_show_origin() {
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");

// Create multiple user configs.
test_env.add_config(
r#"
user-key-1 = "user-val-1"
"#,
);

test_env.add_config(
r#"
user-key-2 = "user-val-2"
"#,
);

// Env
test_env.add_env_var("env-key", "env-value");

// Repo
test_env.jj_cmd_ok(
&repo_path,
&["config", "set", "--repo", "repo-key", "repo-val"],
);

let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"config",
"list",
"--config-toml",
"cmd-key='cmd-val'",
"--show-origin",
],
);

// Paths starting with `$TEST_ENV` confirm that the relative path returned by
// `Value.origin()` has been converted to an absolute path.
insta::assert_snapshot!(stdout, @r###"
usercfg $TEST_ENV/config/config0001.toml: template-aliases.format_time_range(time_range)="time_range.start() ++ \" - \" ++ time_range.end()"
usercfg $TEST_ENV/config/config0002.toml: user-key-1="user-val-1"
usercfg $TEST_ENV/config/config0003.toml: user-key-2="user-val-2"
repocfg $TEST_ENV/repo/.jj/repo/config.toml: repo-key="repo-val"
env: debug.commit-timestamp="2001-02-03T04:05:09+07:00"
env: debug.operation-timestamp="2001-02-03T04:05:09+07:00"
env: debug.randomness-seed="3"
env: operation.hostname="host.example.com"
env: operation.username="test-username"
env: user.email="[email protected]"
env: user.name="Test User"
cmdline: cmd-key="cmd-val"
"###);

// Run again with defaults shown. Rather than assert the full output which
// will change when any default config value is added or updated, check only
// one value to validate the formatting is correct.
let stdout = test_env.jj_cmd_success(
&repo_path,
&["config", "list", "--include-defaults", "--show-origin"],
);
assert!(stdout.contains(r#"default: colors.diff header="yellow""#));
}

#[test]
fn test_config_list_layer() {
let mut test_env = TestEnvironment::default();
Expand Down Expand Up @@ -628,14 +694,16 @@ fn test_config_get() {
"###);

let stdout = test_env.jj_cmd_failure(test_env.env_root(), &["config", "get", "table.list"]);
insta::assert_snapshot!(stdout, @r###"
Config error: invalid type: sequence, expected a value convertible to a string
insta::with_settings!({filters => vec![(r"config\\config0002.toml", "config/config0002.toml")]}, {
insta::assert_snapshot!(stdout, @r###"
Config error: invalid type: sequence, expected a value convertible to a string for key `table.list` in config/config0002.toml
For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md.
"###);
"###)
});

let stdout = test_env.jj_cmd_failure(test_env.env_root(), &["config", "get", "table"]);
insta::assert_snapshot!(stdout, @r###"
Config error: invalid type: map, expected a value convertible to a string
Config error: invalid type: map, expected a value convertible to a string for key `table`
For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md.
"###);

Expand Down
8 changes: 7 additions & 1 deletion cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ fn test_invalid_config() {
test_env.add_config("[section]key = value-missing-quotes");
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["init", "repo"]);
insta::assert_snapshot!(stderr.replace('\\', "/"), @r###"
Config error: expected newline, found an identifier at line 1 column 10 in config/config0002.toml
Config error: TOML parse error at line 1, column 10
|
1 | [section]key = value-missing-quotes
| ^
invalid table header
expected newline, `#`
in config/config0002.toml
For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md.
"###);
}
Expand Down