Skip to content
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

feat: tempfile without flush or close #101

Conversation

iamnamananand996
Copy link
Contributor

@iamnamananand996 iamnamananand996 commented Feb 12, 2024

Be cautious when using $F.name without preceding it with .flush() or .close(), as it may result in an error. This is because the file referenced by $F.name might not exist at the time of use. To prevent issues, ensure that you either call .flush() to write any buffered data to the file or close the file with .close() before referencing $F.name.

grit studio link: https://app.grit.io/studio?key=yeST0wXjbRx38MojeG0jn

title: Warning for tempfile without flush
---

"Be cautious when using `$F.name` without preceding it with `.flush()` or `.close()`, as it may result in an error. This is because the file referenced by `$F.name` might not exist at the time of use. To prevent issues, ensure that you either call `.flush()` to write any buffered data to the file or close the file with .close() before referencing `$F.name`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't just copy and paste quotes from the docs. At the very least the leading quote mark isn't helpful and confuses this.

@@ -0,0 +1,227 @@
---
title: Warning for tempfile without flush
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a shorter file name. And don't call it a "warning" - the level of a diagnostic is decided by the consumer: https://docs.grit.io/guides/config#patterns

$fileOpen <: not contains `$f.flush()`,
$fileOpen <: not contains `$f.close()`,
$fileOpen <: contains `$f.name`,
$fileOpen => `# BAD: should add $f.flush() or $f.close() before calling $f.name \n $fileOpen`
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't manually add annotations like this via rewrites. Grit already has support for checking things - if no meaningful fix is available, you can skip having a rewrite at all.

In this case, a meaningful autofix might be inserting f.close() on the line before the f.name() call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @morgante

In this case, a meaningful autofix might be inserting f.close() on the line before the f.name() call.

Yes, true I am trying to build the same.

any suggestion, if to build this,

cmd = [binary_name, fout.name, *[str(path) for path in targets]]

parsing this and add $f.close() before this, become difficult

Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to handle all cases. It's fine to skip any that are too complex.

For inserting things it's often best to find the containing statement() and append to that.

@iamnamananand996 iamnamananand996 changed the title feat: warning for tempfile without flush feat: tempfile without flush or close Feb 29, 2024
@iamnamananand996
Copy link
Contributor Author

Hi @morgante, this PR ready for review.

@morgante morgante merged commit ca3ac14 into getgrit:main Feb 29, 2024
5 checks passed
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.

2 participants