Fix a bug where no warning is output when parsing of workspace settings fails. (#4014)

## Summary

See #4013

`uv pip ...` command loads workspace settings from pyproject.toml and
uv.toml.

Although a warning is implemented to output a warning when parsing
fails, it is not actually output.


https://github.com/astral-sh/uv/blob/main/crates/uv-workspace/src/workspace.rs#L38-L61

The reason is that the flag to display warnings is enabled after loading
the workspace settings.

This PR turns on the warning output flag before loading the workspace.

## Test Plan

pyproject.toml for test

```toml
[project]
name = "sample"
version = "0.0.0"
dependencies = ["ruff"]

[tool.uv.pip]
# originally string type.
index-url = 1
```

command output (before modification)
```bash
uv pip compile pyproject.toml 

Resolved 1 package in 383ms
# This file was autogenerated by uv via the following command:
#    uv pip compile pyproject.toml
ruff==0.4.7
    # via sample (pyproject.toml)
```

command output (after modification)

```bash
uv pip compile pyproject.toml

warning: Failed to parse `pyproject.toml`: TOML parse error at line 7, column 13
  |
7 | index-url = true
  |             ^^^^
invalid type: boolean `true`, expected a string

Resolved 1 package in 107ms
# This file was autogenerated by uv via the following command:
#    uv pip compile pyproject.toml
ruff==0.4.7
    # via sample (pyproject.toml)
```
This commit is contained in:
Di-Is 2024-06-04 22:21:19 +09:00 committed by GitHub
parent da7d5549a3
commit 41ec302557
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 2 deletions

View file

@ -17,6 +17,11 @@ pub fn enable() {
ENABLED.store(true, std::sync::atomic::Ordering::SeqCst); ENABLED.store(true, std::sync::atomic::Ordering::SeqCst);
} }
/// Disable user-facing warnings.
pub fn disable() {
ENABLED.store(false, std::sync::atomic::Ordering::SeqCst);
}
/// Warn a user, if warnings are enabled. /// Warn a user, if warnings are enabled.
#[macro_export] #[macro_export]
macro_rules! warn_user { macro_rules! warn_user {

View file

@ -107,6 +107,11 @@ async fn run() -> Result<ExitStatus> {
} }
}; };
// enable flag to pick up warnings generated by workspace loading.
if !cli.global_args.quiet {
uv_warnings::enable();
}
// Load the workspace settings, prioritizing (in order): // Load the workspace settings, prioritizing (in order):
// 1. The configuration file specified on the command-line. // 1. The configuration file specified on the command-line.
// 2. The configuration file in the current directory. // 2. The configuration file in the current directory.
@ -148,7 +153,9 @@ async fn run() -> Result<ExitStatus> {
}; };
// Configure the `warn!` macros, which control user-facing warnings in the CLI. // Configure the `warn!` macros, which control user-facing warnings in the CLI.
if !globals.quiet { if globals.quiet {
uv_warnings::disable();
} else {
uv_warnings::enable(); uv_warnings::enable();
} }

View file

@ -3048,7 +3048,6 @@ fn override_multi_dependency() -> Result<()> {
} }
/// Check how invalid `tool.uv.override-dependencies` is handled in `pyproject.toml`. /// Check how invalid `tool.uv.override-dependencies` is handled in `pyproject.toml`.
// TODO(konsti): We should show a warnings here or better fail parsing.
#[test] #[test]
fn override_dependency_from_workspace_invalid_syntax() -> Result<()> { fn override_dependency_from_workspace_invalid_syntax() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
@ -3095,6 +3094,14 @@ fn override_dependency_from_workspace_invalid_syntax() -> Result<()> {
# via flask # via flask
----- stderr ----- ----- stderr -----
warning: Failed to parse `pyproject.toml`: TOML parse error at line 9, column 29
|
9 | override-dependencies = [
| ^
no such comparison operator "=", must be one of ~= == != <= >= < > ===
werkzeug=2.3.0
^^^^^^
Resolved 7 packages in [TIME] Resolved 7 packages in [TIME]
"### "###
); );

View file

@ -142,6 +142,12 @@ fn invalid_pyproject_toml_syntax() -> Result<()> {
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
warning: Failed to parse `pyproject.toml`: TOML parse error at line 1, column 5
|
1 | 123 - 456
| ^
expected `.`, `=`
error: Failed to parse: `pyproject.toml` error: Failed to parse: `pyproject.toml`
Caused by: TOML parse error at line 1, column 5 Caused by: TOML parse error at line 1, column 5
| |