Skip to content

Commit

Permalink
Auto merge of #11321 - FrankYang0529:fix-11311, r=epage
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Nov 11, 2022
2 parents c8b090c + b98c534 commit a3dfea7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::core::{Edition, Shell, Workspace};
use crate::util::errors::CargoResult;
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
use crate::util::{restricted_names, Config};
use anyhow::Context as _;
use anyhow::{anyhow, Context as _};
use cargo_util::paths;
use serde::de;
use serde::Deserialize;
Expand Down Expand Up @@ -595,9 +595,22 @@ impl IgnoreList {
/// already exists. It reads the contents of the given `BufRead` and
/// checks if the contents of the ignore list are already existing in the
/// file.
fn format_existing<T: BufRead>(&self, existing: T, vcs: VersionControl) -> String {
// TODO: is unwrap safe?
let existing_items = existing.lines().collect::<Result<Vec<_>, _>>().unwrap();
fn format_existing<T: BufRead>(&self, existing: T, vcs: VersionControl) -> CargoResult<String> {
let mut existing_items = Vec::new();
for (i, item) in existing.lines().enumerate() {
match item {
Ok(s) => existing_items.push(s),
Err(err) => match err.kind() {
ErrorKind::InvalidData => {
return Err(anyhow!(
"Character at line {} is invalid. Cargo only supports UTF-8.",
i
))
}
_ => return Err(anyhow!(err)),
},
}
}

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

out
Ok(out)
}
}

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

paths::append(&fp_ignore, ignore.as_bytes())?;
Expand Down
20 changes: 20 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,23 @@ fn git_default_branch() {
let head = repo.find_reference("HEAD").unwrap();
assert_eq!(head.symbolic_target().unwrap(), "refs/heads/hello");
}

#[cargo_test]
fn non_utf8_str_in_ignore_file() {
let gitignore = paths::home().join(".gitignore");
File::create(gitignore).unwrap();

fs::write(paths::home().join(".gitignore"), &[0xFF, 0xFE]).unwrap();

cargo_process(&format!("init {} --vcs git", paths::home().display()))
.with_status(101)
.with_stderr(
"\
error: Failed to create package `home` at `[..]`
Caused by:
Character at line 0 is invalid. Cargo only supports UTF-8.
",
)
.run();
}

0 comments on commit a3dfea7

Please sign in to comment.