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

Auto format dune-project files with dune fmt #10716

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

maxRN
Copy link
Contributor

@maxRN maxRN commented Jul 10, 2024

Hi!

This PR enables automatic formatting of dune-project files when running dune fmt. This should close #3223. Note that there were some concerns raised on that issue regarding the formatting, but those should have been addressed by #3928, see also #2291.

Note: I'm new to OCaml, especially working on such a mature codebase so there will probably a lot of mistakes in my submission. I hope it can still be of use and I'm more than happy to learn and address any problems with this PR.

Signed-off-by: Max Große <max12.gro@gmail.com>
Signed-off-by: Max Große <max12.gro@gmail.com>
| true ->
Dune_project.file project
|> Memo.Option.iter ~f:(fun path ->
if String.equal "_build/default" (Path.Build.to_string dir)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not happy with this if check for the hard-coded build directory, but I didn't find a way to check if we are in the "_build/default" directory other than this.

Copy link
Member

Choose a reason for hiding this comment

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

How about checking it for equality against Context.build_dir?

@maxRN
Copy link
Contributor Author

maxRN commented Jul 10, 2024

EDIT: No longer relevant.
I'm not sure why the Format CI step fails. It runs nix develop .#fmt -c make fmt right? When I run this on my machine I don't get any errors so I'm not sure why it's failing in CI.

@emillon
Copy link
Collaborator

emillon commented Jul 11, 2024

Thanks.
I can't give a full review at the moment but here are a few remarks in the meantime:

  • In terms of design, I don't think that this should be tied to the formatting of dune files. Maybe it's useful to be able to disable the formatting of just dune-project files, for example.
  • This will need to be versioned: if (lang dune) is < (3, 17), do not format these files, otherwise do format them by default.
  • I'm not sure the formatting has been fixed enough to work well with dune-project files, but that's something we can try and improve later.

@maxRN
Copy link
Contributor Author

maxRN commented Jul 11, 2024

Thanks. I can't give a full review at the moment but here are a few remarks in the meantime:

  • In terms of design, I don't think that this should be tied to the formatting of dune files. Maybe it's useful to be able to disable the formatting of just dune-project files, for example.
  • This will need to be versioned: if (lang dune) is < (3, 17), do not format these files, otherwise do format them by default.
  • I'm not sure the formatting has been fixed enough to work well with dune-project files, but that's something we can try and improve later.

Thanks for the feedback! I'll take another look

Signed-off-by: Max Große <max12.gro@gmail.com>
Signed-off-by: Max Große <max12.gro@gmail.com>
@maxRN maxRN force-pushed the dune-fmt-dune-project-files branch from 416edcc to c47472e Compare July 13, 2024 10:06
Signed-off-by: Max Große <max12.gro@gmail.com>
Signed-off-by: Max Große <max12.gro@gmail.com>
Signed-off-by: Max Große <max12.gro@gmail.com>
Signed-off-by: Max Große <max12.gro@gmail.com>
Signed-off-by: Max Große <max12.gro@gmail.com>
@@ -190,7 +190,8 @@ Sometimes, the suggestion is to just remove the configuration.
5 | (using fmt 1.2)
^^^^^^^^^^^^^^^
Error: Starting with (lang dune 2.0), formatting is enabled by default.
To port it to the new syntax, you can delete this part.
To port it to the new syntax, you can replace this part by:
(formatting (enabled_for dune ocaml reason))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unsure whether we should extend the help message by mentioning the differences between > 3.17 dune and 3.17+
i.e. that you can remove the stanza, but that it will also format dune-project files.

@maxRN
Copy link
Contributor Author

maxRN commented Jul 14, 2024

@emillon I've added a version check so it will only automatically format from 3.17 (?) onwards. There's no explicit way to disable just formatting for dune-project files, but just like before users can just specify the file types that they want to be formatted automatically and not include dune-project.
One more thing I thought about while implementing this: I think it would make sense to also include dune-workspace files in this PR. I don't know what the state on their formatting is, but it would be a lot easier to add support for both file types in one change then having to go back later and add support.

@@ -14,27 +14,35 @@ module Language = struct
type t =
| Dialect of string
| Dune
| DuneProject
Copy link
Member

Choose a reason for hiding this comment

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

Dune_project is the convention

@rgrinberg
Copy link
Member

Can you enable this feature for dune itself? IIRC, there were some problems with preserving multi line quoted strings in the dune language.

@maxRN
Copy link
Contributor Author

maxRN commented Jul 14, 2024

Can you enable this feature for dune itself? IIRC, there were some problems with preserving multi line quoted strings in the dune language.

Sorry, I don't think I understand the question. What feature exactly are you referring to?

@rgrinberg
Copy link
Member

Dune itself has a dune-project file. It would be good to see how this PR works to auto-format it.

Signed-off-by: Max Große <max12.gro@gmail.com>
Signed-off-by: Max Große <max12.gro@gmail.com>
@maxRN
Copy link
Contributor Author

maxRN commented Jul 14, 2024

Ahh, I see what you mean. I added the formatted dune-project file for reference. NOT intended to be included in the final PR. You are right, there are some problems with multiline strings - the newlines are output verbatim instead of being actual newline characters.
My gut feeling is that this should be an easy fix, do you want me to tackle this in this PR as well or create a new one so that we can discuss this separately?

@rgrinberg
Copy link
Member

If you could fix it in a separate PR that would be great. It's really an issue for all dune files, but project files include large description strings fairly often.

@maxRN
Copy link
Contributor Author

maxRN commented Jul 17, 2024

Alright, I'm investigating some possible solutions and will try to create a separate draft PR in the coming days.

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.

dune-project not formatted with @fmt
3 participants