Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions crates/ty_project/src/uv/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

use std::process::Output;

use pep440_rs::Version;
use ruff_db::system::{Command, CommandExecutor, System, SystemPath, WhichError};
use ty_static::EnvVars;

use super::{UvMetadata, UvMetadataError};

pub(super) const MINIMUM_UV_VERSION: [u64; 3] = [0, 12, 3];

#[derive(Clone)]
pub(crate) struct Uv {
executable: String,
Expand All @@ -31,6 +34,7 @@ impl Uv {
let output = system
.command_executor()
.ok_or_else(unsupported_command_execution)
.map_err(UvMetadataError::Invocation)
.and_then(|executor| self.execute(executor, target));
Self::parse_metadata_output(system, output)
}
Expand All @@ -44,25 +48,27 @@ impl Uv {
&self,
executor: &dyn CommandExecutor,
target: &MetadataTarget<'_>,
) -> std::io::Result<Output> {
) -> Result<Output, UvMetadataError> {
let mut command = Command::new(self.executable.as_str());
command.args(["workspace", "metadata", "--quiet"]);

match target {
let directory = match target {
MetadataTarget::Workspace(path) => {
// Use the environment selected by `uv check` without synchronizing it.
// Let uv apply its configured lockfile policy.
command.arg("--active").current_dir(path);
command.arg("--active");
Some(*path)
}
MetadataTarget::Script { path, python } => {
command.args(["--sync", "--script", path.as_str()]);
if let Some(python) = python {
command.args(["--python", python.as_str()]);
}
if let Some(parent) = path.parent() {
command.current_dir(parent);
}
path.parent()
}
};
if let Some(directory) = directory {
command.current_dir(directory);
}

tracing::debug!(
Expand All @@ -79,15 +85,52 @@ impl Uv {
start.elapsed().as_secs_f64()
);

output
let output = output.map_err(UvMetadataError::Invocation)?;

// Before uv 0.12.3, `--quiet` suppresses the metadata JSON even on success.
if (!output.status.success() || output.stdout.is_empty())
&& let Some(version) = self.version(executor, directory)
&& version < Version::new(MINIMUM_UV_VERSION)
{
return Err(UvMetadataError::UnsupportedVersion {
executable: self.executable.clone(),
version,
});
}

Ok(output)
}

fn version(
&self,
executor: &dyn CommandExecutor,
directory: Option<&SystemPath>,
) -> Option<Version> {
let mut command = Command::new(self.executable.as_str());
command.arg("--version");
if let Some(directory) = directory {
command.current_dir(directory);
}
let output = executor.execute(command).ok()?;
if !output.status.success() {
return None;
}

std::str::from_utf8(&output.stdout)
.ok()?
.strip_prefix("uv ")?
.split_whitespace()
.next()?
.parse()
.ok()
}

/// Parses and validates the output returned by [`Self::execute`].
pub(crate) fn parse_metadata_output(
system: &dyn System,
output: std::io::Result<Output>,
output: Result<Output, UvMetadataError>,
) -> Result<UvMetadata, UvMetadataError> {
let output = output.map_err(UvMetadataError::Invocation)?;
let output = output?;

if !output.status.success() {
return Err(UvMetadataError::CommandFailed {
Expand Down
6 changes: 3 additions & 3 deletions crates/ty_project/src/uv/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ use salsa::Setter;

use crate::script::script_tag;
use crate::uv::{
ScriptSyncRequest, ScriptSyncTask, Uv, UvMetadata, UvMetadataResult, UvMetadataService,
UvSyncTask,
ScriptSyncRequest, ScriptSyncTask, Uv, UvMetadata, UvMetadataError, UvMetadataResult,
UvMetadataService, UvSyncTask,
};
use crate::{Db, ProjectReloadResult, ProjectSyncProgressFactory, UseUv, UvSyncProgress};

Expand Down Expand Up @@ -710,7 +710,7 @@ fn apply_sync_result(
db: &mut dyn Db,
environment: ScriptEnvironment,
request: &ScriptSyncRequest,
output: std::io::Result<std::process::Output>,
output: Result<std::process::Output, UvMetadataError>,
) {
let (uv_metadata, initialization_error) = match Uv::parse_metadata_output(db.system(), output) {
Ok(metadata) => (Some(metadata), None),
Expand Down
9 changes: 9 additions & 0 deletions crates/ty_project/src/uv/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use std::collections::BTreeMap;
use std::path::PathBuf;

use char_str::CharStr;
use pep440_rs::Version;
use ruff_db::system::{System, SystemPath, SystemPathBuf};
use serde::Deserialize;
use thiserror::Error;

use super::command::MINIMUM_UV_VERSION;

mod dependencies;
mod string_interner;

Expand Down Expand Up @@ -82,6 +85,12 @@ pub(crate) struct WorkspaceMember {

#[derive(Debug, Error)]
pub(crate) enum UvMetadataError {
#[error("uv {version} is too old; upgrade `{executable}` to uv {minimum_version} or newer", minimum_version = Version::new(MINIMUM_UV_VERSION))]
UnsupportedVersion {
executable: String,
version: Version,
},

#[error("Failed to invoke `uv workspace metadata`: {0}")]
Invocation(#[source] std::io::Error),

Expand Down
8 changes: 4 additions & 4 deletions crates/ty_project/src/uv/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ruff_db::files::File;
use ruff_db::system::{CommandExecutor, System, SystemPathBuf};

use super::command::unsupported_command_execution;
use super::{MetadataTarget, ScriptEnvironmentCacheKey, Uv, uv_executable_error};
use super::{MetadataTarget, ScriptEnvironmentCacheKey, Uv, UvMetadataError, uv_executable_error};
use crate::UvSyncProgress;

/// Runs workspace and standalone-script metadata requests with uv.
Expand Down Expand Up @@ -71,7 +71,7 @@ impl UvMetadataService {
Err(error) => {
self.publish_result(UvMetadataResult {
task,
output: Some(Err(error)),
output: Some(Err(UvMetadataError::Invocation(error))),
progress,
});
return;
Expand All @@ -97,7 +97,7 @@ impl UvMetadataService {
let job = error.into_inner();
self.publish_result(UvMetadataResult {
task: job.task,
output: Some(Err(worker_disconnected())),
output: Some(Err(UvMetadataError::Invocation(worker_disconnected()))),
progress: job.progress,
});
}
Expand Down Expand Up @@ -211,7 +211,7 @@ impl UvSyncTask {
pub(crate) struct UvMetadataResult {
pub(crate) task: UvSyncTask,
/// `None` if the worker skipped this request because it was cancelled before execution.
pub(crate) output: Option<std::io::Result<Output>>,
pub(crate) output: Option<Result<Output, UvMetadataError>>,
pub(crate) progress: Option<Box<dyn UvSyncProgress>>,
}

Expand Down
Loading