tfausak / cabal-gild

:crown: Format Haskell package descriptions.
https://hackage.haskell.org/package/cabal-gild
MIT License
49 stars 5 forks source link

Sort nested elements #45

Closed tfausak closed 8 months ago

tfausak commented 8 months ago

Some things that are sorted can have other elements contained within them. For example:

build-depends: p:{l1, l2}
mixins: p(M1 as A1, M2 as A2)
-- probably other fields as well

Currently those interior elements are not sorted. I think that they should be.

tfausak commented 8 months ago

It turns out that sub-libraries are already sorted in build-depends and setup-depends.

For mixins, the situation is more complicated. The documentation has this note: https://hackage.haskell.org/package/Cabal-syntax-3.10.2.0/docs/Distribution-Types-ModuleRenaming.html#t:ModuleRenaming

This is a list not a map so that we can preserve order.

It may be a lack of imagination on my part, but I can't figure out why the order might be important. Maybe you can clobber names with renaming?

-- I wonder what this does ...
mixins: p ( M as N, N as O )
tfausak commented 8 months ago

It seems that you can't really clobber names. If you try to do something like this:

mixins: base ( Control.Exception, Control.Exception.Base as Control.Exception )

That is, intentionally try to create an alias to a module that already exists. The order doesn't matter here. It also doesn't matter if you use an existing module name or another alias. In other words, these behave similarly:

mixins: base ( Control.Exception.Base as Control.Exception, Control.Exception )
mixins: base ( Control.Exception as E, Control.Exception.Base as E )

The end result when you actually try to use one of these modules is an ambiguity error from GHC:

.../cabal-gild/dist-newstyle/build/aarch64-osx/ghc-9.8.1/cabal-gild-1.1.4.0/build/autogen/Paths_cabal_gild.hs:15:1: error: [GHC-45102]
    Ambiguous module name ‘Control.Exception’.
    it is bound as base-4.19.0.0:Control.Exception.Base by a package flag
    it is bound as base-4.19.0.0:Control.Exception by a package flag
   |
15 | import qualified Control.Exception as Exception
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

So I think it's safe to rearrange the list of module names.

tfausak commented 8 months ago

Oh, also: You can't refer to aliases that were defined earlier in the list. So this doesn't work:

-- does not work
mixins: base ( Control.Exception as Alias, Alias as Whatever )

You get an error like this:

Error:
    Package 'base-4.19.0.0' does not expose the module 'Alias'
    In mixins: base ( Control.Exception as Alias, Alias as Whatever )
    In the stanza 'library'
    In the inplace package 'cabal-gild-1.1.4.0'
tfausak commented 8 months ago

Oh duh, they're avoiding maps because the keys could show up multiple times. You can alias the same module as two different things.

mixins: base ( Data.Ord, Data.Ord as Ord )