Skip to content

Commit

Permalink
Ignore errors in workspace discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 23, 2024
1 parent 2419760 commit 9d766dd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 36 deletions.
9 changes: 3 additions & 6 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{Context, Result};
use owo_colors::OwoColorize;
use pep440_rs::Version;
use pep508_rs::PackageName;
use tracing::debug;
use tracing::{debug, warn};
use uv_cache::Cache;
use uv_client::{BaseClientBuilder, Connectivity};
use uv_fs::{absolutize_path, Simplified, CWD};
Expand All @@ -14,7 +14,6 @@ use uv_python::{
VersionRequest,
};
use uv_resolver::RequiresPython;
use uv_warnings::warn_user_once;
use uv_workspace::pyproject_mut::{DependencyTarget, PyProjectTomlMut};
use uv_workspace::{check_nested_workspaces, DiscoveryOptions, Workspace, WorkspaceError};

Expand Down Expand Up @@ -184,16 +183,14 @@ async fn init_project(
Err(WorkspaceError::MissingPyprojectToml | WorkspaceError::NonWorkspace(_)) => {
// If the user runs with `--no-workspace` and we can't find a workspace, warn.
if no_workspace {
warn_user_once!("`--no-workspace` was provided, but no workspace was found");
warn!("`--no-workspace` was provided, but no workspace was found");
}
None
}
Err(err) => {
// If the user runs with `--no-workspace`, ignore the error.
if no_workspace {
warn_user_once!(
"Ignoring workspace discovery error due to `--no-workspace`: {err}"
);
warn!("Ignoring workspace discovery error due to `--no-workspace`: {err}");
None
} else {
return Err(err.into());
Expand Down
69 changes: 39 additions & 30 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::{anyhow, bail, Context};
use itertools::Itertools;
use owo_colors::OwoColorize;
use tokio::process::Command;
use tracing::debug;
use tracing::{debug, warn};

use uv_cache::Cache;
use uv_cli::ExternalCommand;
Expand Down Expand Up @@ -264,19 +264,34 @@ pub(crate) async fn run(
))
} else {
match VirtualProject::discover(&CWD, &DiscoveryOptions::default()).await {
Ok(project) => Some(project),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(WorkspaceError::NonWorkspace(_)) => None,
Err(err) => return Err(err.into()),
Ok(project) => {
if no_project {
debug!("Ignoring discovered project due to `--no-project`");
None
} else {
Some(project)
}
}
Err(WorkspaceError::MissingPyprojectToml | WorkspaceError::NonWorkspace(_)) => {
// If the user runs with `--no-project` and we can't find a project, warn.
if no_project {
warn!("`--no-project` was provided, but no project was found");
}
None
}
Err(err) => {
// If the user runs with `--no-project`, ignore the error.
if no_project {
warn!("Ignoring project discovery error due to `--no-project`: {err}");
None
} else {
return Err(err.into());
}
}
}
};

let project = if no_project {
// If the user runs with `--no-project` and we can't find a project, warn.
if project.is_none() {
debug!("`--no-project` was provided, but no project was found; ignoring...");
}

if no_project {
// If the user ran with `--no-project` and provided a project-only setting, warn.
if !extras.is_empty() {
warn_user_once!("Extras have no effect when used alongside `--no-project`");
Expand All @@ -290,27 +305,21 @@ pub(crate) async fn run(
if frozen {
warn_user_once!("`--frozen` has no effect when used alongside `--no-project`");
}

None
} else {
} else if project.is_none() {
// If we can't find a project and the user provided a project-only setting, warn.
if project.is_none() {
if !extras.is_empty() {
warn_user_once!("Extras have no effect when used outside of a project");
}
if !dev {
warn_user_once!("`--no-dev` has no effect when used outside of a project");
}
if locked {
warn_user_once!("`--locked` has no effect when used outside of a project");
}
if frozen {
warn_user_once!("`--frozen` has no effect when used outside of a project");
}
if !extras.is_empty() {
warn_user_once!("Extras have no effect when used outside of a project");
}

project
};
if !dev {
warn_user_once!("`--no-dev` has no effect when used outside of a project");
}
if locked {
warn_user_once!("`--locked` has no effect when used outside of a project");
}
if frozen {
warn_user_once!("`--frozen` has no effect when used outside of a project");
}
}

let interpreter = if let Some(project) = project {
if let Some(project_name) = project.project_name() {
Expand Down
41 changes: 41 additions & 0 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,3 +1422,44 @@ fn run_stdin() -> Result<()> {

Ok(())
}

/// When the `pyproject.toml` file is invalid.
#[test]
fn run_project_toml_error() -> Result<()> {
let context = TestContext::new("3.12")
.with_filtered_python_names()
.with_filtered_virtualenv_bin()
.with_filtered_exe_suffix();

// Create an empty project
let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.touch()?;

let src = context.temp_dir.child("src").child("foo");
src.create_dir_all()?;

let init = src.child("__init__.py");
init.touch()?;

// `run` should fail
uv_snapshot!(context.filters(), context.run().arg("python").arg("-c").arg("import sys; print(sys.executable)"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: No `project` table found in: `[TEMP_DIR]/pyproject.toml`
"###);

// `run --no-project` should not
uv_snapshot!(context.filters(), context.run().arg("--no-project").arg("python").arg("-c").arg("import sys; print(sys.executable)"), @r###"
success: true
exit_code: 0
----- stdout -----
[VENV]/[BIN]/python
----- stderr -----
"###);

Ok(())
}

0 comments on commit 9d766dd

Please sign in to comment.