mirror of
https://github.com/denoland/deno.git
synced 2024-10-29 08:58:01 -04:00
fix(cli/fmt): show filepath for InvalidData error (#17361)
This commit is contained in:
parent
7683ba5e90
commit
df4d0c55c0
4 changed files with 12 additions and 1 deletions
|
@ -189,6 +189,12 @@ itest!(fmt_check_parse_error {
|
|||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(fmt_check_invalid_data {
|
||||
args: "fmt --check fmt/invalid_data.json",
|
||||
output: "fmt/invalid_data.out",
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(fmt_stdin {
|
||||
args: "fmt -",
|
||||
input: Some("const a = 1\n"),
|
||||
|
|
BIN
cli/tests/testdata/fmt/invalid_data.json
vendored
Normal file
BIN
cli/tests/testdata/fmt/invalid_data.json
vendored
Normal file
Binary file not shown.
1
cli/tests/testdata/fmt/invalid_data.out
vendored
Normal file
1
cli/tests/testdata/fmt/invalid_data.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
error: [WILDCARD] is not a valid UTF-8 file
|
|
@ -20,6 +20,7 @@ use crate::util::fs::FileCollector;
|
|||
use crate::util::path::get_extension;
|
||||
use crate::util::text_encoding;
|
||||
use deno_ast::ParsedSource;
|
||||
use deno_core::anyhow::anyhow;
|
||||
use deno_core::anyhow::bail;
|
||||
use deno_core::anyhow::Context;
|
||||
use deno_core::error::generic_error;
|
||||
|
@ -574,7 +575,10 @@ fn read_file_contents(file_path: &Path) -> Result<FileContents, AnyError> {
|
|||
let file_bytes = fs::read(file_path)
|
||||
.with_context(|| format!("Error reading {}", file_path.display()))?;
|
||||
let charset = text_encoding::detect_charset(&file_bytes);
|
||||
let file_text = text_encoding::convert_to_utf8(&file_bytes, charset)?;
|
||||
let file_text = text_encoding::convert_to_utf8(&file_bytes, charset)
|
||||
.map_err(|_| {
|
||||
anyhow!("{} is not a valid UTF-8 file", file_path.display())
|
||||
})?;
|
||||
let had_bom = file_text.starts_with(text_encoding::BOM_CHAR);
|
||||
let text = if had_bom {
|
||||
text_encoding::strip_bom(&file_text).to_string()
|
||||
|
|
Loading…
Reference in a new issue