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

fix: parse ~= as version not as path #804

Merged
Merged
Show file tree
Hide file tree
Changes from 12 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
3 changes: 2 additions & 1 deletion crates/rattler_conda_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ typed-path = { workspace = true }
url = { workspace = true, features = ["serde"] }
indexmap = { workspace = true }
rattler_redaction = { version = "0.1.0", path = "../rattler_redaction" }
dirs = { workspace = true }

[dev-dependencies]
rand = { workspace = true }
insta = { workspace = true, features = ["yaml", "redactions", "toml", "glob"] }
insta = { workspace = true, features = ["yaml", "redactions", "toml", "glob", "filters"] }
rattler_package_streaming = { path = "../rattler_package_streaming", default-features = false, features = ["rustls-tls"] }
tempfile = { workspace = true }
rstest = { workspace = true }
Expand Down
38 changes: 24 additions & 14 deletions crates/rattler_conda_types/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use std::{
};

use file_url::directory_path_to_url;
use rattler_redaction::Redact;
use serde::{Deserialize, Serialize, Serializer};
use thiserror::Error;
use typed_path::{Utf8NativePathBuf, Utf8TypedPath, Utf8TypedPathBuf};
use url::Url;

use rattler_redaction::Redact;

use super::{ParsePlatformError, Platform};
use crate::utils::{
path::is_path,
Expand Down Expand Up @@ -444,11 +443,27 @@ pub(crate) const fn default_platforms() -> &'static [Platform] {

/// Returns the specified path as an absolute path
fn absolute_path(path: &str, root_dir: &Path) -> Result<Utf8TypedPathBuf, ParseChannelError> {
// Non parsable path
if path.starts_with("~\\") {
return Err(ParseChannelError::InvalidPath(path.to_owned()));
}

let path = Utf8TypedPath::from(path);
if path.is_absolute() {
return Ok(path.normalize());
}

// Parse the `~/` as the home folder
if let Ok(user_path) = path.strip_prefix("~/") {
return Ok(Utf8TypedPathBuf::from(
dirs::home_dir()
.ok_or(ParseChannelError::InvalidPath(path.to_string()))?
.to_string_lossy()
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
.as_ref(),
)
.join(user_path));
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
}

let root_dir_str = root_dir
.to_str()
.ok_or_else(|| ParseChannelError::NotUtf8RootDir(root_dir.to_path_buf()))?;
Expand Down Expand Up @@ -515,6 +530,13 @@ mod tests {
absolute_path("../foo", &current_dir).as_ref(),
Ok(&parent_dir.join("foo"))
);

let binding = dirs::home_dir().unwrap();
let home_dir = binding.to_str().unwrap();
assert_eq!(
absolute_path("~/unix_dir", &current_dir).unwrap().as_str(),
format!("{home_dir}/unix_dir").as_str()
);
}

#[test]
Expand Down Expand Up @@ -665,18 +687,6 @@ mod tests {
assert_eq!(channel.name.as_deref(), Some("conda-forge/label/rust_dev"));
}

#[test]
fn test_is_path() {
assert!(is_path("./foo"));
assert!(is_path("/foo"));
assert!(is_path("~/foo"));
assert!(is_path("../foo"));
assert!(is_path("/C:/foo"));
assert!(is_path("C:/foo"));

assert!(!is_path("conda-forge/label/rust_dev"));
}

#[test]
fn channel_canonical_name() {
let config = ChannelConfig::default_with_root_dir(std::env::current_dir().unwrap());
Expand Down
110 changes: 68 additions & 42 deletions crates/rattler_conda_types/src/match_spec/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use super::{
use crate::{
build_spec::{BuildNumberSpec, ParseBuildNumberSpecError},
package::ArchiveIdentifier,
utils::{path::is_path, url::parse_scheme},
utils::{path::is_absolute_path, url::parse_scheme},
version_spec::{
is_start_of_version_constraint,
version_tree::{recognize_constraint, recognize_version},
Expand Down Expand Up @@ -251,8 +251,8 @@ fn parse_bracket_vec_into_components(
let url = if parse_scheme(value).is_some() {
Url::parse(value)?
}
// 2 Is the spec a path, parse it as an url
else if is_path(value) {
// 2 Is the spec an absolute path, parse it as an url
else if is_absolute_path(value) {
let path = Utf8TypedPath::from(value);
file_url::file_path_to_url(path)
.map_err(|_error| ParseMatchSpecError::InvalidPackagePathOrUrl)?
Expand Down Expand Up @@ -286,7 +286,7 @@ pub fn parse_url_like(input: &str) -> Result<Option<Url>, ParseMatchSpecError> {
.map_err(ParseMatchSpecError::from);
}
// Is the spec a path, parse it as an url
if is_path(input) {
if is_absolute_path(input) {
let path = Utf8TypedPath::from(input);
return file_url::file_path_to_url(path)
.map(Some)
Expand Down Expand Up @@ -966,6 +966,10 @@ mod tests {
// subdir in brackets take precedence
"conda-forge/linux-32::python[version=3.9, subdir=linux-64]",
"conda-forge/linux-32::python ==3.9[subdir=linux-64, build_number=\"0\"]",
"rust ~=1.2.3",
"~/channel/dir::package",
"~\\windows_channel::package",
"./relative/channel::package",
];

let evaluated: IndexMap<_, _> = specs
Expand All @@ -982,7 +986,17 @@ mod tests {
)
})
.collect();
insta::assert_yaml_snapshot!(format!("test_from_string_{strictness:?}"), evaluated);

// Strip absolute paths from the channels for testing
let path = Url::from_directory_path(dirs::home_dir().unwrap()).unwrap();
insta::with_settings!({filters => vec![
(path.as_str(), "file://<ROOT>/"),
]}, {
insta::assert_yaml_snapshot!(
format!("test_from_string_{strictness:?}"),
evaluated
);
});
}

#[rstest]
Expand All @@ -1001,6 +1015,28 @@ mod tests {
let specs = [
"2.7|>=3.6",
"https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2",
"~=1.2.3",
"*.* mkl",
"C:\\Users\\user\\conda-bld\\linux-64\\foo-1.0-py27_0.tar.bz2",
"=1.0=py27_0",
"==1.0=py27_0",
"https://conda.anaconda.org/conda-forge/linux-64/py-rattler-0.6.1-py39h8169da8_0.conda",
"https://repo.prefix.dev/ruben-arts/linux-64/boost-cpp-1.78.0-h75c5d50_1.tar.bz2",
"3.8.* *_cpython",
"=*=cuda*",
">=1!164.3095,<1!165",
"/home/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2",
"[version=1.0.*]",
"[version=1.0.*, build_number=\">6\"]",
"==2.7.*.*|>=3.6",
"3.9",
"*",
"[version=3.9]",
"[version=3.9]",
"[version=3.9, subdir=linux-64]",
// subdir in brackets take precedence
"[version=3.9, subdir=linux-64]",
"==3.9[subdir=linux-64, build_number=\"0\"]",
];

let evaluated: IndexMap<_, _> = specs
Expand Down Expand Up @@ -1145,9 +1181,6 @@ mod tests {
let err = MatchSpec::from_str("bla/bla", Strict)
.expect_err("Should try to parse as name not url");
assert_eq!(err.to_string(), "'bla/bla' is not a valid package name. Package names can only contain 0-9, a-z, A-Z, -, _, or .");

let err = MatchSpec::from_str("./test/file", Strict).expect_err("Invalid url");
assert_eq!(err.to_string(), "invalid package path or url");
}

#[test]
Expand All @@ -1172,40 +1205,33 @@ mod tests {

#[test]
fn test_parse_channel_subdir() {
let (channel, subdir) = parse_channel_and_subdir("conda-forge").unwrap();
assert_eq!(
channel.unwrap(),
Channel::from_str("conda-forge", &channel_config()).unwrap()
);
assert_eq!(subdir, None);

let (channel, subdir) = parse_channel_and_subdir("conda-forge/linux-64").unwrap();
assert_eq!(
channel.unwrap(),
Channel::from_str("conda-forge", &channel_config()).unwrap()
);
assert_eq!(subdir, Some("linux-64".to_string()));

let (channel, subdir) = parse_channel_and_subdir("conda-forge/label/test").unwrap();
assert_eq!(
channel.unwrap(),
Channel::from_str("conda-forge/label/test", &channel_config()).unwrap()
);
assert_eq!(subdir, None);

let (channel, subdir) =
parse_channel_and_subdir("conda-forge/linux-64/label/test").unwrap();
assert_eq!(
channel.unwrap(),
Channel::from_str("conda-forge/linux-64/label/test", &channel_config()).unwrap()
);
assert_eq!(subdir, None);
let test_cases = vec![
("conda-forge", Some("conda-forge"), None),
(
"conda-forge/linux-64",
Some("conda-forge"),
Some("linux-64"),
),
(
"conda-forge/label/test",
Some("conda-forge/label/test"),
None,
),
(
"conda-forge/linux-64/label/test",
Some("conda-forge/linux-64/label/test"),
None,
),
("*/linux-64", Some("*"), Some("linux-64")),
];

let (channel, subdir) = parse_channel_and_subdir("*/linux-64").unwrap();
assert_eq!(
channel.unwrap(),
Channel::from_str("*", &channel_config()).unwrap()
);
assert_eq!(subdir, Some("linux-64".to_string()));
for (input, expected_channel, expected_subdir) in test_cases {
let (channel, subdir) = parse_channel_and_subdir(input).unwrap();
assert_eq!(
channel.unwrap(),
Channel::from_str(expected_channel.unwrap(), &channel_config()).unwrap()
);
assert_eq!(subdir, expected_subdir.map(|s| s.to_string()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,18 @@ python=*:
base_url: "https://conda.anaconda.org/conda-forge/"
name: conda-forge
subdir: linux-64
rust ~=1.2.3:
name: rust
version: ~=1.2.3
"~/channel/dir::package":
name: package
channel:
base_url: "file://<ROOT>/channel/dir/"
name: ~/channel/dir
"~\\windows_channel::package":
error: invalid channel
"./relative/channel::package":
name: package
channel:
base_url: "file://<ROOT>/dev/rattler/crates/rattler_conda_types/relative/channel/"
name: "./relative/channel"
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ python=*:
base_url: "https://conda.anaconda.org/conda-forge/"
name: conda-forge
subdir: linux-64
rust ~=1.2.3:
name: rust
version: ~=1.2.3
"~/channel/dir::package":
name: package
channel:
base_url: "file://<ROOT>/channel/dir/"
name: ~/channel/dir
"~\\windows_channel::package":
error: invalid channel
"./relative/channel::package":
name: package
channel:
base_url: "file://<ROOT>/dev/rattler/crates/rattler_conda_types/relative/channel/"
name: "./relative/channel"
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,54 @@ expression: evaluated
version: "==2.7|>=3.6"
"https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2":
url: "https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2"
~=1.2.3:
version: ~=1.2.3
"*.* mkl":
version: "*"
build: mkl
"C:\\Users\\user\\conda-bld\\linux-64\\foo-1.0-py27_0.tar.bz2":
url: "file:///C:/Users/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2"
"=1.0=py27_0":
version: "==1.0"
build: py27_0
"==1.0=py27_0":
version: "==1.0"
build: py27_0
"https://conda.anaconda.org/conda-forge/linux-64/py-rattler-0.6.1-py39h8169da8_0.conda":
url: "https://conda.anaconda.org/conda-forge/linux-64/py-rattler-0.6.1-py39h8169da8_0.conda"
"https://repo.prefix.dev/ruben-arts/linux-64/boost-cpp-1.78.0-h75c5d50_1.tar.bz2":
url: "https://repo.prefix.dev/ruben-arts/linux-64/boost-cpp-1.78.0-h75c5d50_1.tar.bz2"
3.8.* *_cpython:
version: 3.8.*
build: "*_cpython"
"=*=cuda*":
version: "*"
build: cuda*
">=1!164.3095,<1!165":
version: ">=1!164.3095,<1!165"
/home/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2:
url: "file:///home/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2"
"[version=1.0.*]":
version: 1.0.*
"[version=1.0.*, build_number=\">6\"]":
version: 1.0.*
build_number:
op: Gt
rhs: 6
"==2.7.*.*|>=3.6":
version: 2.7.*|>=3.6
"3.9":
version: "==3.9"
"*":
version: "*"
"[version=3.9]":
version: "==3.9"
"[version=3.9, subdir=linux-64]":
version: "==3.9"
subdir: linux-64
"==3.9[subdir=linux-64, build_number=\"0\"]":
version: "==3.9"
build_number:
op: Eq
rhs: 0
subdir: linux-64
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,51 @@ expression: evaluated
version: "==2.7|>=3.6"
"https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2":
url: "https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2"
~=1.2.3:
version: ~=1.2.3
"*.* mkl":
version: "*"
build: mkl
"C:\\Users\\user\\conda-bld\\linux-64\\foo-1.0-py27_0.tar.bz2":
url: "file:///C:/Users/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2"
"=1.0=py27_0":
error: "The build string '=py27_0' is not valid, it can only contain alphanumeric characters and underscores"
"==1.0=py27_0":
error: "The build string '=py27_0' is not valid, it can only contain alphanumeric characters and underscores"
"https://conda.anaconda.org/conda-forge/linux-64/py-rattler-0.6.1-py39h8169da8_0.conda":
url: "https://conda.anaconda.org/conda-forge/linux-64/py-rattler-0.6.1-py39h8169da8_0.conda"
"https://repo.prefix.dev/ruben-arts/linux-64/boost-cpp-1.78.0-h75c5d50_1.tar.bz2":
url: "https://repo.prefix.dev/ruben-arts/linux-64/boost-cpp-1.78.0-h75c5d50_1.tar.bz2"
3.8.* *_cpython:
version: 3.8.*
build: "*_cpython"
"=*=cuda*":
error: "The build string '=cuda*' is not valid, it can only contain alphanumeric characters and underscores"
">=1!164.3095,<1!165":
version: ">=1!164.3095,<1!165"
/home/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2:
url: "file:///home/user/conda-bld/linux-64/foo-1.0-py27_0.tar.bz2"
"[version=1.0.*]":
version: 1.0.*
"[version=1.0.*, build_number=\">6\"]":
version: 1.0.*
build_number:
op: Gt
rhs: 6
"==2.7.*.*|>=3.6":
error: "invalid version constraint: regex constraints are not supported"
"3.9":
version: "==3.9"
"*":
version: "*"
"[version=3.9]":
version: "==3.9"
"[version=3.9, subdir=linux-64]":
version: "==3.9"
subdir: linux-64
"==3.9[subdir=linux-64, build_number=\"0\"]":
version: "==3.9"
build_number:
op: Eq
rhs: 0
subdir: linux-64
Loading
Loading