Skip to content

Commit a3dfea7

Browse files
committed
Auto merge of #11321 - FrankYang0529:fix-11311, r=epage
fix: return non UTF-8 error message Fixes #11311 ### Test Steps 1. Create a new empty Git repository 2. Create a `.gitignore` that is not valid UTF-8, for instance `printf '\xFF\xFE' > .gitignore` 3. `cargo init`
2 parents c8b090c + b98c534 commit a3dfea7

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::core::{Edition, Shell, Workspace};
22
use crate::util::errors::CargoResult;
33
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
44
use crate::util::{restricted_names, Config};
5-
use anyhow::Context as _;
5+
use anyhow::{anyhow, Context as _};
66
use cargo_util::paths;
77
use serde::de;
88
use serde::Deserialize;
@@ -595,9 +595,22 @@ impl IgnoreList {
595595
/// already exists. It reads the contents of the given `BufRead` and
596596
/// checks if the contents of the ignore list are already existing in the
597597
/// file.
598-
fn format_existing<T: BufRead>(&self, existing: T, vcs: VersionControl) -> String {
599-
// TODO: is unwrap safe?
600-
let existing_items = existing.lines().collect::<Result<Vec<_>, _>>().unwrap();
598+
fn format_existing<T: BufRead>(&self, existing: T, vcs: VersionControl) -> CargoResult<String> {
599+
let mut existing_items = Vec::new();
600+
for (i, item) in existing.lines().enumerate() {
601+
match item {
602+
Ok(s) => existing_items.push(s),
603+
Err(err) => match err.kind() {
604+
ErrorKind::InvalidData => {
605+
return Err(anyhow!(
606+
"Character at line {} is invalid. Cargo only supports UTF-8.",
607+
i
608+
))
609+
}
610+
_ => return Err(anyhow!(err)),
611+
},
612+
}
613+
}
601614

602615
let ignore_items = match vcs {
603616
VersionControl::Hg => &self.hg_ignore,
@@ -631,7 +644,7 @@ impl IgnoreList {
631644
out.push('\n');
632645
}
633646

634-
out
647+
Ok(out)
635648
}
636649
}
637650

@@ -660,7 +673,7 @@ fn write_ignore_file(base_path: &Path, list: &IgnoreList, vcs: VersionControl) -
660673
Some(io_err) if io_err.kind() == ErrorKind::NotFound => list.format_new(vcs),
661674
_ => return Err(err),
662675
},
663-
Ok(file) => list.format_existing(BufReader::new(file), vcs),
676+
Ok(file) => list.format_existing(BufReader::new(file), vcs)?,
664677
};
665678

666679
paths::append(&fp_ignore, ignore.as_bytes())?;

tests/testsuite/new.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,23 @@ fn git_default_branch() {
509509
let head = repo.find_reference("HEAD").unwrap();
510510
assert_eq!(head.symbolic_target().unwrap(), "refs/heads/hello");
511511
}
512+
513+
#[cargo_test]
514+
fn non_utf8_str_in_ignore_file() {
515+
let gitignore = paths::home().join(".gitignore");
516+
File::create(gitignore).unwrap();
517+
518+
fs::write(paths::home().join(".gitignore"), &[0xFF, 0xFE]).unwrap();
519+
520+
cargo_process(&format!("init {} --vcs git", paths::home().display()))
521+
.with_status(101)
522+
.with_stderr(
523+
"\
524+
error: Failed to create package `home` at `[..]`
525+
526+
Caused by:
527+
Character at line 0 is invalid. Cargo only supports UTF-8.
528+
",
529+
)
530+
.run();
531+
}

0 commit comments

Comments
 (0)