Skip to content

Commit

Permalink
Remove serde(flatten) to improve error messages
Browse files Browse the repository at this point in the history
Apparently toml (or serde) fails to generate spans when using `#[serde(flatten)]`.

New error message for #1615:

```
💥 maturin failed
  Caused by: pyproject.toml at /home/konsti/maturin/asdf/pyproject.toml is invalid
  Caused by: TOML parse error at line 7, column 17
  |
7 | license-files = [ "license.txt",]
  |                 ^^^^^^^^^^^^^^^^^
wanted string or table
```

Fixes #1615
  • Loading branch information
konstin committed May 18, 2023
1 parent 7237fac commit 26606da
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ impl ProjectResolver {

let manifest_dir = manifest_file.parent().unwrap();
let pyproject_toml: Option<PyProjectToml> = if pyproject_file.is_file() {
let pyproject =
PyProjectToml::new(&pyproject_file).context("pyproject.toml is invalid")?;
let pyproject = PyProjectToml::new(&pyproject_file)?;
pyproject.warn_missing_maturin_version();
pyproject.warn_missing_build_backend();
Some(pyproject)
Expand Down Expand Up @@ -244,8 +243,7 @@ impl ProjectResolver {
"Found pyproject.toml in working directory at {:?}",
pyproject_file
);
let pyproject =
PyProjectToml::new(&pyproject_file).context("pyproject.toml is invalid")?;
let pyproject = PyProjectToml::new(&pyproject_file)?;
if let Some(path) = pyproject.manifest_path() {
debug!("Using cargo manifest path from pyproject.toml {:?}", path);
return Ok((
Expand Down
66 changes: 52 additions & 14 deletions src/pyproject_toml.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! A pyproject.toml as specified in PEP 517
use crate::PlatformTag;
use anyhow::{format_err, Result};
use anyhow::{Context, Result};
use fs_err as fs;
use pyproject_toml::PyProjectToml as ProjectToml;
use pyproject_toml::{BuildSystem, Project};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -179,8 +179,10 @@ pub struct ToolMaturin {
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct PyProjectToml {
#[serde(flatten)]
inner: ProjectToml,
/// Build-related data
pub build_system: BuildSystem,
/// Project metadata
pub project: Option<Project>,
/// PEP 518: The `[tool]` table is where any tool related to your Python project, not just build
/// tools, can have users specify configuration data as long as they use a sub-table within
/// `[tool]`, e.g. the flit tool would store its configuration in `[tool.flit]`.
Expand All @@ -189,14 +191,6 @@ pub struct PyProjectToml {
pub tool: Option<Tool>,
}

impl std::ops::Deref for PyProjectToml {
type Target = ProjectToml;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl PyProjectToml {
/// Returns the contents of a pyproject.toml with a `[build-system]` entry or an error
///
Expand All @@ -205,8 +199,12 @@ impl PyProjectToml {
pub fn new(pyproject_file: impl AsRef<Path>) -> Result<PyProjectToml> {
let path = pyproject_file.as_ref();
let contents = fs::read_to_string(path)?;
let pyproject: PyProjectToml = toml::from_str(&contents)
.map_err(|err| format_err!("pyproject.toml is not PEP 517 compliant: {}", err))?;
let pyproject = toml::from_str(&contents).with_context(|| {
format!(
"pyproject.toml at {} is invalid",
pyproject_file.as_ref().display()
)
})?;
Ok(pyproject)
}

Expand Down Expand Up @@ -355,6 +353,7 @@ mod tests {
PyProjectToml,
};
use fs_err as fs;
use indoc::indoc;
use pretty_assertions::assert_eq;
use std::path::Path;
use tempfile::TempDir;
Expand Down Expand Up @@ -497,4 +496,43 @@ mod tests {
])
);
}

#[test]
fn test_gh_1615() {
let source = indoc!(
r#"[build-system]
requires = [ "maturin>=0.14", "numpy", "wheel", "patchelf",]
build-backend = "maturin"
[project]
name = "..."
license-files = [ "license.txt",]
requires-python = ">=3.8"
requires-dist = [ "maturin>=0.14", "...",]
dependencies = [ "packaging", "...",]
zip-safe = false
version = "..."
readme = "..."
description = "..."
classifiers = [ "...",]
"#
);
let temp_dir = TempDir::new().unwrap();
let pyproject_toml = temp_dir.path().join("pyproject.toml");
fs::write(&pyproject_toml, source).unwrap();
let outer_error = PyProjectToml::new(&pyproject_toml).unwrap_err();
let inner_error = outer_error.source().unwrap();

assert_eq!(
inner_error.to_string(),
indoc!(
r#"TOML parse error at line 7, column 17
|
7 | license-files = [ "license.txt",]
| ^^^^^^^^^^^^^^^^^
wanted string or table
"#
)
);
}
}

0 comments on commit 26606da

Please sign in to comment.