diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index b9bb6a2162..4fae8d33b8 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -730,6 +730,21 @@ fn script_file_copy_of_an_open_file_has_its_changes() { let _ = std::fs::remove_dir_all(base); } +/// A file whose `close` could not write it stays open, so a later `close` writes it. +#[test] +fn script_file_close_that_fails_can_be_tried_again() { + let base = folder("closeagain"); + let r = run( + &base, + "var d = Folder.temp.fsName, f = new File(d + '/f.txt'); f.open('w'); f.write('kept');\n\ + new File(f.fsName).readonly = true; var out = [f.close(), f.error];\n\ + new File(f.fsName).readonly = false; out.push(f.close());\n\ + var g = new File(f.fsName); g.open('r'); out.push(g.read()); g.close(); vc.log(out);", + ); + assert_eq!(r["log"], json!([[false, "Script file could not be written", true, "kept"]])); + let _ = std::fs::remove_dir_all(base); +} + /// `copy` gives the copy the source's permissions: the copy of a read-only file is read-only. #[test] fn script_file_copy_keeps_the_source_read_only() { diff --git a/crates/js/src/file_classes.rs b/crates/js/src/file_classes.rs index 0d8d997a91..7ebcc7bfae 100644 --- a/crates/js/src/file_classes.rs +++ b/crates/js/src/file_classes.rs @@ -220,14 +220,17 @@ fn is_open(i: &Interp, o: ObjId) -> JsResult> { } /// Closes the object's open file, if it has one, which writes what the script wrote to it. → false -/// after setting `error` when that could not be written. +/// after setting `error` when that could not be written; the file then stays open, so a later +/// `close` tries again. fn close_open(i: &mut Interp, o: ObjId) -> JsResult { let mut written = true; if let Some(h) = is_open(i, o)? { let r = host(i, "close", &[h])?; written = outcome(i, o, &r)?; } - set_slot(i, o, HANDLE, Value::Number(-1.0))?; + if written { + set_slot(i, o, HANDLE, Value::Number(-1.0))?; + } Ok(written) } diff --git a/docs/mcp.md b/docs/mcp.md index 8fefe75b07..1b642340cb 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2300,7 +2300,8 @@ VectorCraft writes files by these rules of its own: `Script file access denied`. - What a script writes is written when it closes the file or when the run ends, also when the run fails. At the run's end the files left open are written in the order they were opened. A file - that `close` could not write is written again when the run ends, and a `write` that would pass the run's limit + that `close` could not write stays open, so a later `close` tries again, and is written again when the run ends; a + `write` that would pass the run's limit is not written. A run that would otherwise succeed fails when a changed file it left open cannot be written, with `Error: the script left open files that could not be written` and the files' paths. One file