Skip to content

Commit 78beb37

Browse files
committed
Auto merge of #8912 - aniljava:master, r=alexcrichton
Fixes #8783 , cargo new fails without a author name or email If user can not be obtained from git or env variables $USER, new command defaults to empty author in generated Cargo.toml Could not edit old PR(#8910 8783) as the original clone was deleted.
2 parents c7939ae + a006347 commit 78beb37

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -645,19 +645,30 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
645645
init_vcs(path, vcs, config)?;
646646
write_ignore_file(path, &ignore, vcs)?;
647647

648-
let (author_name, email) = discover_author(path)?;
649-
let author = match (cfg.name, cfg.email, author_name, email) {
650-
(Some(name), Some(email), _, _)
651-
| (Some(name), None, _, Some(email))
652-
| (None, Some(email), name, _)
653-
| (None, None, name, Some(email)) => {
648+
let (discovered_name, discovered_email) = discover_author(path);
649+
650+
// "Name <email>" or "Name" or "<email>" or None if neither name nor email is obtained
651+
// cfg takes priority over the discovered ones
652+
let author_name = cfg.name.or(discovered_name);
653+
let author_email = cfg.email.or(discovered_email);
654+
655+
let author = match (author_name, author_email) {
656+
(Some(name), Some(email)) => {
654657
if email.is_empty() {
655-
name
658+
Some(name)
656659
} else {
657-
format!("{} <{}>", name, email)
660+
Some(format!("{} <{}>", name, email))
658661
}
659662
}
660-
(Some(name), None, _, None) | (None, None, name, None) => name,
663+
(Some(name), None) => Some(name),
664+
(None, Some(email)) => {
665+
if email.is_empty() {
666+
None
667+
} else {
668+
Some(format!("<{}>", email))
669+
}
670+
}
671+
(None, None) => None,
661672
};
662673

663674
let mut cargotoml_path_specifier = String::new();
@@ -706,7 +717,10 @@ edition = {}
706717
[dependencies]
707718
{}"#,
708719
name,
709-
toml::Value::String(author),
720+
match author {
721+
Some(value) => format!("{}", toml::Value::String(value)),
722+
None => format!(""),
723+
},
710724
match opts.edition {
711725
Some(edition) => toml::Value::String(edition.to_string()),
712726
None => toml::Value::String("2018".to_string()),
@@ -781,7 +795,7 @@ fn get_environment_variable(variables: &[&str]) -> Option<String> {
781795
variables.iter().filter_map(|var| env::var(var).ok()).next()
782796
}
783797

784-
fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
798+
fn discover_author(path: &Path) -> (Option<String>, Option<String>) {
785799
let git_config = find_git_config(path);
786800
let git_config = git_config.as_ref();
787801

@@ -798,15 +812,10 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
798812
.or_else(|| get_environment_variable(&name_variables[3..]));
799813

800814
let name = match name {
801-
Some(name) => name,
802-
None => {
803-
let username_var = if cfg!(windows) { "USERNAME" } else { "USER" };
804-
anyhow::bail!(
805-
"could not determine the current user, please set ${}",
806-
username_var
807-
)
808-
}
815+
Some(namestr) => Some(namestr.trim().to_string()),
816+
None => None,
809817
};
818+
810819
let email_variables = [
811820
"CARGO_EMAIL",
812821
"GIT_AUTHOR_EMAIL",
@@ -817,7 +826,6 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
817826
.or_else(|| git_config.and_then(|g| g.get_string("user.email").ok()))
818827
.or_else(|| get_environment_variable(&email_variables[3..]));
819828

820-
let name = name.trim().to_string();
821829
let email = email.map(|s| {
822830
let mut s = s.trim();
823831

@@ -830,7 +838,7 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
830838
s.to_string()
831839
});
832840

833-
Ok((name, email))
841+
(name, email)
834842
}
835843

836844
fn find_git_config(path: &Path) -> Option<GitConfig> {

tests/testsuite/new.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,37 @@ fn finds_author_user() {
199199
assert!(contents.contains(r#"authors = ["foo"]"#));
200200
}
201201

202+
#[cargo_test]
203+
fn author_without_user_or_email() {
204+
create_empty_gitconfig();
205+
cargo_process("new foo")
206+
.env_remove("USER")
207+
.env_remove("USERNAME")
208+
.env_remove("NAME")
209+
.env_remove("EMAIL")
210+
.run();
211+
212+
let toml = paths::root().join("foo/Cargo.toml");
213+
let contents = fs::read_to_string(&toml).unwrap();
214+
assert!(contents.contains(r#"authors = []"#));
215+
}
216+
217+
#[cargo_test]
218+
fn finds_author_email_only() {
219+
create_empty_gitconfig();
220+
cargo_process("new foo")
221+
.env_remove("USER")
222+
.env_remove("USERNAME")
223+
.env_remove("NAME")
224+
.env_remove("EMAIL")
225+
.env("EMAIL", "baz")
226+
.run();
227+
228+
let toml = paths::root().join("foo/Cargo.toml");
229+
let contents = fs::read_to_string(&toml).unwrap();
230+
assert!(contents.contains(r#"authors = ["<baz>"]"#));
231+
}
232+
202233
#[cargo_test]
203234
fn finds_author_user_escaped() {
204235
create_empty_gitconfig();

0 commit comments

Comments
 (0)