Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paths to toml parse errors #9358

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/ruff_cli/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
| ^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,10 @@ fn unreadable_pyproject_toml() -> Result<()> {
err.chain()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>(),
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(())
}
Expand Down
14 changes: 9 additions & 5 deletions crates/ruff_workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -41,14 +41,18 @@ impl Pyproject {

/// Parse a `ruff.toml` file.
fn parse_ruff_toml<P: AsRef<Path>>(path: P) -> Result<Options> {
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<P: AsRef<Path>>(path: P) -> Result<Pyproject> {
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.
Expand Down
3 changes: 1 addition & 2 deletions crates/ruff_workspace/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
Loading