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 a07e70d commit a90a309
Show file tree
Hide file tree
Showing 5 changed files with 30 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}` during settings discovery; skipping...");
}
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
1 change: 1 addition & 0 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3204,6 +3204,7 @@ fn override_dependency_from_workspace_invalid_syntax() -> Result<()> {
----- stdout -----
----- stderr -----
warning: Failed to parse `pyproject.toml` during settings discovery; skipping...
error: Failed to parse: `pyproject.toml`
Caused by: TOML parse error at line 9, column 29
|
Expand Down
1 change: 1 addition & 0 deletions crates/uv/tests/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fn invalid_pyproject_toml_syntax() -> Result<()> {
----- stdout -----
----- stderr -----
warning: Failed to parse `pyproject.toml` during settings discovery; skipping...
error: Failed to parse: `pyproject.toml`
Caused by: TOML parse error at line 1, column 5
|
Expand Down

0 comments on commit a90a309

Please sign in to comment.