Skip to content

Commit 166c556

Browse files
voidstackloopclaude
andcommitted
Fix report --out failing when its directory doesn't exist yet
Found by the community-monitor workflow's own first real run on GitHub Actions: "Build the dashboard's data index" failed with a bare "No such file or directory" writing results/index.json, because the prior step (an empty community-forms/ registry — nothing real registered yet) never created results/ at all, and `report --out` never created it either. This is the exact bug class already fixed once for `formwatch init subdir/file.yml` earlier this session — that fix just never reached `report`'s own two --out write sites (--json and --html). Extracted both fixes into one write_output() helper so there's a single place this gets right, and reused it from init() too instead of leaving two copies of the same logic. Verified with a test building on a fresh temp dir, then confirmed it's meaningful by stripping the parent-directory creation out of write_output() and watching it fail with the exact same "No such file or directory" error the real workflow run hit, before restoring it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 0740871 commit 166c556

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ the bug, not just reasoned about:
118118
reliably the first time `cargo test` ran on GitHub Actions' faster,
119119
more-parallel runner. Each launch now gets its own unique profile
120120
directory.
121+
- `formwatch report --out some/new/dir/file` (plain `--json` or
122+
`--html`) failed with a bare "No such file or directory" if that
123+
directory didn't exist yet — the same bug already fixed once for
124+
`formwatch init subdir/file.yml`, in a command that fix never reached.
125+
Found by the community-monitor workflow's own first real run, writing
126+
to a `results/` directory nothing had created yet.
121127

122128
### Changed
123129

‎src/main.rs‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ async fn main() -> Result<()> {
247247
Command::Report { html, json, out } => {
248248
if html {
249249
let path = out.unwrap_or_else(|| PathBuf::from("formwatch-report.html"));
250-
std::fs::write(&path, report::render_html(&history_dir)?)?;
250+
write_output(&path, &report::render_html(&history_dir)?)?;
251251
println!("Wrote {}", path.display());
252252
} else if json {
253253
let body = serde_json::to_string_pretty(&history::all_known_forms(&history_dir)?)?;
254254
match out {
255255
Some(path) => {
256-
std::fs::write(&path, body)?;
256+
write_output(&path, &body)?;
257257
println!("Wrote {}", path.display());
258258
}
259259
None => println!("{body}"),
@@ -289,6 +289,20 @@ fn paced<S: stream::Stream>(
289289
})
290290
}
291291

292+
/// Writes `contents` to `path`, creating its parent directory first if
293+
/// needed — `report --out some/new/dir/file.json` (or `--html`'s own
294+
/// `--out`) failing with a bare "No such file or directory" because
295+
/// nothing had created `some/new/dir/` yet is exactly the bug already
296+
/// fixed once for `formwatch init subdir/file.yml`; this is the same bug
297+
/// in a different command that fix never reached.
298+
fn write_output(path: &Path, contents: &str) -> Result<()> {
299+
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
300+
std::fs::create_dir_all(parent)
301+
.with_context(|| format!("creating {}", parent.display()))?;
302+
}
303+
std::fs::write(path, contents).with_context(|| format!("writing {}", path.display()))
304+
}
305+
292306
fn load_forms_config(path: &Path) -> Result<FormsConfig> {
293307
let text =
294308
std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
@@ -302,12 +316,7 @@ fn init(path: &Path) -> Result<()> {
302316
path.display()
303317
);
304318
}
305-
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
306-
std::fs::create_dir_all(parent)
307-
.with_context(|| format!("creating {}", parent.display()))?;
308-
}
309-
std::fs::write(path, STARTER_FORMS_YML)
310-
.with_context(|| format!("writing {}", path.display()))?;
319+
write_output(path, STARTER_FORMS_YML)?;
311320
println!("Wrote {}", path.display());
312321

313322
let checks_dir = Path::new("checks");
@@ -488,6 +497,25 @@ mod tests {
488497
let _ = std::fs::remove_dir_all(&dir);
489498
}
490499

500+
#[test]
501+
fn write_output_creates_a_new_directory_that_does_not_exist_yet() {
502+
// Found via a real deploy: `formwatch report --json --out
503+
// results/index.json` failed with a bare "No such file or
504+
// directory" the very first time it ran against a fresh
505+
// checkout, because nothing had created results/ yet — the same
506+
// bug class already fixed once for `formwatch init
507+
// subdir/file.yml`, in a command that fix never reached.
508+
let dir = std::env::temp_dir().join("formwatch-test-write-output");
509+
let _ = std::fs::remove_dir_all(&dir);
510+
let path = dir.join("nested").join("index.json");
511+
512+
write_output(&path, "[]").expect("write_output should create nested/ itself");
513+
514+
assert_eq!(std::fs::read_to_string(&path).expect("read back"), "[]");
515+
516+
let _ = std::fs::remove_dir_all(&dir);
517+
}
518+
491519
#[tokio::test]
492520
async fn paced_with_zero_delay_does_not_slow_the_stream() {
493521
let start = std::time::Instant::now();

0 commit comments

Comments
 (0)