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

Add flag to check if files are formatted #92

Merged
merged 1 commit into from Nov 2, 2023
Merged

Add flag to check if files are formatted #92

merged 1 commit into from Nov 2, 2023

Conversation

ratsclub
Copy link
Contributor

This is a follow-up PR for #91.

About the flag name, what do you think of --dryrun?

@ratsclub
Copy link
Contributor Author

Another issue I'm facing is that the pretty output doesn't have a break line at the end of the file. Do you think we should trim the original one in order to compare?

@shwestrick
Copy link
Owner

Another issue I'm facing is that the pretty output doesn't have a break line at the end of the file. Do you think we should trim the original one in order to compare?

I think the formatted output should always be exactly result ^ "\n" -- does that work?

@shwestrick
Copy link
Owner

shwestrick commented Nov 1, 2023

About the flag name, what do you think of --dryrun?

I like it, although I think the name "dry run" suggests that it will tell us exactly what changes a normal run would make. (For example, git clean --dry-run.)

This wouldn't be a huge change; it would just require not immediately failing on the first file, and instead allowing the tool to continue.

The output could be something like:

Would modify path/to/foo.sml
Would modify bar.sml

Thoughts?

@shwestrick shwestrick mentioned this pull request Nov 1, 2023
@ratsclub
Copy link
Contributor Author

ratsclub commented Nov 1, 2023

@shwestrick Would you be open to give this the --check flag? Other tools that use it:

In this case, I suggest changing the current --check to --safety-check.

@shwestrick
Copy link
Owner

@shwestrick Would you be open to give this the --check flag? Other tools that use it:

Oh excellent, I like it. Let's use --check then.

In this case, I suggest changing the current --check to --safety-check.

Sounds good to me!

@ratsclub
Copy link
Contributor Author

ratsclub commented Nov 1, 2023

@shwestrick May you check if this is good enough, please?

@shwestrick
Copy link
Owner

shwestrick commented Nov 2, 2023

Just did a little testing and found a bug. (This file should pass.)

$ ./smlfmt --check src/base/MLtonPathMap.sml
ERROR: Unformatted file 'src/base/MLtonPathMap.sml'

The issue appears to be on this line:

val original = TextIO.input (TextIO.openIn hfp)

This is subtle, but TextIO.input doesn't guarantee that you get all the input.

If I switch to this instead, it seems to work:

val original = ReadFile.contents hfp

(ReadFile.contents is usually faster, gets the entire contents, and also guarantees that the opened file is closed before returning.)

Could you make that change? Then I think we're good to merge.

@ratsclub
Copy link
Contributor Author

ratsclub commented Nov 2, 2023

This is subtle, but TextIO.input doesn't guarantee that you get all the input.

This is surprising and good to know. Do you have any place where I can read more about that?

Could you make that change? Then I think we're good to merge.

Done.

@shwestrick
Copy link
Owner

The official docs for TextIO.input are a bit vague but technically allow for this behavior: "... returns a vector of at least one element"

Moscow ML's description here is perhaps at little better: "... returns some elements from [the stream]"

My understanding is that this function has a very particular intended mode-of-use, where you call TextIO.input(...) in a loop until it returns an empty string. This would let you incrementally process characters from a large file without needing the entire string in memory all at once. (There is a separate function TextIO.inputAll which gets you the entire string.)

The naming is unfortunate (the name input is unassuming but the function is very niche), and I wish the docs were more clear. Perhaps it all made a bit more sense back in the 80s and 90s when the Basis Library was designed. In 2023, I find it a bit strange. Oh well 🤷‍♂️

@shwestrick
Copy link
Owner

Thanks for putting this together, it looks great! Merging now.

@shwestrick shwestrick merged commit 8bd9cbc into shwestrick:main Nov 2, 2023
@ratsclub ratsclub deleted the 1-introduce-flag-for-ci-usage branch November 2, 2023 14:48
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.

None yet

2 participants