sol / hpack

hpack: A modern format for Haskell packages
MIT License
624 stars 103 forks source link

Add a format command. #341

Closed philderbeast closed 4 years ago

philderbeast commented 6 years ago

Please add a format command to hpack with an --inplace option to overwrite the package.yaml file.

hpack format --inplace

If a format command was added to hpack then use of this command could help to reduce the chance of git conflicts when rebasing/merging.

Differences

I'm currently adding some real world golden tests for the executable dhall-hpack-yaml from package hpack-dhall, trying to generate output that matches hpack's package.yaml and stack's package.yaml. The kinds of differences I've seen between handwritten and generated package.yaml files are;

dependencies:

maintainer: Simon Hengel sol@typeful.net github: sol/hpack category: Development extra-source-files:

dependencies:

executable: main: Main.hs source-dirs: driver dependencies:

tests: spec:

* Version numbers

name: stack version: ! '''1.10.0'''

name: stack version: '1.10.0'

name: hpack version: 0.31.1

sol commented 6 years ago

Hey! Thanks for opening this issue!

I think this can be provided as a third-party executable. Please feel free to do so.

Regarding adding this to hpack itself: Unless somebody can make the case on how this benefits hpack users at large, I'm not inclined to do so.

tfausak commented 6 years ago

In the past I've used a separate YAML pretty printer for this and it's worked fine.

philderbeast commented 6 years ago

I look for fields like name and version at the head of a package.yaml file or .cabal files and expect to find exposed-modules before other-modules and library before test-suite. This is the order fields are listed in the top-level table and other tables in the hpack docs.

I have implemented a sorting of sorts for hpack field names following the hpack docs.

Can you please consider exposing a field name comparison function in hpack itself as a lesser suggestion?

sol commented 4 years ago

@philderbeast for your specific used case, one thing I that could be worth trying is reading package.yaml into a Value and then compare it based on that.

I'm closing this issue, but please, if you need any changes to hpacks API, please bring them up and I'm open to discuss (no promises).

wraithm commented 3 months ago

@tfausak, what separate yaml pretty printer do you use? We use the imported anchor features pretty extensively in our package.yaml files. This messes up every single yaml formatter I've tried.

Here's an example of an error with prettier, but this is basically what I get with any of the other formatters:

$ prettier package.yaml
package.yaml
[error] package.yaml: SyntaxError: Aliased anchor not found: defaults (5:5)
[error] > 5 | <<: *defaults
[error]     |     ^^^^^^^^^
tfausak commented 3 months ago

I use yq, which I think should handle that.