diff --git a/crates/ruff/src/pyproject_toml.rs b/crates/ruff/src/pyproject_toml.rs index cabdff1b39..a0cfeb961b 100644 --- a/crates/ruff/src/pyproject_toml.rs +++ b/crates/ruff/src/pyproject_toml.rs @@ -7,7 +7,9 @@ use ruff_diagnostics::Diagnostic; use ruff_python_ast::source_code::SourceFile; use crate::message::Message; +use crate::registry::Rule; use crate::rules::ruff::rules::InvalidPyprojectToml; +use crate::settings::Settings; use crate::IOError; /// Unlike [`pyproject_toml::PyProjectToml`], in our case `build_system` is also optional @@ -20,9 +22,11 @@ struct PyProjectToml { project: Option, } -pub fn lint_pyproject_toml(source_file: SourceFile) -> Result> { +pub fn lint_pyproject_toml(source_file: SourceFile, settings: &Settings) -> Result> { + let mut messages = vec![]; + let err = match toml::from_str::(source_file.source_text()) { - Ok(_) => return Ok(Vec::default()), + Ok(_) => return Ok(messages), Err(err) => err, }; @@ -32,17 +36,20 @@ pub fn lint_pyproject_toml(source_file: SourceFile) -> Result> { None => TextRange::default(), Some(range) => { let Ok(end) = TextSize::try_from(range.end) else { - let diagnostic = Diagnostic::new( - IOError { - message: "pyproject.toml is larger than 4GB".to_string(), - }, - TextRange::default(), - ); - return Ok(vec![Message::from_diagnostic( - diagnostic, - source_file, - TextSize::default(), - )]); + if settings.rules.enabled(Rule::IOError) { + let diagnostic = Diagnostic::new( + IOError { + message: "pyproject.toml is larger than 4GB".to_string(), + }, + TextRange::default(), + ); + messages.push(Message::from_diagnostic( + diagnostic, + source_file, + TextSize::default(), + )); + } + return Ok(messages); }; TextRange::new( // start <= end, so if end < 4GB follows start < 4GB @@ -52,11 +59,15 @@ pub fn lint_pyproject_toml(source_file: SourceFile) -> Result> { } }; - let toml_err = err.message().to_string(); - let diagnostic = Diagnostic::new(InvalidPyprojectToml { message: toml_err }, range); - Ok(vec![Message::from_diagnostic( - diagnostic, - source_file, - TextSize::default(), - )]) + if settings.rules.enabled(Rule::InvalidPyprojectToml) { + let toml_err = err.message().to_string(); + let diagnostic = Diagnostic::new(InvalidPyprojectToml { message: toml_err }, range); + messages.push(Message::from_diagnostic( + diagnostic, + source_file, + TextSize::default(), + )); + } + + Ok(messages) } diff --git a/crates/ruff/src/registry.rs b/crates/ruff/src/registry.rs index 56e30db0ff..a6ac161b7b 100644 --- a/crates/ruff/src/registry.rs +++ b/crates/ruff/src/registry.rs @@ -252,6 +252,7 @@ pub enum LintSource { Imports, Noqa, Filesystem, + PyprojectToml, } impl Rule { @@ -259,6 +260,7 @@ impl Rule { /// physical lines). pub const fn lint_source(&self) -> LintSource { match self { + Rule::InvalidPyprojectToml => LintSource::PyprojectToml, Rule::UnusedNOQA => LintSource::Noqa, Rule::BlanketNOQA | Rule::BlanketTypeIgnore diff --git a/crates/ruff/src/rules/ruff/mod.rs b/crates/ruff/src/rules/ruff/mod.rs index a110328a24..336a65c7e1 100644 --- a/crates/ruff/src/rules/ruff/mod.rs +++ b/crates/ruff/src/rules/ruff/mod.rs @@ -203,7 +203,10 @@ mod tests { .join("pyproject.toml"); let contents = fs::read_to_string(path)?; let source_file = SourceFileBuilder::new("pyproject.toml", contents).finish(); - let messages = lint_pyproject_toml(source_file)?; + let messages = lint_pyproject_toml( + source_file, + &settings::Settings::for_rule(Rule::InvalidPyprojectToml), + )?; assert_messages!(snapshot, messages); Ok(()) } diff --git a/crates/ruff_cli/src/diagnostics.rs b/crates/ruff_cli/src/diagnostics.rs index fe638ad169..f994b7a055 100644 --- a/crates/ruff_cli/src/diagnostics.rs +++ b/crates/ruff_cli/src/diagnostics.rs @@ -127,11 +127,20 @@ pub(crate) fn lint_path( debug!("Checking: {}", path.display()); - // We have to special case this here since the python tokenizer doesn't work with toml + // We have to special case this here since the Python tokenizer doesn't work with TOML. if is_project_toml(path) { - let contents = std::fs::read_to_string(path)?; - let source_file = SourceFileBuilder::new(path.to_string_lossy(), contents).finish(); - let messages = lint_pyproject_toml(source_file)?; + let messages = if settings + .lib + .rules + .iter_enabled() + .any(|rule_code| rule_code.lint_source().is_pyproject_toml()) + { + let contents = std::fs::read_to_string(path)?; + let source_file = SourceFileBuilder::new(path.to_string_lossy(), contents).finish(); + lint_pyproject_toml(source_file, &settings.lib)? + } else { + vec![] + }; return Ok(Diagnostics { messages, ..Diagnostics::default()