From 5f9837fc1ebe8459b904f42d354aeb42aa9505ac Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 2 Jan 2024 17:07:12 +0100 Subject: [PATCH 1/2] Add paths to toml parse errors **Summary** Previously, the information which toml failed to parse was missing in errors. **Before** ```console $ ruff check /home/konsti/projects/datasett ruff failed Cause: TOML parse error at line 12, column 8 | 12 | python "=3.9.2" | ^ expected `.`, `=` ``` **After** ```console $ ruff check /home/konsti/projects/datasett ruff failed Cause: Failed to parse /home/konsti/projects/datasett/datasett-0.0.1.tar.gz/datasett-0.0.1/pyproject.toml Cause: TOML parse error at line 12, column 8 | 12 | python "=3.9.2" | ^ expected `.`, `=` ``` --- crates/ruff_workspace/src/pyproject.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/ruff_workspace/src/pyproject.rs b/crates/ruff_workspace/src/pyproject.rs index 3fe7fe35bc3c6..c5d610f0c7fd7 100644 --- a/crates/ruff_workspace/src/pyproject.rs +++ b/crates/ruff_workspace/src/pyproject.rs @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf}; -use anyhow::Result; +use anyhow::{Context, Result}; use log::debug; use pep440_rs::VersionSpecifiers; use serde::{Deserialize, Serialize}; @@ -41,14 +41,18 @@ impl Pyproject { /// Parse a `ruff.toml` file. fn parse_ruff_toml>(path: P) -> Result { - let contents = std::fs::read_to_string(path)?; - toml::from_str(&contents).map_err(Into::into) + let contents = std::fs::read_to_string(path.as_ref()) + .with_context(|| format!("Failed to read {}", path.as_ref().display()))?; + toml::from_str(&contents) + .with_context(|| format!("Failed to parse {}", path.as_ref().display())) } /// Parse a `pyproject.toml` file. fn parse_pyproject_toml>(path: P) -> Result { - let contents = std::fs::read_to_string(path)?; - toml::from_str(&contents).map_err(Into::into) + let contents = std::fs::read_to_string(path.as_ref()) + .with_context(|| format!("Failed to read {}", path.as_ref().display()))?; + toml::from_str(&contents) + .with_context(|| format!("Failed to parse {}", path.as_ref().display())) } /// Return `true` if a `pyproject.toml` contains a `[tool.ruff]` section. From dcdae633953e7454531d155fb587423fee8a5916 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 2 Jan 2024 17:18:42 +0100 Subject: [PATCH 2/2] Update tests --- crates/ruff_cli/tests/format.rs | 3 ++- crates/ruff_cli/tests/integration_test.rs | 5 ++++- crates/ruff_workspace/src/resolver.rs | 3 +-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/ruff_cli/tests/format.rs b/crates/ruff_cli/tests/format.rs index f31b5bde78aa7..232635b8e3047 100644 --- a/crates/ruff_cli/tests/format.rs +++ b/crates/ruff_cli/tests/format.rs @@ -672,7 +672,8 @@ format = "json" ----- stderr ----- ruff failed - Cause: Failed to parse `[RUFF-TOML-PATH]`: TOML parse error at line 2, column 10 + Cause: Failed to parse [RUFF-TOML-PATH] + Cause: TOML parse error at line 2, column 10 | 2 | format = "json" | ^^^^^^ diff --git a/crates/ruff_cli/tests/integration_test.rs b/crates/ruff_cli/tests/integration_test.rs index 1ee634f6c09d3..f4793e7914b7e 100644 --- a/crates/ruff_cli/tests/integration_test.rs +++ b/crates/ruff_cli/tests/integration_test.rs @@ -1047,7 +1047,10 @@ fn unreadable_pyproject_toml() -> Result<()> { err.chain() .map(std::string::ToString::to_string) .collect::>(), - vec!["Permission denied (os error 13)".to_string()], + vec![ + format!("Failed to read {}/pyproject.toml", tempdir.path().display()), + "Permission denied (os error 13)".to_string() + ], ); Ok(()) } diff --git a/crates/ruff_workspace/src/resolver.rs b/crates/ruff_workspace/src/resolver.rs index 93df74b1e2aad..ac1f94294a9ff 100644 --- a/crates/ruff_workspace/src/resolver.rs +++ b/crates/ruff_workspace/src/resolver.rs @@ -220,8 +220,7 @@ fn resolve_configuration( } // Resolve the current path. - let options = pyproject::load_options(&path) - .map_err(|err| anyhow!("Failed to parse `{}`: {}", path.display(), err))?; + let options = pyproject::load_options(&path)?; let project_root = relativity.resolve(&path); let configuration = Configuration::from_options(options, &project_root)?;