Skip to content

Commit

Permalink
Add a newline to the generated JSON files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyurik committed Jun 28, 2023
1 parent 0c8fe72 commit fbe476e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sqlx-macros-core/src/query/data.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit fbe476e

Please sign in to comment.