mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
better error messages while loading configuration extend
s (#15658)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
0babbca43f
commit
9304fdf4ec
5 changed files with 182 additions and 16 deletions
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue