From e8c9ffe8ded733c9980bb4effc5cd721266a4eeb Mon Sep 17 00:00:00 2001 From: Greg Hurrell Date: Tue, 7 Jun 2022 14:05:47 -0700 Subject: [PATCH] fix(compiler): make LocalPersister include trailing newline (#3938) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: We're seeing noise in our diffs because the LocalPersister does not include a trailing newline in its JSON output, but many editors append one automatically (for example, when people inspect the file): image Presumably, editors are doing this to be "helpful" because [POSIX defines a "line"](https://stackoverflow.com/a/729795/2103996) as (paraphrasing) "some or no non-newline stuff followed by a newline", but the unexpected diff output can be confusing for developers working in a large project — many of whom may not even know what persisted queries are. This PR makes a small edit to append the newline automatically to Serde JSON's pretty output. Pull Request resolved: https://github.com/facebook/relay/pull/3938 Reviewed By: alunyov Differential Revision: D36937182 Pulled By: captbaritone fbshipit-source-id: 14341811f1885fa2750d1b18576f06b783ea88fa --- .../src/operation_persister/local_persister.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs b/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs index a7dcc1c3cd8c8..75d8b91324830 100644 --- a/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs +++ b/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs @@ -13,6 +13,8 @@ use relay_config::{LocalPersistAlgorithm, LocalPersistConfig}; use sha1::{Digest, Sha1}; use sha2::Sha256; use std::collections::BTreeMap; +use std::fs::File; +use std::io::{BufWriter, Write}; use crate::OperationPersister; @@ -76,9 +78,10 @@ impl OperationPersister for LocalPersister { .map(|x| (x.key().clone(), x.value().clone())) .collect(); - let content = serde_json::to_string_pretty(&ordered)?; - std::fs::write(&self.config.file, content)?; - + let mut writer = BufWriter::new(File::create(&self.config.file)?); + serde_json::to_writer_pretty(&mut writer, &ordered)?; + writer.write_all(b"\n")?; + writer.flush()?; Ok(()) } }