Skip to content

Commit

Permalink
Revert uv-settings changes
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 14, 2024
1 parent b7207be commit 6cae887
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/uv-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ schemars = { workspace = true, optional = true }
serde = { workspace = true }
textwrap = { workspace = true }
thiserror = { workspace = true }
toml_edit = { workspace = true }
toml = { workspace = true }
tracing = { workspace = true }

[package.metadata.cargo-shear]
Expand Down
56 changes: 13 additions & 43 deletions crates/uv-settings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use serde::de::IntoDeserializer;
use serde::Deserialize;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use tracing::{debug, warn};

use uv_fs::Simplified;
Expand Down Expand Up @@ -74,18 +72,10 @@ impl FilesystemOptions {
Ok(None) => {
// Continue traversing the directory tree.
}
Err(Error::PyprojectTomlSchema(file, err)) => {
Err(Error::PyprojectToml(file, err)) => {
// If we see an invalid `pyproject.toml`, warn but continue.
warn!(
"Failed to parse `{}` during settings discovery due to invalid schema:\n{}",
file.cyan(),
textwrap::indent(&err.to_string(), " ")
);
}
Err(Error::PyprojectTomlSyntax(file, err)) => {
// If we see an invalid `pyproject.toml`, warn but continue.
warn!(
"Failed to parse `{}` during settings discovery due to invalid syntax:\n{}",
"Failed to parse `{}` during settings discovery:\n{}",
file.cyan(),
textwrap::indent(&err.to_string(), " ")
);
Expand All @@ -101,26 +91,20 @@ impl FilesystemOptions {

/// Load a [`FilesystemOptions`] from a directory, preferring a `uv.toml` file over a
/// `pyproject.toml` file.
fn from_directory(dir: impl AsRef<Path>) -> Result<Option<Self>, Error> {
pub fn from_directory(dir: impl AsRef<Path>) -> Result<Option<Self>, Error> {
// Read a `uv.toml` file in the current directory.
let path = dir.as_ref().join("uv.toml");
match fs_err::read_to_string(&path) {
Ok(content) => {
let options: toml_edit::ImDocument<_> =
toml_edit::ImDocument::from_str(&content)
.map_err(|err| Error::UvTomlSyntax(path.user_display().to_string(), err))?;
let options: Options = Options::deserialize(options.into_deserializer())
.map_err(|err| Error::UvTomlSchema(path.user_display().to_string(), err))?;
let options: Options = toml::from_str(&content)
.map_err(|err| Error::UvToml(path.user_display().to_string(), err))?;

// If the directory also contains a `[tool.uv]` table in a `pyproject.toml` file,
// warn.
let pyproject = dir.as_ref().join("pyproject.toml");
if let Some(pyproject) = fs_err::read_to_string(pyproject)
.ok()
.and_then(|content| toml_edit::ImDocument::from_str(&content).ok())
.and_then(|pyproject| {
PyProjectToml::deserialize(pyproject.into_deserializer()).ok()
})
.and_then(|content| toml::from_str::<PyProjectToml>(&content).ok())
{
if pyproject.tool.is_some_and(|tool| tool.uv.is_some()) {
warn!(
Expand All @@ -141,14 +125,8 @@ impl FilesystemOptions {
match fs_err::read_to_string(&path) {
Ok(content) => {
// Parse, but skip any `pyproject.toml` that doesn't have a `[tool.uv]` section.
let pyproject: toml_edit::ImDocument<_> = toml_edit::ImDocument::from_str(&content)
.map_err(|err| {
Error::PyprojectTomlSyntax(path.user_display().to_string(), err)
})?;
let pyproject: PyProjectToml =
PyProjectToml::deserialize(pyproject.into_deserializer()).map_err(|err| {
Error::PyprojectTomlSchema(path.user_display().to_string(), err)
})?;
let pyproject: PyProjectToml = toml::from_str(&content)
.map_err(|err| Error::PyprojectToml(path.user_display().to_string(), err))?;
let Some(tool) = pyproject.tool else {
debug!(
"Skipping `pyproject.toml` in `{}` (no `[tool]` section)",
Expand Down Expand Up @@ -210,10 +188,8 @@ fn config_dir() -> Option<PathBuf> {
/// Load [`Options`] from a `uv.toml` file.
fn read_file(path: &Path) -> Result<Options, Error> {
let content = fs_err::read_to_string(path)?;
let options: toml_edit::ImDocument<_> = toml_edit::ImDocument::from_str(&content)
.map_err(|err| Error::UvTomlSyntax(path.user_display().to_string(), err))?;
let options: Options = Options::deserialize(options.into_deserializer())
.map_err(|err| Error::UvTomlSchema(path.user_display().to_string(), err))?;
let options: Options = toml::from_str(&content)
.map_err(|err| Error::UvToml(path.user_display().to_string(), err))?;
Ok(options)
}

Expand All @@ -223,14 +199,8 @@ pub enum Error {
Io(#[from] std::io::Error),

#[error("Failed to parse: `{0}`")]
PyprojectTomlSyntax(String, #[source] toml_edit::TomlError),

#[error("Failed to parse: `{0}`")]
PyprojectTomlSchema(String, #[source] toml_edit::de::Error),

#[error("Failed to parse: `{0}`")]
UvTomlSyntax(String, #[source] toml_edit::TomlError),
PyprojectToml(String, #[source] toml::de::Error),

#[error("Failed to parse: `{0}`")]
UvTomlSchema(String, #[source] toml_edit::de::Error),
UvToml(String, #[source] toml::de::Error),
}
2 changes: 1 addition & 1 deletion crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ fn parse_pyproject_toml(
pyproject_path: &Path,
contents: &str,
) -> Result<PyProjectToml, WorkspaceError> {
let pyproject_toml: toml_edit::ImDocument<_> = toml_edit::ImDocument::from_str(&contents)
let pyproject_toml: toml_edit::ImDocument<_> = toml_edit::ImDocument::from_str(contents)
.map_err(|err| WorkspaceError::TomlSyntax(pyproject_path.to_path_buf(), Box::new(err)))?;
PyProjectToml::deserialize(pyproject_toml.into_deserializer()).map_err(|err| {
// TODO(konsti): A typed error would be nicer, this can break on toml upgrades.
Expand Down

0 comments on commit 6cae887

Please sign in to comment.