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

Correctly parse arguments from environment variables #14376

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ serde_json = "1.0.120"
sha1 = "0.10.6"
sha2 = "0.10.8"
shell-escape = "0.1.5"
shlex = "1.3.0"
similar = "2.6.0"
supports-hyperlinks = "3.0.0"
snapbox = { version = "0.6.16", features = ["diff", "dir", "term-svg", "regex", "json"] }
Expand Down Expand Up @@ -192,6 +193,7 @@ serde_ignored.workspace = true
serde_json = { workspace = true, features = ["raw_value"] }
sha1.workspace = true
shell-escape.workspace = true
shlex.workspace = true
supports-hyperlinks.workspace = true
tar.workspace = true
tempfile.workspace = true
Expand Down
42 changes: 40 additions & 2 deletions src/cargo/util/context/mod.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocked on discussion in #14381

Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ use lazycell::LazyCell;
use serde::de::IntoDeserializer as _;
use serde::Deserialize;
use serde_untagged::UntaggedEnumVisitor;
use shlex::Shlex;
use time::OffsetDateTime;
use toml_edit::Item;
use url::Url;
Expand Down Expand Up @@ -973,8 +974,8 @@ impl GlobalContext {
}
} else {
output.extend(
env_val
.split_whitespace()
Shlex::new(env_val)
.into_iter()
.map(|s| (s.to_string(), def.clone())),
Comment on lines 976 to 979
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need a test for this.

Ideally, the PR gets split up into two commits

  • The first adds the test, showing the bad behavior
  • This commit updates the test showing the good behavior

As discussed at https://doc.crates.io/contrib/process/working-on-cargo.html#submitting-a-pull-request

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted! Rewriting git history and using this occasion to add a test.

);
}
Expand Down Expand Up @@ -2971,9 +2972,13 @@ fn disables_multiplexing_for_bad_curl(
mod tests {
use super::disables_multiplexing_for_bad_curl;
use super::CargoHttpConfig;
use super::ConfigKey;
use super::Definition;
use super::GlobalContext;
use super::Shell;

use std::collections::HashMap;

#[test]
fn disables_multiplexing() {
let mut gctx = GlobalContext::new(Shell::new(), "".into(), "".into());
Expand Down Expand Up @@ -3010,4 +3015,37 @@ mod tests {
assert_eq!(http.multiplexing, result);
}
}

#[test]
fn env_argument_parsing() {
let mut gctx = GlobalContext::new(Shell::new(), "".into(), "".into());
gctx.set_search_stop_path(std::path::PathBuf::new());

let values: &[(&str, &[&str])] = &[
("VAL1", &["--path"]),
("VAL2", &["--path=\"y z\""]),
("VAL3", &["--path", "\"y z\""]),
];

let mut env = HashMap::new();
for (key, value) in values {
env.insert(format!("CARGO_{key}"), value.join(" "));
}
gctx.set_env(env);

for (key, value) in values {
let mut args = Vec::new();
gctx.get_env_list(&ConfigKey::from_str(key), &mut args)
.unwrap();

let mut expected = Vec::new();
for sub in *value {
expected.push((
sub.replace("\"", ""),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shlex removes (double) quotes. Not sure whether or not it's a good thing. If it's not the expected behaviour, I can implement a simple shell parser which keeps them.

Definition::Environment(format!("CARGO_{key}")),
));
}
assert_eq!(args, expected, "failed for key {}", key);
}
}
}