Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/cli/src/subcommands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ fn upsert_env_db_names_and_hosts(env_path: &Path, server_host_url: &str, databas
} else {
String::new()
};
let original_contents = contents.clone();

for prefix in prefixes {
for (suffix, value) in [("DB_NAME", database_name), ("HOST", server_host_url)] {
Expand All @@ -335,7 +336,9 @@ fn upsert_env_db_names_and_hosts(env_path: &Path, server_host_url: &str, databas
contents.push('\n');
}

fs::write(env_path, contents)?;
if contents != original_contents {
fs::write(env_path, contents)?;
}
Ok(())
}

Expand Down
5 changes: 4 additions & 1 deletion crates/cli/src/subcommands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ pub async fn exec_ex(
fs::create_dir_all(out_dir.join(parent))?;
}
let path = out_dir.join(fname);
fs::write(&path, code)?;
let contents = fs::read_to_string(&path);
if contents.is_err() || contents.is_ok_and(|contents| code != contents) {
fs::write(&path, code)?;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior is slightly different than in the dev command. Since in the following

    let mut contents = if env_path.exists() {
        fs::read_to_string(env_path)?
    } else {
        String::new()
    };

if the file exists but there is file read error we will not write the file out, for example PermissionDenied which would cause the program to try to write even if it can't read. Could you modify this to have the same behavior as above?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the expectations for the two types of files be somewhat different?

If .env.local exists but we can't read it, we shouldn't clobber over the existing file contents and should exit with error. spacetime dev (likely) isn't the only source of changes to that file.

For module_bindings however, this spacetime generate/dev IS likely the only source of changes. If we can't read the file, that's probably fine and we should attempt to write it still.

If you want, I can still make the change so that if a module_binding file exists but is unreadable, we exit out. That scenario is definitely an edge case so I see the value in making it consistent with how .env.local is handled.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloutiertyler Can you take a look at this comment? I'm not sure if making .env.local file existence checking the same as module_bindings is the right call here, but happy to implement if you still think it's correct.

paths.insert(path);
}

Expand Down