Skip to content

Commit 9abd9b1

Browse files
authored
Respect --isolated in uv check (#19666)
1 parent 9550737 commit 9abd9b1

5 files changed

Lines changed: 263 additions & 22 deletions

File tree

‎crates/uv-cli/src/lib.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5329,6 +5329,14 @@ pub struct CheckArgs {
53295329
#[arg(long)]
53305330
pub no_sync: bool,
53315331

5332+
/// Run checks without mutating project state [env: UV_ISOLATED=]
5333+
///
5334+
/// Uses a temporary virtual environment and leaves existing environments and the project
5335+
/// lockfile unchanged. Declared project requirements are resolved and installed into the
5336+
/// temporary environment.
5337+
#[arg(long, value_parser = clap::builder::BoolishValueParser::new())]
5338+
pub isolated: bool,
5339+
53325340
/// The Python interpreter to use for the project environment.
53335341
///
53345342
/// By default, the first interpreter that meets the project's

‎crates/uv/src/commands/project/check.rs‎

Lines changed: 96 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ use tracing::debug;
66
use uv_cache::Cache;
77
use uv_client::BaseClientBuilder;
88
use uv_configuration::{
9-
Concurrency, DependencyGroups, DryRun, ExtrasSpecification, InstallOptions,
9+
Concurrency, DependencyGroups, DependencyGroupsWithDefaults, DryRun, ExtrasSpecification,
10+
InstallOptions,
1011
};
1112
use uv_normalize::DefaultExtras;
1213
use uv_preview::{Preview, PreviewFeature};
13-
use uv_python::{PythonDownloads, PythonPreference, PythonRequest};
14+
use uv_python::{
15+
EnvironmentPreference, PythonDownloads, PythonInstallation, PythonPreference, PythonRequest,
16+
};
1417
use uv_settings::{MalwareCheckSettings, PythonInstallMirrors};
1518
use uv_warnings::warn_user;
1619
use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceCache, WorkspaceError};
@@ -20,8 +23,10 @@ use crate::commands::pip::operations::Modifications;
2023
use crate::commands::project::install_target::InstallTarget;
2124
use crate::commands::project::lock::LockMode;
2225
use crate::commands::project::{
23-
ProjectEnvironment, ProjectError, UniversalState, default_dependency_groups,
26+
ProjectEnvironment, ProjectError, UniversalState, WorkspacePython, default_dependency_groups,
27+
validate_project_requires_python,
2428
};
29+
use crate::commands::reporters::PythonDownloadReporter;
2530
use crate::commands::{ExitStatus, diagnostics, project};
2631
use crate::printer::Printer;
2732
use crate::settings::{FrozenSource, LockCheck, ResolverInstallerSettings};
@@ -35,6 +40,7 @@ pub(crate) async fn check(
3540
lock_check: LockCheck,
3641
frozen: Option<FrozenSource>,
3742
no_sync: bool,
43+
isolated: bool,
3844
extras: ExtrasSpecification,
3945
groups: DependencyGroups,
4046
python: Option<String>,
@@ -117,30 +123,97 @@ pub(crate) async fn check(
117123
.map(|p| p.root().to_owned())
118124
.unwrap_or_else(|| project_dir.to_owned());
119125

120-
// If we found a project, sync the environment before running checks.
121-
let venv_path = if let Some(project) = &project {
122-
let default_groups = default_dependency_groups(project.pyproject_toml())?;
123-
let default_extras = DefaultExtras::default();
124-
let groups = groups.with_defaults(default_groups);
125-
let extras = extras.with_defaults(default_extras);
126+
let groups = if let Some(project) = &project {
127+
groups.with_defaults(default_dependency_groups(project.pyproject_toml())?)
128+
} else {
129+
DependencyGroupsWithDefaults::none()
130+
};
126131

127-
let venv = ProjectEnvironment::get_or_init(
128-
project.workspace(),
129-
&groups,
132+
// Create an isolated environment, if requested.
133+
let temp_dir;
134+
let isolated_venv = if isolated {
135+
debug!("Creating isolated virtual environment");
136+
137+
let workspace = project.as_ref().map(VirtualProject::workspace);
138+
let WorkspacePython {
139+
source,
140+
python_request,
141+
requires_python,
142+
} = WorkspacePython::from_request(
130143
python.as_deref().map(PythonRequest::parse),
131-
&install_mirrors,
132-
&client_builder,
144+
workspace,
145+
&groups,
146+
project_dir,
147+
no_config,
148+
)
149+
.await?;
150+
151+
let reporter = PythonDownloadReporter::single(printer);
152+
let interpreter = PythonInstallation::find_or_download(
153+
python_request.as_ref(),
154+
EnvironmentPreference::Any,
133155
python_preference,
134156
python_downloads,
135-
no_sync,
136-
no_config,
137-
None,
157+
&client_builder,
138158
cache,
139-
DryRun::Disabled,
140-
printer,
159+
Some(&reporter),
160+
install_mirrors.python_install_mirror.as_deref(),
161+
install_mirrors.pypy_install_mirror.as_deref(),
162+
install_mirrors.python_downloads_json_url.as_deref(),
141163
)
142164
.await?
143-
.into_environment()?;
165+
.into_interpreter();
166+
167+
if let Some(requires_python) = requires_python.as_ref() {
168+
validate_project_requires_python(
169+
&interpreter,
170+
workspace,
171+
&groups,
172+
requires_python,
173+
&source,
174+
)?;
175+
}
176+
177+
temp_dir = cache.venv_dir()?;
178+
Some(uv_virtualenv::create_venv(
179+
temp_dir.path(),
180+
interpreter,
181+
uv_virtualenv::Prompt::None,
182+
false,
183+
uv_virtualenv::OnExisting::Remove(uv_virtualenv::RemovalReason::TemporaryEnvironment),
184+
false,
185+
false,
186+
false,
187+
)?)
188+
} else {
189+
None
190+
};
191+
192+
// Select an environment and, if we found a project, sync it before running checks.
193+
let venv_path = if let Some(project) = &project {
194+
let extras = extras.with_defaults(DefaultExtras::default());
195+
196+
let venv = if let Some(venv) = isolated_venv {
197+
venv
198+
} else {
199+
ProjectEnvironment::get_or_init(
200+
project.workspace(),
201+
&groups,
202+
python.as_deref().map(PythonRequest::parse),
203+
&install_mirrors,
204+
&client_builder,
205+
python_preference,
206+
python_downloads,
207+
no_sync,
208+
no_config,
209+
None,
210+
cache,
211+
DryRun::Disabled,
212+
printer,
213+
)
214+
.await?
215+
.into_environment()?
216+
};
144217

145218
if no_sync {
146219
debug!("Skipping environment synchronization due to `--no-sync`");
@@ -160,6 +233,8 @@ pub(crate) async fn check(
160233
LockMode::Frozen(frozen_source.into())
161234
} else if let LockCheck::Enabled(lock_check) = lock_check {
162235
LockMode::Locked(venv.interpreter(), lock_check)
236+
} else if isolated {
237+
LockMode::DryRun(venv.interpreter())
163238
} else {
164239
LockMode::Write(venv.interpreter())
165240
};
@@ -245,7 +320,7 @@ pub(crate) async fn check(
245320

246321
Some(venv.root().to_owned())
247322
} else {
248-
None
323+
isolated_venv.map(|venv| venv.root().to_owned())
249324
};
250325

251326
let exclude_newer = settings

‎crates/uv/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
230230
}) => false,
231231

232232
// Supports `--isolated` as its own argument, so we can't warn either way.
233-
Commands::Project(command) if matches!(**command, ProjectCommand::Run(_)) => false,
233+
Commands::Project(command)
234+
if matches!(**command, ProjectCommand::Run(_) | ProjectCommand::Check(_)) =>
235+
{
236+
false
237+
}
234238

235239
// `--isolated` moved to `--no-workspace`.
236240
Commands::Project(command) if matches!(**command, ProjectCommand::Init(_)) => {
@@ -2698,6 +2702,7 @@ async fn run_project(
26982702
args.lock_check,
26992703
args.frozen,
27002704
args.no_sync,
2705+
args.isolated,
27012706
args.extras,
27022707
args.groups,
27032708
args.python,

‎crates/uv/src/settings.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,7 @@ pub(crate) struct CheckSettings {
27502750
pub(crate) lock_check: LockCheck,
27512751
pub(crate) frozen: Option<FrozenSource>,
27522752
pub(crate) no_sync: bool,
2753+
pub(crate) isolated: bool,
27532754
pub(crate) python: Option<String>,
27542755
pub(crate) install_mirrors: PythonInstallMirrors,
27552756
pub(crate) refresh: Refresh,
@@ -2782,6 +2783,7 @@ impl CheckSettings {
27822783
locked,
27832784
frozen,
27842785
no_sync,
2786+
isolated,
27852787
python,
27862788
ty_version,
27872789
no_project,
@@ -2798,6 +2800,7 @@ impl CheckSettings {
27982800
let locked = resolve_flag(locked, "locked", environment.locked);
27992801
let frozen = resolve_flag(frozen, "frozen", environment.frozen);
28002802
let no_sync = resolve_flag(no_sync, "no-sync", environment.no_sync);
2803+
let isolated = resolve_flag(isolated, "isolated", environment.isolated).is_enabled();
28012804
check_conflicts(locked, frozen);
28022805

28032806
let (dev, no_dev) = resolve_flag_pair(
@@ -2840,6 +2843,7 @@ impl CheckSettings {
28402843
lock_check: resolve_lock_check(locked),
28412844
frozen: resolve_frozen(frozen),
28422845
no_sync: no_sync.is_enabled(),
2846+
isolated,
28432847
python: python.and_then(Maybe::into_option),
28442848
install_mirrors: environment
28452849
.install_mirrors

0 commit comments

Comments
 (0)