mirror of
https://github.com/astral-sh/uv.git
synced 2025-08-01 01:22:18 +00:00
Make --directory
a global argument (#5579)
## Summary Cargo makes this global (and uses the same technique). It's still hidden so we can always decide to remove it.
This commit is contained in:
parent
3e329029bc
commit
c46adee48d
7 changed files with 18 additions and 59 deletions
|
@ -164,6 +164,10 @@ pub struct GlobalArgs {
|
||||||
/// Hides all progress outputs when set
|
/// Hides all progress outputs when set
|
||||||
#[arg(global = true, long)]
|
#[arg(global = true, long)]
|
||||||
pub no_progress: bool,
|
pub no_progress: bool,
|
||||||
|
|
||||||
|
/// Change to the given directory prior to running the command.
|
||||||
|
#[arg(global = true, long, hide = true)]
|
||||||
|
pub directory: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, clap::ValueEnum)]
|
#[derive(Debug, Copy, Clone, clap::ValueEnum)]
|
||||||
|
@ -1932,10 +1936,6 @@ pub struct RunArgs {
|
||||||
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
|
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
|
||||||
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
|
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
|
||||||
pub python: Option<String>,
|
pub python: Option<String>,
|
||||||
|
|
||||||
/// The path to the project. Defaults to the current working directory.
|
|
||||||
#[arg(long, hide = true)]
|
|
||||||
pub directory: Option<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
|
@ -2000,10 +2000,6 @@ pub struct SyncArgs {
|
||||||
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
|
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
|
||||||
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
|
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
|
||||||
pub python: Option<String>,
|
pub python: Option<String>,
|
||||||
|
|
||||||
/// The path to the project. Defaults to the current working directory.
|
|
||||||
#[arg(long, hide = true)]
|
|
||||||
pub directory: Option<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
|
@ -2039,10 +2035,6 @@ pub struct LockArgs {
|
||||||
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
|
/// - `/home/ferris/.local/bin/python3.10` uses the exact Python at the given path.
|
||||||
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
|
#[arg(long, short, env = "UV_PYTHON", verbatim_doc_comment)]
|
||||||
pub python: Option<String>,
|
pub python: Option<String>,
|
||||||
|
|
||||||
/// The path to the project. Defaults to the current working directory.
|
|
||||||
#[arg(long, hide = true)]
|
|
||||||
pub directory: Option<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use anstream::eprint;
|
use anstream::eprint;
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
@ -17,7 +16,7 @@ use uv_client::{Connectivity, FlatIndexClient, RegistryClientBuilder};
|
||||||
use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode, Reinstall, SetupPyStrategy};
|
use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode, Reinstall, SetupPyStrategy};
|
||||||
use uv_dispatch::BuildDispatch;
|
use uv_dispatch::BuildDispatch;
|
||||||
use uv_distribution::DEV_DEPENDENCIES;
|
use uv_distribution::DEV_DEPENDENCIES;
|
||||||
use uv_fs::{Simplified, CWD};
|
use uv_fs::CWD;
|
||||||
use uv_git::ResolvedRepositoryReference;
|
use uv_git::ResolvedRepositoryReference;
|
||||||
use uv_normalize::PackageName;
|
use uv_normalize::PackageName;
|
||||||
use uv_python::{Interpreter, PythonFetch, PythonPreference, PythonRequest};
|
use uv_python::{Interpreter, PythonFetch, PythonPreference, PythonRequest};
|
||||||
|
@ -49,7 +48,6 @@ pub(crate) async fn lock(
|
||||||
frozen: bool,
|
frozen: bool,
|
||||||
python: Option<String>,
|
python: Option<String>,
|
||||||
settings: ResolverSettings,
|
settings: ResolverSettings,
|
||||||
directory: Option<PathBuf>,
|
|
||||||
preview: PreviewMode,
|
preview: PreviewMode,
|
||||||
python_preference: PythonPreference,
|
python_preference: PythonPreference,
|
||||||
python_fetch: PythonFetch,
|
python_fetch: PythonFetch,
|
||||||
|
@ -63,14 +61,8 @@ pub(crate) async fn lock(
|
||||||
warn_user_once!("`uv lock` is experimental and may change without warning");
|
warn_user_once!("`uv lock` is experimental and may change without warning");
|
||||||
}
|
}
|
||||||
|
|
||||||
let directory = if let Some(directory) = directory {
|
|
||||||
directory.simple_canonicalize()?
|
|
||||||
} else {
|
|
||||||
CWD.to_path_buf()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Find the project requirements.
|
// Find the project requirements.
|
||||||
let workspace = Workspace::discover(&directory, &DiscoveryOptions::default()).await?;
|
let workspace = Workspace::discover(&CWD, &DiscoveryOptions::default()).await?;
|
||||||
|
|
||||||
// Find an interpreter for the project
|
// Find an interpreter for the project
|
||||||
let interpreter = FoundInterpreter::discover(
|
let interpreter = FoundInterpreter::discover(
|
||||||
|
|
|
@ -44,7 +44,6 @@ pub(crate) async fn run(
|
||||||
extras: ExtrasSpecification,
|
extras: ExtrasSpecification,
|
||||||
dev: bool,
|
dev: bool,
|
||||||
python: Option<String>,
|
python: Option<String>,
|
||||||
directory: Option<PathBuf>,
|
|
||||||
settings: ResolverInstallerSettings,
|
settings: ResolverInstallerSettings,
|
||||||
isolated: bool,
|
isolated: bool,
|
||||||
preview: PreviewMode,
|
preview: PreviewMode,
|
||||||
|
@ -90,12 +89,6 @@ pub(crate) async fn run(
|
||||||
|
|
||||||
let reporter = PythonDownloadReporter::single(printer);
|
let reporter = PythonDownloadReporter::single(printer);
|
||||||
|
|
||||||
let directory = if let Some(directory) = directory {
|
|
||||||
directory.simple_canonicalize()?
|
|
||||||
} else {
|
|
||||||
CWD.to_path_buf()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Determine whether the command to execute is a PEP 723 script.
|
// Determine whether the command to execute is a PEP 723 script.
|
||||||
let script_interpreter = if let RunCommand::Python(target, _) = &command {
|
let script_interpreter = if let RunCommand::Python(target, _) = &command {
|
||||||
if let Some(metadata) = uv_scripts::read_pep723_metadata(&target).await? {
|
if let Some(metadata) = uv_scripts::read_pep723_metadata(&target).await? {
|
||||||
|
@ -109,7 +102,7 @@ pub(crate) async fn run(
|
||||||
let python_request = if let Some(request) = python.as_deref() {
|
let python_request = if let Some(request) = python.as_deref() {
|
||||||
Some(PythonRequest::parse(request))
|
Some(PythonRequest::parse(request))
|
||||||
// (2) Request from `.python-version`
|
// (2) Request from `.python-version`
|
||||||
} else if let Some(request) = request_from_version_file(&directory).await? {
|
} else if let Some(request) = request_from_version_file(&CWD).await? {
|
||||||
Some(request)
|
Some(request)
|
||||||
// (3) `Requires-Python` in `pyproject.toml`
|
// (3) `Requires-Python` in `pyproject.toml`
|
||||||
} else {
|
} else {
|
||||||
|
@ -174,13 +167,13 @@ pub(crate) async fn run(
|
||||||
// We need a workspace, but we don't need to have a current package, we can be e.g. in
|
// We need a workspace, but we don't need to have a current package, we can be e.g. in
|
||||||
// the root of a virtual workspace and then switch into the selected package.
|
// the root of a virtual workspace and then switch into the selected package.
|
||||||
Some(VirtualProject::Project(
|
Some(VirtualProject::Project(
|
||||||
Workspace::discover(&directory, &DiscoveryOptions::default())
|
Workspace::discover(&CWD, &DiscoveryOptions::default())
|
||||||
.await?
|
.await?
|
||||||
.with_current_project(package.clone())
|
.with_current_project(package.clone())
|
||||||
.with_context(|| format!("Package `{package}` not found in workspace"))?,
|
.with_context(|| format!("Package `{package}` not found in workspace"))?,
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
match VirtualProject::discover(&directory, &DiscoveryOptions::default()).await {
|
match VirtualProject::discover(&CWD, &DiscoveryOptions::default()).await {
|
||||||
Ok(project) => Some(project),
|
Ok(project) => Some(project),
|
||||||
Err(WorkspaceError::MissingPyprojectToml) => None,
|
Err(WorkspaceError::MissingPyprojectToml) => None,
|
||||||
Err(WorkspaceError::NonWorkspace(_)) => None,
|
Err(WorkspaceError::NonWorkspace(_)) => None,
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
use uv_auth::store_credentials_from_url;
|
use uv_auth::store_credentials_from_url;
|
||||||
|
@ -10,7 +8,7 @@ use uv_configuration::{
|
||||||
};
|
};
|
||||||
use uv_dispatch::BuildDispatch;
|
use uv_dispatch::BuildDispatch;
|
||||||
use uv_distribution::DEV_DEPENDENCIES;
|
use uv_distribution::DEV_DEPENDENCIES;
|
||||||
use uv_fs::{Simplified, CWD};
|
use uv_fs::CWD;
|
||||||
use uv_installer::SitePackages;
|
use uv_installer::SitePackages;
|
||||||
use uv_python::{PythonEnvironment, PythonFetch, PythonPreference, PythonRequest};
|
use uv_python::{PythonEnvironment, PythonFetch, PythonPreference, PythonRequest};
|
||||||
use uv_resolver::{FlatIndex, Lock};
|
use uv_resolver::{FlatIndex, Lock};
|
||||||
|
@ -37,7 +35,6 @@ pub(crate) async fn sync(
|
||||||
python_preference: PythonPreference,
|
python_preference: PythonPreference,
|
||||||
python_fetch: PythonFetch,
|
python_fetch: PythonFetch,
|
||||||
settings: ResolverInstallerSettings,
|
settings: ResolverInstallerSettings,
|
||||||
directory: Option<PathBuf>,
|
|
||||||
preview: PreviewMode,
|
preview: PreviewMode,
|
||||||
connectivity: Connectivity,
|
connectivity: Connectivity,
|
||||||
concurrency: Concurrency,
|
concurrency: Concurrency,
|
||||||
|
@ -49,14 +46,8 @@ pub(crate) async fn sync(
|
||||||
warn_user_once!("`uv sync` is experimental and may change without warning");
|
warn_user_once!("`uv sync` is experimental and may change without warning");
|
||||||
}
|
}
|
||||||
|
|
||||||
let directory = if let Some(directory) = directory {
|
|
||||||
directory.simple_canonicalize()?
|
|
||||||
} else {
|
|
||||||
CWD.to_path_buf()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Identify the project
|
// Identify the project
|
||||||
let project = VirtualProject::discover(&directory, &DiscoveryOptions::default()).await?;
|
let project = VirtualProject::discover(&CWD, &DiscoveryOptions::default()).await?;
|
||||||
|
|
||||||
// Discover or create the virtual environment.
|
// Discover or create the virtual environment.
|
||||||
let venv = project::get_or_init_environment(
|
let venv = project::get_or_init_environment(
|
||||||
|
|
|
@ -62,6 +62,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
|
||||||
uv_warnings::enable();
|
uv_warnings::enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Switch directories as early as possible.
|
||||||
|
if let Some(directory) = cli.global_args.directory.as_ref() {
|
||||||
|
std::env::set_current_dir(directory)?;
|
||||||
|
}
|
||||||
|
|
||||||
// Load configuration from the filesystem, prioritizing (in order):
|
// Load configuration from the filesystem, prioritizing (in order):
|
||||||
// 1. The configuration file specified on the command-line.
|
// 1. The configuration file specified on the command-line.
|
||||||
// 2. The configuration file in the current workspace (i.e., the `pyproject.toml` or `uv.toml`
|
// 2. The configuration file in the current workspace (i.e., the `pyproject.toml` or `uv.toml`
|
||||||
|
@ -915,7 +920,6 @@ async fn run_project(
|
||||||
args.extras,
|
args.extras,
|
||||||
args.dev,
|
args.dev,
|
||||||
args.python,
|
args.python,
|
||||||
args.directory,
|
|
||||||
args.settings,
|
args.settings,
|
||||||
globals.isolated,
|
globals.isolated,
|
||||||
globals.preview,
|
globals.preview,
|
||||||
|
@ -949,7 +953,6 @@ async fn run_project(
|
||||||
globals.python_preference,
|
globals.python_preference,
|
||||||
globals.python_fetch,
|
globals.python_fetch,
|
||||||
args.settings,
|
args.settings,
|
||||||
args.directory,
|
|
||||||
globals.preview,
|
globals.preview,
|
||||||
globals.connectivity,
|
globals.connectivity,
|
||||||
Concurrency::default(),
|
Concurrency::default(),
|
||||||
|
@ -972,7 +975,6 @@ async fn run_project(
|
||||||
args.frozen,
|
args.frozen,
|
||||||
args.python,
|
args.python,
|
||||||
args.settings,
|
args.settings,
|
||||||
args.directory,
|
|
||||||
globals.preview,
|
globals.preview,
|
||||||
globals.python_preference,
|
globals.python_preference,
|
||||||
globals.python_fetch,
|
globals.python_fetch,
|
||||||
|
|
|
@ -193,7 +193,6 @@ pub(crate) struct RunSettings {
|
||||||
pub(crate) with_requirements: Vec<PathBuf>,
|
pub(crate) with_requirements: Vec<PathBuf>,
|
||||||
pub(crate) package: Option<PackageName>,
|
pub(crate) package: Option<PackageName>,
|
||||||
pub(crate) python: Option<String>,
|
pub(crate) python: Option<String>,
|
||||||
pub(crate) directory: Option<PathBuf>,
|
|
||||||
pub(crate) refresh: Refresh,
|
pub(crate) refresh: Refresh,
|
||||||
pub(crate) settings: ResolverInstallerSettings,
|
pub(crate) settings: ResolverInstallerSettings,
|
||||||
}
|
}
|
||||||
|
@ -218,7 +217,6 @@ impl RunSettings {
|
||||||
refresh,
|
refresh,
|
||||||
package,
|
package,
|
||||||
python,
|
python,
|
||||||
directory,
|
|
||||||
} = args;
|
} = args;
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -237,7 +235,6 @@ impl RunSettings {
|
||||||
.collect(),
|
.collect(),
|
||||||
package,
|
package,
|
||||||
python,
|
python,
|
||||||
directory,
|
|
||||||
refresh: Refresh::from(refresh),
|
refresh: Refresh::from(refresh),
|
||||||
settings: ResolverInstallerSettings::combine(
|
settings: ResolverInstallerSettings::combine(
|
||||||
resolver_installer_options(installer, build),
|
resolver_installer_options(installer, build),
|
||||||
|
@ -530,7 +527,6 @@ pub(crate) struct SyncSettings {
|
||||||
pub(crate) python: Option<String>,
|
pub(crate) python: Option<String>,
|
||||||
pub(crate) refresh: Refresh,
|
pub(crate) refresh: Refresh,
|
||||||
pub(crate) settings: ResolverInstallerSettings,
|
pub(crate) settings: ResolverInstallerSettings,
|
||||||
pub(crate) directory: Option<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SyncSettings {
|
impl SyncSettings {
|
||||||
|
@ -550,7 +546,6 @@ impl SyncSettings {
|
||||||
build,
|
build,
|
||||||
refresh,
|
refresh,
|
||||||
python,
|
python,
|
||||||
directory,
|
|
||||||
} = args;
|
} = args;
|
||||||
|
|
||||||
let modifications = if no_clean {
|
let modifications = if no_clean {
|
||||||
|
@ -574,7 +569,6 @@ impl SyncSettings {
|
||||||
resolver_installer_options(installer, build),
|
resolver_installer_options(installer, build),
|
||||||
filesystem,
|
filesystem,
|
||||||
),
|
),
|
||||||
directory,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -588,7 +582,6 @@ pub(crate) struct LockSettings {
|
||||||
pub(crate) python: Option<String>,
|
pub(crate) python: Option<String>,
|
||||||
pub(crate) refresh: Refresh,
|
pub(crate) refresh: Refresh,
|
||||||
pub(crate) settings: ResolverSettings,
|
pub(crate) settings: ResolverSettings,
|
||||||
pub(crate) directory: Option<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LockSettings {
|
impl LockSettings {
|
||||||
|
@ -602,7 +595,6 @@ impl LockSettings {
|
||||||
build,
|
build,
|
||||||
refresh,
|
refresh,
|
||||||
python,
|
python,
|
||||||
directory,
|
|
||||||
} = args;
|
} = args;
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -611,7 +603,6 @@ impl LockSettings {
|
||||||
python,
|
python,
|
||||||
refresh: Refresh::from(refresh),
|
refresh: Refresh::from(refresh),
|
||||||
settings: ResolverSettings::combine(resolver_options(resolver, build), filesystem),
|
settings: ResolverSettings::combine(resolver_options(resolver, build), filesystem),
|
||||||
directory,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -865,9 +865,7 @@ fn run_from_directory() -> Result<()> {
|
||||||
.arg("project")
|
.arg("project")
|
||||||
.arg("main");
|
.arg("main");
|
||||||
|
|
||||||
let mut filters = context.filters();
|
uv_snapshot!(context.filters(), command_with_args, @r###"
|
||||||
filters.push((r"project(\\|/).venv", "[VENV]"));
|
|
||||||
uv_snapshot!(filters, command_with_args, @r###"
|
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
|
@ -875,7 +873,7 @@ fn run_from_directory() -> Result<()> {
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
|
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
|
||||||
Creating virtualenv at: [VENV]
|
Creating virtualenv at: .venv
|
||||||
Resolved 1 package in [TIME]
|
Resolved 1 package in [TIME]
|
||||||
Prepared 1 package in [TIME]
|
Prepared 1 package in [TIME]
|
||||||
Installed 1 package in [TIME]
|
Installed 1 package in [TIME]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue