summaryrefslogtreecommitdiff
path: root/cli/fmt.rs
diff options
context:
space:
mode:
authorMaayan Hanin <maayan.asa.hanin@gmail.com>2020-08-04 00:39:48 +0300
committerGitHub <noreply@github.com>2020-08-03 23:39:48 +0200
commit5fc5e7b54a9fba421dfc473016625a4f592403ed (patch)
treee66bbbdaa30b34b7cef8072ded8ea3f0575c47f0 /cli/fmt.rs
parentd615ebefe2e306f2877afb40dc603f71263407d6 (diff)
fix(cli): add support for non-UTF8 source files (#6789)
Fixes: #5542
Diffstat (limited to 'cli/fmt.rs')
-rw-r--r--cli/fmt.rs7
1 files changed, 5 insertions, 2 deletions
diff --git a/cli/fmt.rs b/cli/fmt.rs
index 70bc0e8bc..319f7fece 100644
--- a/cli/fmt.rs
+++ b/cli/fmt.rs
@@ -11,6 +11,7 @@ use crate::colors;
use crate::diff::diff;
use crate::fs::files_in_subtree;
use crate::op_error::OpError;
+use crate::text_encoding;
use deno_core::ErrBox;
use dprint_plugin_typescript as dprint;
use std::fs;
@@ -247,13 +248,15 @@ struct FileContents {
}
fn read_file_contents(file_path: &PathBuf) -> Result<FileContents, ErrBox> {
- let file_text = fs::read_to_string(&file_path)?;
+ let file_bytes = fs::read(&file_path)?;
+ let charset = text_encoding::detect_charset(&file_bytes);
+ let file_text = text_encoding::convert_to_utf8(&file_bytes, charset)?;
let had_bom = file_text.starts_with(BOM_CHAR);
let text = if had_bom {
// remove the BOM
String::from(&file_text[BOM_CHAR.len_utf8()..])
} else {
- file_text
+ String::from(file_text)
};
Ok(FileContents { text, had_bom })