Skip to content

Commit

Permalink
Read persistent configuration from non-workspace pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 25, 2024
1 parent e39f5f7 commit 3e0a146
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/uv-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ pep508_rs = { workspace = true }
pypi-types = { workspace = true }
uv-configuration = { workspace = true, features = ["schemars"] }
uv-fs = { workspace = true }
uv-macros = { workspace = true }
uv-normalize = { workspace = true, features = ["schemars"] }
uv-resolver = { workspace = true, features = ["schemars"] }
uv-toolchain = { workspace = true, features = ["schemars"] }
uv-macros = { workspace = true }
uv-warnings = { workspace = true }

dirs-sys = { workspace = true }
fs-err = { workspace = true }
Expand Down
38 changes: 25 additions & 13 deletions crates/uv-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
use tracing::debug;

use uv_fs::Simplified;
use uv_warnings::warn_user;

pub use crate::combine::*;
pub use crate::settings::*;
Expand Down Expand Up @@ -57,22 +58,25 @@ impl FilesystemOptions {

/// Find the [`FilesystemOptions`] for the given path.
///
/// The search starts at the given path and goes up the directory tree until a `uv.toml` file is
/// found.
/// The search starts at the given path and goes up the directory tree until a `uv.toml` file or
/// `pyproject.toml` file is found.
pub fn find(path: impl AsRef<Path>) -> Result<Option<Self>, Error> {
for ancestor in path.as_ref().ancestors() {
// Read a `uv.toml` file in the current directory.
let path = ancestor.join("uv.toml");
match fs_err::read_to_string(&path) {
Ok(content) => {
let options: Options = toml::from_str(&content)
.map_err(|err| Error::UvToml(path.user_display().to_string(), err))?;

debug!("Found workspace configuration at `{}`", path.display());
return Ok(Some(Self(options)));
match Self::from_directory(ancestor) {
Ok(Some(options)) => {
return Ok(Some(options));
}
Ok(None) => {
// Continue traversing the directory tree.
}
Err(Error::PyprojectToml(file, err)) => {
// If we see an invalid `pyproject.toml`, warn but continue.
warn_user!("Failed to parse `{file}`: {err}");
}
Err(err) => {
// Otherwise, warn and stop.
return Err(err);
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => return Err(err.into()),
}
}
Ok(None)
Expand Down Expand Up @@ -103,9 +107,17 @@ impl FilesystemOptions {
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)",
dir.as_ref().display()
);
return Ok(None);
};
let Some(options) = tool.uv else {
debug!(
"Skipping `pyproject.toml` in `{}` (no `[tool.uv]` section)",
dir.as_ref().display()
);
return Ok(None);
};

Expand Down

0 comments on commit 3e0a146

Please sign in to comment.