Create parent directories when saving to a non-existent path #738
+42
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Main Problem:
When saving a file to a path where one or more parent directories did not exist, the application would fail with an error (e.g., "The system cannot find the path specified"). This prevented users from saving files in new folders directly, which is a common and expected workflow in modern editors like VS Code.
How We Solved It:
We updated the file-saving logic so that, before attempting to create or write a file, the application checks if the parent directory exists. If it does not, it automatically creates all necessary parent directories using Rust’s
std::fs::create_dir_all. This ensures that saving to a new path always works, even if the directory structure does not exist yet.Function Before the Change
Function After the Change
Test Results
test_parse_last_numbers(documents.rs)tui.rs,stdext arena::debug::ArenaAdditional Test: Error Handling in Directory Creation
A new unit test was added to ensure that the error handling logic in
open_for_writingworks as expected. This test attempts to create a file in a temporary directory (which should always succeed), and asserts that the function returnsOk. This guarantees that the error handling path is exercised in a safe and CI-friendly way.