better error messages while loading configuration extends (#15658)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
purajit 2025-02-17 01:35:30 -08:00 committed by GitHub
parent 0babbca43f
commit 9304fdf4ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 182 additions and 16 deletions

View file

@ -21,12 +21,13 @@ ruff_macros = { workspace = true }
ruff_python_ast = { workspace = true }
ruff_python_formatter = { workspace = true, features = ["serde"] }
ruff_python_semantic = { workspace = true, features = ["serde"] }
ruff_python_stdlib = {workspace = true}
ruff_python_stdlib = { workspace = true }
ruff_source_file = { workspace = true }
anyhow = { workspace = true }
colored = { workspace = true }
ignore = { workspace = true }
indexmap = { workspace = true }
is-macro = { workspace = true }
itertools = { workspace = true }
log = { workspace = true }
@ -58,7 +59,12 @@ ignored = ["colored"]
[features]
default = []
schemars = ["dep:schemars", "ruff_formatter/schemars", "ruff_python_formatter/schemars", "ruff_python_semantic/schemars"]
schemars = [
"dep:schemars",
"ruff_formatter/schemars",
"ruff_python_formatter/schemars",
"ruff_python_semantic/schemars",
]
[lints]
workspace = true

View file

@ -7,8 +7,8 @@ use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use anyhow::Result;
use anyhow::{anyhow, bail};
use anyhow::{Context, Result};
use globset::{Candidate, GlobSet};
use ignore::{DirEntry, Error, ParallelVisitor, WalkBuilder, WalkState};
use itertools::Itertools;
@ -304,16 +304,39 @@ pub fn resolve_configuration(
relativity: Relativity,
transformer: &dyn ConfigurationTransformer,
) -> Result<Configuration> {
let mut seen = FxHashSet::default();
let mut stack = vec![];
let mut configurations = indexmap::IndexMap::new();
let mut next = Some(fs::normalize_path(pyproject));
while let Some(path) = next {
if seen.contains(&path) {
bail!("Circular dependency detected in pyproject.toml");
if configurations.contains_key(&path) {
bail!(format!(
"Circular configuration detected: {chain}",
chain = configurations
.keys()
.chain([&path])
.map(|p| format!("`{}`", p.display()))
.join(" extends "),
));
}
// Resolve the current path.
let options = pyproject::load_options(&path)?;
let options = pyproject::load_options(&path).with_context(|| {
if configurations.is_empty() {
format!(
"Failed to load configuration `{path}`",
path = path.display()
)
} else {
let chain = configurations
.keys()
.chain([&path])
.map(|p| format!("`{}`", p.display()))
.join(" extends ");
format!(
"Failed to load extended configuration `{path}` ({chain})",
path = path.display()
)
}
})?;
let project_root = relativity.resolve(&path);
let configuration = Configuration::from_options(options, Some(&path), project_root)?;
@ -329,14 +352,13 @@ pub fn resolve_configuration(
// Keep track of (1) the paths we've already resolved (to avoid cycles), and (2)
// the base configuration for every path.
seen.insert(path);
stack.push(configuration);
configurations.insert(path, configuration);
}
// Merge the configurations, in order.
stack.reverse();
let mut configuration = stack.pop().unwrap();
while let Some(extend) = stack.pop() {
let mut configurations = configurations.into_values();
let mut configuration = configurations.next().unwrap();
for extend in configurations {
configuration = configuration.combine(extend);
}
Ok(transformer.transform(configuration))