Skip to content

Commit 6689848

Browse files
Add check for .env.local and module bindings generations to only write on changes (#3604)
# Description of Changes The `spacetime dev` and `spacetime generate` commands always overwrite the `.env.local` and `module_bindings` files, even if the contents are identical. This can be a nuisance if your client is watching for changes and reloading, as `spacetime dev` will modify both of those at different times in the process. This change will first read the contents of the files and see if a write is necessary. For writing `.env.local` as part of `spacetime dev`, the file contents were already read so no real impact should exist. For `module_bindings`, I've done some initial profiling with 181 generated typescript files and separately 161 rust files and noticed no performance impact. # API and ABI breaking changes None # Expected complexity level and risk 1 # Testing I've ran this code on a project that generates 181 source files for typescript and 161 for rust. It correctly writes files when changes have been made and leaves the files unmodified if no changes are made. For review testing, I would recommend running both with and without my change on a much more complex project if it's available. I would think not writing the contents of each file would mitigate the cost to read most of the time, but perhaps in certain project makeup scenarios and storage performance characteristics, this could possible be marginally slower sometimes, though I have not observed that to be the case. Co-authored-by: Tyler Cloutier <cloutiertyler@users.noreply.github.com>
1 parent e30d503 commit 6689848

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

crates/cli/src/subcommands/dev.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ fn upsert_env_db_names_and_hosts(env_path: &Path, server_host_url: &str, databas
323323
} else {
324324
String::new()
325325
};
326+
let original_contents = contents.clone();
326327

327328
for prefix in prefixes {
328329
for (suffix, value) in [("DB_NAME", database_name), ("HOST", server_host_url)] {
@@ -343,7 +344,9 @@ fn upsert_env_db_names_and_hosts(env_path: &Path, server_host_url: &str, databas
343344
contents.push('\n');
344345
}
345346

346-
fs::write(env_path, contents)?;
347+
if contents != original_contents {
348+
fs::write(env_path, contents)?;
349+
}
347350
Ok(())
348351
}
349352

crates/cli/src/subcommands/generate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ pub async fn exec_ex(
200200
fs::create_dir_all(out_dir.join(parent))?;
201201
}
202202
let path = out_dir.join(fname);
203-
fs::write(&path, code)?;
203+
if !path.exists() || fs::read_to_string(&path)? != code {
204+
fs::write(&path, code)?;
205+
}
204206
paths.insert(path);
205207
}
206208

0 commit comments

Comments
 (0)