diff --git a/crates/uv-workspace/src/workspace.rs b/crates/uv-workspace/src/workspace.rs index daac9562f..ee1ee3889 100644 --- a/crates/uv-workspace/src/workspace.rs +++ b/crates/uv-workspace/src/workspace.rs @@ -21,7 +21,7 @@ impl Workspace { /// found. pub fn find(path: impl AsRef) -> Result, WorkspaceError> { for ancestor in path.as_ref().ancestors() { - match read_options(ancestor) { + match find_in_directory(ancestor) { Ok(Some(options)) => { return Ok(Some(Self { options, @@ -43,10 +43,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) { @@ -94,6 +102,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::PyprojectToml(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::UvToml(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 86176b759..6da061d3a 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", hide = true)] + pub(crate) config_file: Option, } #[derive(Parser, Debug, Clone)] diff --git a/crates/uv/src/main.rs b/crates/uv/src/main.rs index f327946f6..9ca1f9ae6 100644 --- a/crates/uv/src/main.rs +++ b/crates/uv/src/main.rs @@ -110,7 +110,11 @@ async fn run() -> Result { }; // Load the workspace settings. - let workspace = uv_workspace::Workspace::find(env::current_dir()?)?; + 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()?)? + }; // Resolve the global settings. let globals = GlobalSettings::resolve(cli.global_args, workspace.as_ref());