Reject legacy [profiles] when using profile-v2 (#22647)

## Why

`profile-v2` layers the selected profile file on top of the base user
`config.toml`, but the legacy `[profiles]` table also stores named
profile overrides in that same base file. Allowing both paths during one
load makes it too easy to get a mixed profile where stale legacy
settings still influence a profile-v2 run.

## What Changed

- Detect a legacy `[profiles]` table in the base user config whenever
`--profile-v2` selects a profile file.
- Fail config loading with an `InvalidData` error that tells the user to
move those settings into the selected profile-v2 file or remove
`[profiles]`.
- Add a loader regression covering `--profile-v2` with legacy
`[profiles]` in `config.toml`.

## Testing

- `cargo test -p codex-config
profile_v2_rejects_legacy_profiles_in_base_user_config`
This commit is contained in:
jif-oai authored and GitHub committed 2026-05-15 11:35:42 +02:00
1 parent 302149d979
commit a5e5faf216
2 files changed
+202 -12

No files matched your search

+30 -12
View File
@@ -1,6 +1,8 @@
mod layer_io;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(test)]
mod tests;
use self::layer_io::LoadedConfigLayers;
use crate::CONFIG_TOML_FILE;
@@ -211,19 +213,35 @@ pub async fn load_config_layers_state(
// Add the base user config layer. When profile-v2 is selected, add the
// profile config as a second user layer on top so the profile only needs to
// contain overrides.
let base_user_file = AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, codex_home);
layers.push(
load_user_config_layer(
fs,
&base_user_file,
/*profile*/ None,
ignore_user_config,
strict_config,
)
.await?,
);
let active_user_file = overrides.user_config_path(codex_home)?;
let base_user_file = AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, codex_home);
let base_user_layer = load_user_config_layer(
fs,
&base_user_file,
/*profile*/ None,
ignore_user_config,
strict_config,
)
.await?;
if let Some(active_user_profile) = active_user_profile.as_ref()
&& base_user_layer.config.as_table().is_some_and(|config| {
config
.get("profiles")
.and_then(TomlValue::as_table)
.is_some_and(|profiles| profiles.contains_key(active_user_profile.as_str()))
})
{
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"--profile-v2 `{active_user_profile}` cannot be used while {} contains legacy `[profiles.{active_user_profile}]` config; move those settings into {} or remove `[profiles.{active_user_profile}]`",
base_user_file.as_path().display(),
active_user_file.as_path().display()
),
));
}
layers.push(base_user_layer);
if active_user_file != base_user_file {
layers.push(
load_user_config_layer(