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

Use PrettyField to format cabal file in cabal init #6718

Closed
wants to merge 4 commits into from

Conversation

m-renaud
Copy link
Collaborator

@m-renaud m-renaud commented Apr 19, 2020

This PR replaces the hard-rolled cabal file formatting with PrettyField.

Notable differences:

  • Uses 4 space indent instead of 2
  • Long lists (extra-source-files, build-depends, etc.) are formatted nicely (not just one long list on a single line)

This resolves #5555.

Open Questions

Unspecified/any version range formatting

It appears not to be possible to pretty-print a dependency of the form: <package-name> with no version constraints, the only matching constructor for VersionRange is AnyVersion which is printed as <package-name> -any. This "unspecified" version range is needed to express a dependency from the package executable to the library within the same package (as is created in --libandexe).

executable foo
    build-depends:
        foo -any

Looking for input on a solution here, we could introduce a new UnspecifiedVersion constructor such that:

parsecSimple "" :: VersionRange
Just UnspecifiedVersion

pretty UnspecifiedVersion
Doc.empty

How to represent standalone comments in PrettyField

Currently (see below) I've created a new constructor in PrettyField for a "commented out" field that can also have comments. Should this be generalized to just PrettyComment? Thoughts on implementation here?

Implementation Notes

In order to re-use the pretty-printing logic I needed to expose the specific list format functions from FieldGrammar.hs. These functions mostly wrap list fields in newtypes so the pretty printer knows how to lay them out.

The one thing that was not easy to express with the existing PrettyField construct was commented out fields that also had comments associated with them. To address this I added a new constructor to the PrettyField type, namely PrettyFieldCommentedOut, this is pretty specific and should probably just be renamed to PrettyComment or similar, lmk what you think about this.

Testing

  • Cabal golden file (see updates)
  • Manual testing

Please include the following checklist in your PR:

  • Patches conform to the coding conventions.
  • Any changes that could be relevant to users have been recorded in the changelog (add file to changelog.d directory).
  • The documentation has been updated, if necessary.

Please also shortly describe how you tested your change. Bonus points for added tests!

@m-renaud m-renaud marked this pull request as draft April 19, 2020 18:10
@m-renaud m-renaud force-pushed the m-renaud-init-format branch 3 times, most recently from efbc3c7 to 6213832 Compare April 19, 2020 18:40
@m-renaud m-renaud force-pushed the m-renaud-init-format branch from 6213832 to de3eb91 Compare April 19, 2020 18:53
@m-renaud m-renaud requested a review from phadej April 19, 2020 19:06
@m-renaud m-renaud marked this pull request as ready for review April 19, 2020 19:07
@m-renaud m-renaud changed the title [WIP] Use PrettyField to format cabal file in cabal init Use PrettyField to format cabal file in cabal init Apr 19, 2020
Copy link
Collaborator

@phadej phadej left a comment

Choose a reason for hiding this comment

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

Good start!

cabal-install/Distribution/Client/Init/Utils.hs Outdated Show resolved Hide resolved
cabal-install/Distribution/Client/Init/FileCreators.hs Outdated Show resolved Hide resolved
Cabal/Distribution/Fields/Pretty.hs Outdated Show resolved Hide resolved
Cabal/Distribution/PackageDescription/FieldGrammar.hs Outdated Show resolved Hide resolved
cabal-install/Distribution/Client/Init/FileCreators.hs Outdated Show resolved Hide resolved
@m-renaud m-renaud force-pushed the m-renaud-init-format branch from de3eb91 to 03faa1a Compare April 21, 2020 01:40
@phadej
Copy link
Collaborator

phadej commented Apr 21, 2020

About Unspecified/any version range formatting,

I think we can change how Dependency as pretty-printed. Try to modify its Pretty instance.

cabal run hackage-tests roundtrip packagenameprefix

will run roundtrip tests on Hackage data, so if it passes, I'm confident it's ok.

I don't like extra -any, so I'm 👍 about getting rid of them.

EDIT: we don't need to worry about other usages of Pretty Dependency, as most likely Dependency is wrong type there (and something like PackageVersionConstraint should be used instead. If we discover those use cases as a side-effect, we can fix them later.

@m-renaud m-renaud force-pushed the m-renaud-init-format branch from 03faa1a to 346e3e5 Compare May 4, 2020 02:32
Copy link
Collaborator Author

@m-renaud m-renaud left a comment

Choose a reason for hiding this comment

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

Alright, think I addressed all your comments, lmk if I missed anything!

@phadej
Copy link
Collaborator

phadej commented May 4, 2020

7.10.3 failure is legit

@m-renaud
Copy link
Collaborator Author

m-renaud commented May 5, 2020

Huh, interesting. The failure is on line 278 in the prependPrefix helper function in mainHs which wasn't touched at all in this PR, even transitively :/ Do you know what's up with that @phadej ?

Also, when trying to build with ghc-7.10.3 locally cryptohash-sha256-0.11.101.0 fails to build:

Build log ``` Failed to build cryptohash-sha256-0.11.101.0. Build log ( /home/mrenaud/.cabal/logs/ghc-7.10.3/cryptohash-sha256-0.11.101.0-541559c14cc76365a19f26bf3fd7c60613e1870c1ce0f5cd3363cf2a9b76d027.log ): Configuring library for cryptohash-sha256-0.11.101.0.. Warning: Packages using 'cabal-version: >= 1.10' must specify the 'default-language' field for each component (e.g. Haskell98 or Haskell2010). If a component uses different languages in different modules then list the other ones in the 'other-languages' field. Preprocessing library for cryptohash-sha256-0.11.101.0.. Building library for cryptohash-sha256-0.11.101.0.. [1 of 2] Compiling Crypto.Hash.SHA256.FFI ( src/Crypto/Hash/SHA256/FFI.hs, dist/build/Crypto/Hash/SHA256/FFI.o ) /usr/bin/ld: -r and -pie may not be used together collect2: error: ld returned 1 exit status cabal: Failed to build cryptohash-sha256-0.11.101.0 (which is required by test:unit-tests from cabal-install-3.3.0.0, test:solver-quickcheck from cabal-install-3.3.0.0 and others). See the build log above for details. ```

Appears to be a poor interaction with OverloadedStrings in ghc 7.10.3.
@m-renaud
Copy link
Collaborator Author

m-renaud commented May 6, 2020

Looks like a poor interaction between OverloadedStrings and ghc 7.10.3.

@m-renaud m-renaud requested a review from phadej May 6, 2020 23:07
@phadej
Copy link
Collaborator

phadej commented May 7, 2020

I added changelog and merged (squashed) as 10051c3

@phadej phadej closed this May 7, 2020
@phadej phadej mentioned this pull request Jul 10, 2020
@phadej phadej added this to the 3.4.0.0-rc1 milestone Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cabal init generates badly formatted file
2 participants