Skip to content

Commit 66142e9

Browse files
authored
Support --active in uv workspace metadata (#20500)
1 parent bb9eba0 commit 66142e9

5 files changed

Lines changed: 64 additions & 4 deletions

File tree

crates/uv-cli/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8367,6 +8367,14 @@ pub struct MetadataArgs {
83678367
#[arg(long)]
83688368
pub sync: bool,
83698369

8370+
/// Sync dependencies to the active virtual environment.
8371+
///
8372+
/// Instead of creating or updating the virtual environment for the project or script, the
8373+
/// active virtual environment will be preferred, if the `VIRTUAL_ENV` environment variable is
8374+
/// set.
8375+
#[arg(long)]
8376+
pub active: bool,
8377+
83708378
/// The Python interpreter to use during resolution.
83718379
///
83728380
/// A Python interpreter is required for building source distributions to determine package

crates/uv/src/commands/workspace/metadata.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub(crate) async fn metadata(
3939
dry_run: DryRun,
4040
refresh: Refresh,
4141
sync: bool,
42+
active: bool,
4243
python: Option<String>,
4344
install_mirrors: PythonInstallMirrors,
4445
malware_settings: MalwareCheckSettings,
@@ -93,7 +94,7 @@ pub(crate) async fn metadata(
9394
&install_mirrors,
9495
false,
9596
config_discovery,
96-
Some(false),
97+
Some(active),
9798
cache,
9899
printer,
99100
)
@@ -117,7 +118,7 @@ pub(crate) async fn metadata(
117118
python_downloads,
118119
&install_mirrors,
119120
false,
120-
Some(false),
121+
Some(active),
121122
cache,
122123
printer,
123124
)
@@ -184,7 +185,7 @@ pub(crate) async fn metadata(
184185
python_downloads,
185186
false,
186187
config_discovery,
187-
Some(false),
188+
Some(active),
188189
cache,
189190
DryRun::Disabled,
190191
LinkErrorReporting::User,
@@ -201,7 +202,7 @@ pub(crate) async fn metadata(
201202
&install_mirrors,
202203
false,
203204
config_discovery,
204-
Some(false),
205+
Some(active),
205206
cache,
206207
DryRun::Disabled,
207208
printer,

crates/uv/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,7 @@ pub async fn run(cli: Cli, global_initialization: GlobalInitialization) -> Resul
20662066
args.dry_run,
20672067
args.refresh,
20682068
args.sync,
2069+
args.active,
20692070
args.python,
20702071
args.install_mirrors,
20712072
args.malware_settings,

crates/uv/src/settings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,7 @@ pub(crate) struct MetadataSettings {
21012101
pub(crate) frozen: Option<FrozenSource>,
21022102
pub(crate) dry_run: DryRun,
21032103
pub(crate) sync: bool,
2104+
pub(crate) active: bool,
21042105
pub(crate) python: Option<String>,
21052106
pub(crate) install_mirrors: PythonInstallMirrors,
21062107
pub(crate) refresh: Refresh,
@@ -2124,6 +2125,7 @@ impl MetadataSettings {
21242125
build,
21252126
refresh,
21262127
sync,
2128+
active,
21272129
python,
21282130
} = *args;
21292131

@@ -2147,6 +2149,7 @@ impl MetadataSettings {
21472149
frozen: resolve_frozen(frozen),
21482150
dry_run: DryRun::from_args(dry_run),
21492151
sync,
2152+
active,
21502153
python: python.and_then(Maybe::into_option),
21512154
refresh: Refresh::from(refresh),
21522155
settings: ResolverSettings::combine(

crates/uv/tests/workspace/workspace_metadata.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use async_zip::{Compression, ZipEntryBuilder};
88
use futures::executor::block_on;
99
use url::Url;
1010

11+
use uv_static::EnvVars;
1112
use uv_test::{copy_dir_ignore, uv_snapshot};
1213

1314
fn write_wheel(
@@ -525,6 +526,52 @@ fn workspace_metadata_sync_centralized_environment() -> Result<()> {
525526
Ok(())
526527
}
527528

529+
#[test]
530+
fn workspace_metadata_sync_active_environment() -> Result<()> {
531+
let context = uv_test::test_context_with_versions!(&["3.12", "3.11"]);
532+
533+
context.temp_dir.child("pyproject.toml").write_str(
534+
r#"
535+
[project]
536+
name = "project"
537+
version = "0.1.0"
538+
requires-python = ">=3.12"
539+
dependencies = []
540+
"#,
541+
)?;
542+
543+
context
544+
.venv()
545+
.arg("--python")
546+
.arg("3.11")
547+
.assert()
548+
.success();
549+
let active = context.temp_dir.child("active");
550+
context
551+
.venv()
552+
.arg(active.path())
553+
.arg("--python")
554+
.arg("3.12")
555+
.assert()
556+
.success();
557+
558+
let assert = context
559+
.workspace_metadata()
560+
.arg("--sync")
561+
.arg("--active")
562+
.env(EnvVars::VIRTUAL_ENV, active.path())
563+
.assert()
564+
.success();
565+
let metadata: serde_json::Value = serde_json::from_slice(&assert.get_output().stdout)?;
566+
567+
assert_eq!(
568+
metadata["environment"]["root"].as_str().map(Path::new),
569+
Some(active.path())
570+
);
571+
572+
Ok(())
573+
}
574+
528575
#[test]
529576
fn workspace_metadata_module_owners_from_locked_wheels() -> Result<()> {
530577
let context = uv_test::test_context!("3.12");

0 commit comments

Comments
 (0)