hpack icon indicating copy to clipboard operation
hpack copied to clipboard

Verbatim imports put at bottom of stanza first time

Open brandon-leapyear opened this issue 3 years ago • 2 comments

https://github.com/sol/hpack/pull/490 moves import lines to the top of the stanza, but it only happens the first time hpack is called.

Repro:

  1. Run rm *.cabal
  2. Add verbatim: { import: ... } line to a stanza
  3. Run hpack
  4. Cabal file incorrectly has import line at bottom
  5. Run hpack again
  6. Cabal file correctly has import line moved to top

brandon-leapyear avatar Sep 20 '22 21:09 brandon-leapyear

Hi @brandon-leapyear 👋 thanks for reporting this!

This is something we want to fix. I am away from my computer for the next couple of days. Still, I took a quick look at the code. From what I understand, these are the underlying issues:

  1. I did not add a test with #490. Our acceptance tests accept any field order, so adding an acceptance test may require some extra work. We have existing tests for field sorting in: https://github.com/sol/hpack/blob/7c46a3e4030b6d9c2e1eeb45d015e5f7d84825c9/test/Hpack/Render/DslSpec.hs#L134 However the code added in #490 is not covered by those tests.
  2. We only sort stanza fields if we already have an existing fieldOrder (and by extension only when we already have a .cabal-file): https://github.com/sol/hpack/blob/d9cbc10813e752140f0f22ee24c1420786c7e893/src/Hpack/Render.hs#L126

sol avatar Sep 21 '22 00:09 sol

:sparkles: This is an old work account. Please reference @brandonchinn178 for all future communication :sparkles:


Makes sense! I don't know if you have any end-to-end tests, but I think a simple end-to-end test for this could be:

  1. In an empty directory, create package.yaml that imports a common stanza adding a dependency (e.g. aeson or containers) + write a library file using that dependency (such that a cabal file lacking that dependency would fail to build)
  2. In that directory, run hpack and cabal
  3. Build should succeed

brandon-leapyear avatar Sep 21 '22 00:09 brandon-leapyear