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

Avoid treating uv add -r as --raw-sources #6287

Merged
merged 1 commit into from
Aug 21, 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
2 changes: 0 additions & 2 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2382,8 +2382,6 @@ pub struct AddArgs {
pub packages: Vec<String>,

/// Add all packages listed in the given `requirements.txt` files.
///
/// Implies `--raw-sources`.
#[arg(long, short, group = "sources", value_parser = parse_file_path)]
pub requirements: Vec<PathBuf>,

Expand Down
51 changes: 26 additions & 25 deletions crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,31 +351,32 @@ impl Source {
subdirectory,
..
} => {
// We can only resolve a full commit hash from a pep508 URL, everything else is ambiguous.
let rev = match reference {
GitReference::FullCommit(ref mut rev) => Some(mem::take(rev)),
_ => None,
}
// Give precedence to an explicit argument.
.or(rev);

// Error if the user tried to specify a reference but didn't disambiguate.
if reference != GitReference::DefaultBranch
&& rev.is_none()
&& tag.is_none()
&& branch.is_none()
{
return Err(SourceError::UnresolvedReference(
reference.as_str().unwrap().to_owned(),
));
}

Source::Git {
rev,
tag,
branch,
git: repository,
subdirectory: subdirectory.map(|path| path.to_string_lossy().into_owned()),
if rev.is_none() && tag.is_none() && branch.is_none() {
let rev = match reference {
GitReference::FullCommit(ref mut rev) => Some(mem::take(rev)),
GitReference::Branch(ref mut rev) => Some(mem::take(rev)),
GitReference::Tag(ref mut rev) => Some(mem::take(rev)),
GitReference::ShortCommit(ref mut rev) => Some(mem::take(rev)),
GitReference::BranchOrTag(ref mut rev) => Some(mem::take(rev)),
GitReference::BranchOrTagOrCommit(ref mut rev) => Some(mem::take(rev)),
GitReference::NamedRef(ref mut rev) => Some(mem::take(rev)),
GitReference::DefaultBranch => None,
};
Source::Git {
rev,
tag,
branch,
git: repository,
subdirectory: subdirectory.map(|path| path.to_string_lossy().into_owned()),
}
} else {
Source::Git {
rev,
tag,
branch,
git: repository,
subdirectory: subdirectory.map(|path| path.to_string_lossy().into_owned()),
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ fn resolve_requirement(
let source = match result {
Ok(source) => source,
Err(SourceError::UnresolvedReference(rev)) => {
anyhow::bail!(
bail!(
"Cannot resolve Git reference `{rev}` for requirement `{name}`. Specify the reference with one of `--tag`, `--branch`, or `--rev`, or use the `--raw-sources` flag.",
name = requirement.name
)
Expand Down
12 changes: 1 addition & 11 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,16 +1113,6 @@ async fn run_project(
.combine(Refresh::from(args.settings.upgrade.clone())),
);

// Use raw sources if requirements files are provided as input.
let raw_sources = if args.requirements.is_empty() {
args.raw_sources
} else {
if args.raw_sources {
warn_user!("`--raw-sources` is a no-op for `requirements.txt` files, which are always treated as raw sources");
}
true
};

let requirements = args
.packages
.into_iter()
Expand All @@ -1141,7 +1131,7 @@ async fn run_project(
requirements,
args.editable,
args.dependency_type,
raw_sources,
args.raw_sources,
args.rev,
args.tag,
args.branch,
Expand Down
52 changes: 17 additions & 35 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,19 @@ fn add_git() -> Result<()> {
+ sniffio==1.3.1
"###);

// Adding with an ambiguous Git reference will fail.
// Adding with an ambiguous Git reference should treat it as a revision.
uv_snapshot!(context.filters(), context.add(&["uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@0.0.1"]).arg("--preview"), @r###"
success: false
exit_code: 2
success: true
exit_code: 0
----- stdout -----

----- stderr -----
error: Cannot resolve Git reference `0.0.1` for requirement `uv-public-pypackage`. Specify the reference with one of `--tag`, `--branch`, or `--rev`, or use the `--raw-sources` flag.
Resolved 5 packages in [TIME]
Prepared 2 packages in [TIME]
Uninstalled 1 package in [TIME]
Installed 2 packages in [TIME]
~ project==0.1.0 (from file://[TEMP_DIR]/)
+ uv-public-pypackage==0.1.0 (from git+https://github.com/astral-test/uv-public-pypackage@0dacfd662c64cb4ceb16e6cf65a157a8b715b979)
"###);

uv_snapshot!(context.filters(), context.add(&["uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage"]).arg("--tag=0.0.1").arg("--preview"), @r###"
Expand All @@ -185,11 +190,10 @@ fn add_git() -> Result<()> {

----- stderr -----
Resolved 5 packages in [TIME]
Prepared 2 packages in [TIME]
Prepared 1 package in [TIME]
Uninstalled 1 package in [TIME]
Installed 2 packages in [TIME]
Installed 1 package in [TIME]
~ project==0.1.0 (from file://[TEMP_DIR]/)
+ uv-public-pypackage==0.1.0 (from git+https://github.com/astral-test/uv-public-pypackage@0dacfd662c64cb4ceb16e6cf65a157a8b715b979)
"###);

let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;
Expand Down Expand Up @@ -3339,7 +3343,8 @@ fn add_requirements_file() -> Result<()> {
"#})?;

let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("Flask==2.3.2\ngit+https://github.com/agronholm/anyio.git@4.4.0")?;
requirements_txt
.write_str("Flask==2.3.2\nanyio @ git+https://github.com/agronholm/anyio.git@4.4.0")?;

uv_snapshot!(context.filters(), context.add(&[]).arg("-r").arg("requirements.txt"), @r###"
success: true
Expand Down Expand Up @@ -3377,24 +3382,15 @@ fn add_requirements_file() -> Result<()> {
requires-python = ">=3.12"
dependencies = [
"flask==2.3.2",
"anyio @ git+https://github.com/agronholm/anyio.git@4.4.0",
"anyio",
]

[tool.uv.sources]
anyio = { git = "https://github.com/agronholm/anyio.git", rev = "4.4.0" }
"###
);
});

// Using `--raw-sources` with `-r` should warn.
uv_snapshot!(context.filters(), context.add(&[]).arg("-r").arg("requirements.txt").arg("--raw-sources"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
warning: `--raw-sources` is a no-op for `requirements.txt` files, which are always treated as raw sources
Resolved [N] packages in [TIME]
Audited [N] packages in [TIME]
"###);

// Passing a `setup.py` should fail.
uv_snapshot!(context.filters(), context.add(&[]).arg("-r").arg("setup.py"), @r###"
success: false
Expand Down Expand Up @@ -3775,20 +3771,6 @@ fn add_git_to_script() -> Result<()> {
pprint([(k, v["title"]) for k, v in data.items()][:10])
"#})?;

// Adding with an ambiguous Git reference will fail.
uv_snapshot!(context.filters(), context
.add(&["uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@0.0.1"])
.arg("--preview")
.arg("--script")
.arg("script.py"), @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: Cannot resolve Git reference `0.0.1` for requirement `uv-public-pypackage`. Specify the reference with one of `--tag`, `--branch`, or `--rev`, or use the `--raw-sources` flag.
"###);

uv_snapshot!(context.filters(), context
.add(&["uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage"])
.arg("--tag=0.0.1")
Expand Down
4 changes: 1 addition & 3 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,7 @@ uv add [OPTIONS] <PACKAGES|--requirements <REQUIREMENTS>>

</dd><dt><code>--reinstall-package</code> <i>reinstall-package</i></dt><dd><p>Reinstall a specific package, regardless of whether it&#8217;s already installed. Implies <code>--refresh-package</code></p>

</dd><dt><code>--requirements</code>, <code>-r</code> <i>requirements</i></dt><dd><p>Add all packages listed in the given <code>requirements.txt</code> files.</p>

<p>Implies <code>--raw-sources</code>.</p>
</dd><dt><code>--requirements</code>, <code>-r</code> <i>requirements</i></dt><dd><p>Add all packages listed in the given <code>requirements.txt</code> files</p>

</dd><dt><code>--resolution</code> <i>resolution</i></dt><dd><p>The strategy to use when selecting between the different compatible versions for a given package requirement.</p>

Expand Down
Loading