From ab3a8e8456eb683cb4a66ca4120e976d75629b94 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 30 Jun 2023 22:18:56 +0200 Subject: [PATCH] Add a newline to the generated JSON files (#2570) This adds a newline at the end of all cached ./.sqlx/... JSON files so that anyone who has "auto-newline" enabled in their IDE would not accidentally add it to the cached file. This also ensures that GitHub diff would not show an alarming red icon next to the end of the checked in sqlx files. --- sqlx-macros-core/src/query/data.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sqlx-macros-core/src/query/data.rs b/sqlx-macros-core/src/query/data.rs index 1402f2ebd..207f97b7c 100644 --- a/sqlx-macros-core/src/query/data.rs +++ b/sqlx-macros-core/src/query/data.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::fmt::{Debug, Display, Formatter}; use std::fs; +use std::io::Write as _; use std::marker::PhantomData; use std::path::{Path, PathBuf}; use std::sync::Mutex; @@ -161,8 +162,15 @@ where // with persisting the file across filesystems. let mut tmp_file = tempfile::NamedTempFile::new_in(tmp_dir) .map_err(|err| format!("failed to create query file: {:?}", err))?; + serde_json::to_writer_pretty(tmp_file.as_file_mut(), self) .map_err(|err| format!("failed to serialize query data to file: {:?}", err))?; + // Ensure there is a newline at the end of the JSON file to avoid accidental modification by IDE + // and make github diff tool happier + tmp_file + .as_file_mut() + .write_all(b"\n") + .map_err(|err| format!("failed to append a newline to file: {:?}", err))?; tmp_file .persist(dir.as_ref().join(format!("query-{}.json", self.hash)))