From 9fcddd441cfe6cd616cea46cb7f60bc5d4b5cac6 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 15 Apr 2024 19:27:27 -0400 Subject: [PATCH] Support --config-file support --- crates/uv-workspace/src/workspace.rs | 26 ++++++++++++++++++++++++-- crates/uv/src/cli.rs | 4 ++++ crates/uv/src/main.rs | 5 +++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/uv-workspace/src/workspace.rs b/crates/uv-workspace/src/workspace.rs index bbb496edc0093..3167b5d2ca33a 100644 --- a/crates/uv-workspace/src/workspace.rs +++ b/crates/uv-workspace/src/workspace.rs @@ -19,7 +19,7 @@ impl Workspace { /// found. pub fn find(path: impl AsRef) -> Result, WorkspaceError> { for ancestor in path.as_ref().ancestors() { - if let Some(options) = read_options(ancestor)? { + if let Some(options) = find_in_directory(ancestor)? { return Ok(Some(Self { options, root: ancestor.to_path_buf(), @@ -28,10 +28,18 @@ impl Workspace { } Ok(None) } + + /// Load a [`Workspace`] from a `pyproject.toml` or `uv.toml` file. + pub fn from_file(path: impl AsRef) -> Result { + Ok(Self { + options: read_file(path.as_ref())?, + root: path.as_ref().parent().unwrap().to_path_buf(), + }) + } } /// Read a `uv.toml` or `pyproject.toml` file in the given directory. -fn read_options(dir: &Path) -> Result, WorkspaceError> { +fn find_in_directory(dir: &Path) -> Result, WorkspaceError> { // Read a `uv.toml` file in the current directory. let path = dir.join("uv.toml"); match fs_err::read_to_string(&path) { @@ -59,6 +67,20 @@ fn read_options(dir: &Path) -> Result, WorkspaceError> { Ok(None) } +/// Load [`Options`] from a `pyproject.toml` or `ruff.toml` file. +fn read_file(path: &Path) -> Result { + let content = fs_err::read_to_string(path)?; + if path.ends_with("pyproject.toml") { + let pyproject: PyProjectToml = toml::from_str(&content) + .map_err(|err| WorkspaceError::Toml(path.user_display().to_string(), err))?; + Ok(pyproject.tool.and_then(|tool| tool.uv).unwrap_or_default()) + } else { + let options: Options = toml::from_str(&content) + .map_err(|err| WorkspaceError::Toml(path.user_display().to_string(), err))?; + Ok(options) + } +} + #[derive(thiserror::Error, Debug)] pub enum WorkspaceError { #[error(transparent)] diff --git a/crates/uv/src/cli.rs b/crates/uv/src/cli.rs index 6a5498f4884fc..c69c7c97e8a23 100644 --- a/crates/uv/src/cli.rs +++ b/crates/uv/src/cli.rs @@ -30,6 +30,10 @@ pub(crate) struct Cli { #[command(flatten)] pub(crate) cache_args: CacheArgs, + + /// The path to a `pyproject.toml` or `uv.toml` file to use for configuration. + #[arg(long, short, env = "UV_CONFIG_FILE")] + pub(crate) config_file: Option, } #[derive(Parser, Debug, Clone)] diff --git a/crates/uv/src/main.rs b/crates/uv/src/main.rs index f40c64374a901..587a8bc331dff 100644 --- a/crates/uv/src/main.rs +++ b/crates/uv/src/main.rs @@ -108,6 +108,11 @@ async fn run() -> Result { }; // Load the workspace settings. + let _workspace = if let Some(config_file) = cli.config_file.as_ref() { + Some(uv_workspace::Workspace::from_file(config_file)?) + } else { + uv_workspace::Workspace::find(env::current_dir()?)? + }; let workspace = uv_workspace::Workspace::find(env::current_dir()?)?; // Resolve the global settings.