8000 internal: use sync_all by Young-Flash · Pull Request #831 · moonbitlang/moon · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

internal: use sync_all #831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

internal: use sync_all #831

wants to merge 1 commit into from

Conversation

Young-Flash
Copy link
Collaborator

No description provided.

@Young-Flash Young-Flash enabled auto-merge (squash) May 28, 2025 03:50
Copy link
peter-jerry-ye-code-review bot commented May 28, 2025
Replacing flush() with sync_all() for more reliable file operations

Category
Correctness
Code Snippet
file.flush()
.context(format!("failed to flush {}", pj_path.display()))?;

// Changed to:
file.sync_all()?;
Recommendation
The change to sync_all() is good but should maintain the contextual error message:

file.sync_all()
.context(format!("failed to sync {}", pj_path.display()))?;
Reasoning
sync_all() is more robust than flush() as it ensures all data is written to disk and metadata is updated. However, dropping the context message makes debugging harder if errors occur.

Duplicate code in gen_doc_test_patch and gen_md_test_patch functions

Category
Maintainability
Code Snippet
Both functions contain identical file writing logic:
file.write_all(content.as_bytes())
.context(format!("failed to write to {}", pj_path.display()))?
file.sync_all()?;
Recommendation
Extract the common file writing logic into a separate function:

fn write_project_json(file: &mut File, content: String, path: &Path) -> anyhow::Result<()> {
file.write_all(content.as_bytes())
.context(format!("failed to write to {}", path.display()))?
file.sync_all()
.context(format!("failed to sync {}", path.display()))
}
Reasoning
Reducing code duplication improves maintainability and ensures consistent behavior across both test generation functions.

sync_all() may impact performance in test scenarios

Category
Performance
Code Snippet
file.sync_all()?;
Recommendation
Consider adding a configuration option to control sync behavior in test environments:

if config.force_sync {
file.sync_all().context(...)?
} else {
file.flush().context(...)?
}
Reasoning
sync_all() forces immediate disk writes which may not be necessary in all test scenarios and could slow down test execution. A configuration option would allow users to choose between safety and performance.

@@ -316,8 +315,7 @@ pub fn gen_md_test_patch(pkg: &Package, moonc_opt: &MooncOpt) -> anyhow::Result<
let content = serde_json_lenient::to_string_pretty(&pj)?;
file.write_all(content.as_bytes())
.context(format!("failed to write to {}", pj_path.display()))?;
file.flush()
.context(format!("failed to flush {}", pj_path.display()))?;
file.sync_all()?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Young-Flash Young-Flash force-pushed the use_sync_all branch 2 times, most recently from 63121a3 to 807d848 Compare May 29, 2025 06:18
auto-merge was automatically disabled May 30, 2025 10:18

Pull request was closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0