diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 871966b0a7..2ef5f5723c 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -14,6 +14,8 @@ use codex_cli::login::run_login_with_chatgpt; use codex_cli::login::run_logout; use codex_cli::proto; use codex_common::CliConfigOverrides; +use codex_core::config::find_codex_home; +use codex_core::config::load_config_from_toml; use codex_exec::Cli as ExecCli; use codex_responses_api_proxy::Args as ResponsesApiProxyArgs; use codex_tui::AppExitInfo; @@ -85,6 +87,9 @@ enum Subcommand { /// Resume a previous interactive session (picker by default; use --last to continue the most recent). Resume(ResumeCommand), + /// Validate the configuration file. + Config, + /// Internal: generate TypeScript protocol bindings. #[clap(hide = true)] GenerateTs(GenerateTsCommand), @@ -313,6 +318,31 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() Some(Subcommand::Completion(completion_cli)) => { print_completion(completion_cli); } + Some(Subcommand::Config) => { + let cli_overrides = match root_config_overrides.parse_overrides() { + Ok(overrides) => overrides, + Err(err) => { + eprintln!("Error parsing -c overrides: {err}"); + std::process::exit(1); + } + }; + + let codex_home = match find_codex_home() { + Ok(path) => path, + Err(err) => { + eprintln!("Error finding codex home: {err}"); + std::process::exit(1); + } + }; + + match load_config_from_toml(&codex_home, cli_overrides) { + Ok(_) => println!("Configuration validated successfully"), + Err(err) => { + eprintln!("Config validation error: {err}"); + std::process::exit(3); + } + } + } Some(Subcommand::Debug(debug_args)) => match debug_args.cmd { DebugCommand::Seatbelt(mut seatbelt_cli) => { prepend_config_flags( diff --git a/codex-rs/cli/tests/config.rs b/codex-rs/cli/tests/config.rs new file mode 100644 index 0000000000..3e487b98ac --- /dev/null +++ b/codex-rs/cli/tests/config.rs @@ -0,0 +1,44 @@ +use std::fs; +use std::path::Path; + +use anyhow::Result; +use assert_cmd::Command; +use tempfile::TempDir; + +fn codex_command(codex_home: &Path) -> Result { + let mut cmd = Command::cargo_bin("codex")?; + cmd.env("CODEX_HOME", codex_home); + Ok(cmd) +} + +#[test] +fn config_subcommand_reports_success_for_valid_config() -> Result<()> { + let codex_home = TempDir::new()?; + let config_path = codex_home.path().join("config.toml"); + fs::write(config_path, "model = \"gpt-5\"\n")?; + + let mut cmd = codex_command(codex_home.path())?; + let output = cmd.arg("config").output()?; + + assert!(output.status.success()); + let stdout = String::from_utf8(output.stdout)?; + assert!(stdout.contains("Configuration validated successfully")); + + Ok(()) +} + +#[test] +fn config_subcommand_exits_with_code_three_on_validation_error() -> Result<()> { + let codex_home = TempDir::new()?; + let config_path = codex_home.path().join("config.toml"); + fs::write(config_path, "model = 123\n")?; + + let mut cmd = codex_command(codex_home.path())?; + let output = cmd.arg("config").output()?; + + assert_eq!(output.status.code(), Some(3)); + let stderr = String::from_utf8(output.stderr)?; + assert!(stderr.contains("Config validation error")); + + Ok(()) +} diff --git a/codex-rs/core/src/config.rs b/codex-rs/core/src/config.rs index 5b5b60f8df..f932d0b691 100644 --- a/codex-rs/core/src/config.rs +++ b/codex-rs/core/src/config.rs @@ -254,6 +254,15 @@ pub fn load_config_as_toml_with_cli_overrides( Ok(cfg) } +/// Load `config.toml` from `codex_home`, apply CLI overrides, and deserialize +/// it into [`ConfigToml`]. +pub fn load_config_from_toml( + codex_home: &Path, + cli_overrides: Vec<(String, TomlValue)>, +) -> std::io::Result { + load_config_as_toml_with_cli_overrides(codex_home, cli_overrides) +} + /// Read `CODEX_HOME/config.toml` and return it as a generic TOML value. Returns /// an empty TOML table when the file does not exist. pub fn load_config_as_toml(codex_home: &Path) -> std::io::Result {