Skip to content

Commit

Permalink
Surface dedicated project.name error for workspaces (#7399)
Browse files Browse the repository at this point in the history
## Summary

An extension of #6803 to cover `uv
run`.
  • Loading branch information
charliermarsh authored Sep 14, 2024
1 parent 4aad89c commit e07281d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/pypi-types/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum MetadataError {
MailParse(#[from] MailParseError),
#[error("Invalid `pyproject.toml`")]
InvalidPyprojectTomlSyntax(#[source] toml_edit::TomlError),
#[error("`pyproject.toml` is using the `[project]` table, but the required `project.name` is not set.")]
#[error("`pyproject.toml` is using the `[project]` table, but the required `project.name` field is not set.")]
InvalidPyprojectTomlMissingName(#[source] toml_edit::de::Error),
#[error(transparent)]
InvalidPyprojectTomlSchema(toml_edit::de::Error),
Expand Down
30 changes: 25 additions & 5 deletions crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
//!
//! Then lowers them into a dependency specification.
use glob::Pattern;
use serde::{de::IntoDeserializer, Deserialize, Serialize};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{collections::BTreeMap, mem};

use glob::Pattern;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use url::Url;

Expand All @@ -22,6 +22,16 @@ use uv_git::GitReference;
use uv_macros::OptionsMetadata;
use uv_normalize::{ExtraName, PackageName};

#[derive(Error, Debug)]
pub enum PyprojectTomlError {
#[error(transparent)]
TomlSyntax(#[from] toml_edit::TomlError),
#[error(transparent)]
TomlSchema(#[from] toml_edit::de::Error),
#[error("`pyproject.toml` is using the `[project]` table, but the required `project.name` field is not set")]
MissingName(#[source] toml_edit::de::Error),
}

/// A `pyproject.toml` as specified in PEP 517.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
Expand All @@ -41,8 +51,18 @@ pub struct PyProjectToml {

impl PyProjectToml {
/// Parse a `PyProjectToml` from a raw TOML string.
pub fn from_string(raw: String) -> Result<Self, toml::de::Error> {
let pyproject = toml::from_str(&raw)?;
pub fn from_string(raw: String) -> Result<Self, PyprojectTomlError> {
let pyproject: toml_edit::ImDocument<_> =
toml_edit::ImDocument::from_str(&raw).map_err(PyprojectTomlError::TomlSyntax)?;
let pyproject =
PyProjectToml::deserialize(pyproject.into_deserializer()).map_err(|err| {
// TODO(konsti): A typed error would be nicer, this can break on toml upgrades.
if err.message().contains("missing field `name`") {
PyprojectTomlError::MissingName(err)
} else {
PyprojectTomlError::TomlSchema(err)
}
})?;
Ok(PyProjectToml { raw, ..pyproject })
}

Expand Down
16 changes: 8 additions & 8 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Resolve the current [`ProjectWorkspace`] or [`Workspace`].
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use either::Either;
use glob::{glob, GlobError, PatternError};
use rustc_hash::FxHashSet;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use tracing::{debug, trace, warn};

use pep508_rs::{MarkerTree, RequirementOrigin, VerbatimUrl};
Expand All @@ -14,7 +13,9 @@ use uv_fs::{Simplified, CWD};
use uv_normalize::{GroupName, PackageName, DEV_DEPENDENCIES};
use uv_warnings::{warn_user, warn_user_once};

use crate::pyproject::{Project, PyProjectToml, Source, ToolUvSources, ToolUvWorkspace};
use crate::pyproject::{
Project, PyProjectToml, PyprojectTomlError, Source, ToolUvSources, ToolUvWorkspace,
};

#[derive(thiserror::Error, Debug)]
pub enum WorkspaceError {
Expand All @@ -39,7 +40,7 @@ pub enum WorkspaceError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Failed to parse: `{}`", _0.user_display())]
Toml(PathBuf, #[source] Box<toml::de::Error>),
Toml(PathBuf, #[source] Box<PyprojectTomlError>),
#[error("Failed to normalize workspace member path")]
Normalize(#[source] std::io::Error),
}
Expand Down Expand Up @@ -120,7 +121,7 @@ impl Workspace {

let pyproject_path = project_path.join("pyproject.toml");
let contents = fs_err::tokio::read_to_string(&pyproject_path).await?;
let pyproject_toml = PyProjectToml::from_string(contents.clone())
let pyproject_toml = PyProjectToml::from_string(contents)
.map_err(|err| WorkspaceError::Toml(pyproject_path.clone(), Box::new(err)))?;

// Check if the project is explicitly marked as unmanaged.
Expand Down Expand Up @@ -588,7 +589,7 @@ impl Workspace {
let pyproject_path = workspace_root.join("pyproject.toml");
let contents = fs_err::read_to_string(&pyproject_path)?;
let pyproject_toml = PyProjectToml::from_string(contents)
.map_err(|err| WorkspaceError::Toml(pyproject_path, Box::new(err)))?;
.map_err(|err| WorkspaceError::Toml(pyproject_path.clone(), Box::new(err)))?;

debug!(
"Adding root workspace member: `{}`",
Expand Down Expand Up @@ -699,7 +700,6 @@ impl Workspace {
}
Err(err) => return Err(err.into()),
};

let pyproject_toml = PyProjectToml::from_string(contents)
.map_err(|err| WorkspaceError::Toml(pyproject_path.clone(), Box::new(err)))?;

Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12729,7 +12729,7 @@ fn lock_invalid_project_table() -> Result<()> {
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
error: Failed to build: `b @ file://[TEMP_DIR]/b`
Caused by: Failed to extract static metadata from `pyproject.toml`
Caused by: `pyproject.toml` is using the `[project]` table, but the required `project.name` is not set.
Caused by: `pyproject.toml` is using the `[project]` table, but the required `project.name` field is not set.
Caused by: TOML parse error at line 2, column 10
|
2 | [project.urls]
Expand Down
3 changes: 2 additions & 1 deletion crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,7 @@ fn run_exit_code() -> Result<()> {
}

#[test]
fn run_lock_invalid_project_table() -> Result<()> {
fn run_invalid_project_table() -> Result<()> {
let context = TestContext::new_with_versions(&["3.12", "3.11", "3.8"]);

let pyproject_toml = context.temp_dir.child("pyproject.toml");
Expand All @@ -1939,6 +1939,7 @@ fn run_lock_invalid_project_table() -> Result<()> {
----- stderr -----
error: Failed to parse: `pyproject.toml`
Caused by: `pyproject.toml` is using the `[project]` table, but the required `project.name` field is not set
Caused by: TOML parse error at line 1, column 2
|
1 | [project.urls]
Expand Down

0 comments on commit e07281d

Please sign in to comment.