Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore errors in workspace discovery with --no-project #6554

Merged
merged 1 commit into from
Aug 23, 2024
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
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
2 changes: 0 additions & 2 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,6 @@ fn init_no_workspace() -> Result<()> {
----- stdout -----

----- stderr -----
warning: Ignoring workspace discovery error due to `--no-workspace`: No `project` table found in: `[TEMP_DIR]/`
Initialized project `bar`
"###);

Expand Down Expand Up @@ -728,7 +727,6 @@ fn init_no_workspace_warning() -> Result<()> {
----- stdout -----

----- stderr -----
warning: `--no-workspace` was provided, but no workspace was found
Initialized project `project`
"###);

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(())
}
Loading