Centralize schema selection for config diagnostics (#62587)

## What changed

Choose the full config or shell environment filter schema inside the
diagnostics module. Config loaders call the helper for the validation
that failed without passing a deserialization type or the
`config.toml` filename. Both helpers use the same layer traversal and
source location reporting.

GitOrigin-RevId: 5dcc7984b907e3769904c303cd08577c6faf4769
This commit is contained in:
Charlie Marsh
2026-11-19 20:42:24 +00:00
committed by copyberry
parent 90dd6cf9df
commit 8b2070d29f
5 changed files with 62 additions and 56 deletions
@@ -1,9 +1,7 @@
use super::*;
use crate::CONFIG_TOML_FILE;
use crate::ConfigLayerStack;
use crate::ConfigRequirements;
use crate::ConfigRequirementsToml;
use crate::config_toml::ConfigToml;
use crate::first_layer_config_error_from_entries;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_absolute_path::test_support::PathBufExt;
@@ -212,7 +210,7 @@ async fn raw_toml_diagnostics_use_enterprise_layer_name() {
)
.expect("cloud config layers should parse");
let error = first_layer_config_error_from_entries::<ConfigToml>(&layers, CONFIG_TOML_FILE)
let error = first_layer_config_error_from_entries(&layers)
.await
.expect("invalid raw TOML should produce a layer diagnostic");
+53 -30
View File
@@ -1,12 +1,14 @@
//! Helpers for mapping config parse/validation failures to file locations and
//! rendering them in a user-friendly way.
use crate::CONFIG_TOML_FILE;
use crate::ConfigLayerEntry;
use crate::ConfigLayerSource;
use crate::ConfigLayerStack;
use crate::config_toml::ConfigToml;
use crate::format_config_layer_source;
use crate::shell_environment_policy::ShellEnvironmentPolicyFilterConfigToml;
use codex_utils_absolute_path::AbsolutePathBufGuard;
use serde::de::DeserializeOwned;
use serde_path_to_error::Path as SerdePath;
use serde_path_to_error::Segment as SerdeSegment;
use std::fmt;
@@ -129,18 +131,39 @@ pub(crate) fn config_error_from_toml_for_source(
ConfigError::new(source.to_path_buf(), range, err.message())
}
pub fn config_error_from_typed_toml<T: DeserializeOwned>(
path: impl AsRef<Path>,
contents: &str,
) -> Option<ConfigError> {
config_error_from_typed_toml_for_source::<T>(
ConfigDiagnosticSource::Path(path.as_ref()),
#[derive(Clone, Copy)]
enum DiagnosticSchema {
Config,
ShellEnvironmentFilters,
}
impl DiagnosticSchema {
fn deserialize_error<'de, D: serde::Deserializer<'de>>(
self,
deserializer: D,
) -> Option<serde_path_to_error::Error<D::Error>> {
match self {
Self::Config => serde_path_to_error::deserialize::<_, ConfigToml>(deserializer).err(),
Self::ShellEnvironmentFilters => serde_path_to_error::deserialize::<
_,
ShellEnvironmentPolicyFilterConfigToml,
>(deserializer)
.err(),
}
}
}
pub fn config_error_from_typed_toml(path: &Path, contents: &str) -> Option<ConfigError> {
config_error_from_typed_toml_for_source(
DiagnosticSchema::Config,
ConfigDiagnosticSource::Path(path),
contents,
/*layer*/ None,
)
}
fn config_error_from_typed_toml_for_source<T: DeserializeOwned>(
fn config_error_from_typed_toml_for_source(
schema: DiagnosticSchema,
source: ConfigDiagnosticSource<'_>,
contents: &str,
layer: Option<&ConfigLayerSource>,
@@ -150,13 +173,13 @@ fn config_error_from_typed_toml_for_source<T: DeserializeOwned>(
Err(err) => return Some(config_error_from_toml_for_source(source, contents, err)),
};
let mut err = serde_path_to_error::deserialize::<_, T>(deserializer).err()?;
let mut err = schema.deserialize_error(deserializer)?;
// Diagnose the same effective fields against the text we just read from disk.
if let Some(layer) = layer
&& let Ok(raw) = toml::from_str(contents)
&& let std::borrow::Cow::Owned(effective) = layer.effective_values(&raw)
{
err = serde_path_to_error::deserialize::<_, T>(effective).err()?;
err = schema.deserialize_error(effective)?;
}
let path_hint = err.path().clone();
let toml_err: toml::de::Error = err.into_inner();
@@ -171,37 +194,35 @@ fn config_error_from_typed_toml_for_source<T: DeserializeOwned>(
))
}
pub async fn first_layer_config_error<T: DeserializeOwned>(
layers: &ConfigLayerStack,
config_toml_file: &str,
) -> Option<ConfigError> {
pub async fn first_layer_config_error(layers: &ConfigLayerStack) -> Option<ConfigError> {
// When the merged config fails schema validation, we surface the first concrete
// per-file error to point users at a specific file and range rather than an
// opaque merged-layer failure.
first_layer_config_error_for_entries::<T, _>(layers.layers_low_to_high(), config_toml_file)
.await
first_layer_error(layers.layers_low_to_high(), DiagnosticSchema::Config).await
}
pub async fn first_layer_config_error_from_entries<T: DeserializeOwned>(
pub async fn first_layer_config_error_from_entries(
layers: &[ConfigLayerEntry],
config_toml_file: &str,
) -> Option<ConfigError> {
first_layer_config_error_for_entries::<T, _>(layers.iter(), config_toml_file).await
first_layer_error(layers, DiagnosticSchema::Config).await
}
async fn first_layer_config_error_for_entries<'a, T: DeserializeOwned, I>(
layers: I,
config_toml_file: &str,
) -> Option<ConfigError>
where
I: IntoIterator<Item = &'a ConfigLayerEntry>,
{
pub(crate) async fn first_layer_shell_environment_filter_error(
layers: &[ConfigLayerEntry],
) -> Option<ConfigError> {
first_layer_error(layers, DiagnosticSchema::ShellEnvironmentFilters).await
}
async fn first_layer_error<'a>(
layers: impl IntoIterator<Item = &'a ConfigLayerEntry>,
schema: DiagnosticSchema,
) -> Option<ConfigError> {
for layer in layers {
if layer.is_disabled() {
continue;
}
if let Some(contents) = layer.raw_toml() {
let source_name = format_config_layer_source(&layer.name, config_toml_file);
let source_name = format_config_layer_source(&layer.name, CONFIG_TOML_FILE);
let Some(base_dir) = layer.raw_toml_base_dir() else {
tracing::debug!(
"Skipping raw TOML diagnostics for {source_name} because it has no base directory"
@@ -212,7 +233,8 @@ where
// parsed into the runtime layer so diagnostics resolve relative
// path fields with the same semantics.
let _absolute_path_base = AbsolutePathBufGuard::new(base_dir.as_path());
if let Some(error) = config_error_from_typed_toml_for_source::<T>(
if let Some(error) = config_error_from_typed_toml_for_source(
schema,
ConfigDiagnosticSource::DisplayName(&source_name),
contents,
Some(&layer.name),
@@ -222,7 +244,7 @@ where
continue;
}
let Some(path) = config_path_for_layer(layer, config_toml_file) else {
let Some(path) = config_path_for_layer(layer, CONFIG_TOML_FILE) else {
continue;
};
let contents = match tokio::fs::read_to_string(&path).await {
@@ -239,7 +261,8 @@ where
continue;
};
let _guard = AbsolutePathBufGuard::new(parent);
if let Some(error) = config_error_from_typed_toml_for_source::<T>(
if let Some(error) = config_error_from_typed_toml_for_source(
schema,
ConfigDiagnosticSource::Path(&path),
&contents,
Some(&layer.name),
+3 -12
View File
@@ -28,15 +28,14 @@ use crate::config_requirements::RequirementSource;
use crate::config_requirements::SandboxModeRequirement;
use crate::config_toml::ConfigToml;
use crate::config_toml::ProjectConfig;
use crate::diagnostics::ConfigError;
use crate::diagnostics::config_error_from_toml;
use crate::diagnostics::first_layer_config_error_from_entries as typed_first_layer_config_error_from_entries;
use crate::diagnostics::first_layer_config_error_from_entries;
use crate::diagnostics::first_layer_shell_environment_filter_error;
use crate::diagnostics::io_error_from_config_error;
use crate::merge::merge_toml_values;
use crate::overrides::build_cli_overrides_layer;
use crate::project_root_markers::default_project_root_markers;
use crate::project_root_markers::project_root_markers_from_config;
use crate::shell_environment_policy::ShellEnvironmentPolicyFilterConfigToml;
use crate::state::ConfigLayerEntry;
use crate::state::ConfigLayerStack;
use crate::state::ConfigLoadOptions;
@@ -104,10 +103,6 @@ const PROJECT_LOCAL_CONFIG_DENYLIST: &[&str] = &[
"otel",
];
async fn first_layer_config_error_from_entries(layers: &[ConfigLayerEntry]) -> Option<ConfigError> {
typed_first_layer_config_error_from_entries::<ConfigToml>(layers, CONFIG_TOML_FILE).await
}
/// To build up the set of admin-enforced constraints, requirements layers are
/// collected in ascending precedence order, matching config layers, and then
/// composed with config-style TOML merging plus field-specific handling for
@@ -504,11 +499,7 @@ pub async fn load_config_layers_state(
}
if let Err(err) = validate_enabled_config_layers(&layers) {
if let Some(config_error) = typed_first_layer_config_error_from_entries::<
ShellEnvironmentPolicyFilterConfigToml,
>(&layers, CONFIG_TOML_FILE)
.await
{
if let Some(config_error) = first_layer_shell_environment_filter_error(&layers).await {
return Err(io_error_from_config_error(
io::ErrorKind::InvalidData,
config_error,
@@ -362,8 +362,7 @@ async fn returns_config_error_for_schema_error_in_user_config() {
let config_error = config_error_from_io(&err);
let _guard = codex_utils_absolute_path::AbsolutePathBufGuard::new(tmp.path());
let expected_config_error =
codex_config::config_error_from_typed_toml::<ConfigToml>(&config_path, contents)
.expect("schema error");
codex_config::config_error_from_typed_toml(&config_path, contents).expect("schema error");
assert_eq!(config_error, &expected_config_error);
}
@@ -487,7 +486,7 @@ exclude = ["LEGACY_*"]
assert_eq!(
config_error_from_io(&err),
&config_error_from_typed_toml::<ConfigToml>(&config_path, contents)
&config_error_from_typed_toml(&config_path, contents)
.expect("mixed shell policy should produce a typed config error")
);
}
@@ -845,8 +844,8 @@ collaboration_modes = "true""#;
std::fs::write(&config_path, contents).expect("write config");
let _guard = codex_utils_absolute_path::AbsolutePathBufGuard::new(tmp.path());
let error = codex_config::config_error_from_typed_toml::<ConfigToml>(&config_path, contents)
.expect("schema error");
let error =
codex_config::config_error_from_typed_toml(&config_path, contents).expect("schema error");
let value_line = contents.lines().nth(1).expect("value line");
let value_column = value_line.find("\"true\"").expect("value") + 1;
+1 -6
View File
@@ -1701,12 +1701,7 @@ async fn config_toml_from_layers(layers: &ConfigLayerStack) -> std::io::Result<C
match ConfigToml::try_from(layers.effective_config()) {
Ok(config_toml) => Ok(config_toml),
Err(err) => {
if let Some(config_error) = codex_config::first_layer_config_error::<ConfigToml>(
layers,
codex_config::CONFIG_TOML_FILE,
)
.await
{
if let Some(config_error) = codex_config::first_layer_config_error(layers).await {
return Err(codex_config::io_error_from_config_error(
std::io::ErrorKind::InvalidData,
config_error,