Repository navigation
Description
What is this issue?
A central place to discuss the issue of a Cabal exact-printer so that we can push forward the work all in one place. Currently, The discussion stretches back across many issues over the past 6 years.
What is it?
An exact-printer, as inspired by ghc-exactprint, is a byte-for-byte bidirectional parser and pretty printer, which gaurantees the following constructs exist and are principled:
- A
.cabalfile source tree representation, which is in bijection with the package description for a.cabalfile for a given spec version. This representation should be parametrized with all offset and encoding information along with file data. - A method for introducing deltas to
.cabalfile entries. I should able to update and transform individual components of data deep in the representation without modifying the broader structure. - A pretty printer which adds offset annotations to the specification fragment so that the output should have a byte-for-byte round trip on no-op parse/print and print/parse tests.
- Tests which confirm this in
cabal-testsuiteandcabal install's test suite for the individual commands.
Why do we need it?
This work is tied very closely with the format, init, gen-bounds and other work as shown in historical discussions:
- RFC: new intermediate cabal file representation. #3614
- WIP/RFC: Parse .cabal files to a source code representation #6621
- Use Distribution.Fields.Pretty in
cabal init. #6187 - Make a spec-looking reference implementation of outer grammar. #4965
- etc...
This would free us up for several important quality of life improvements to the cabal install ecosystem, as well as general consumers of the Cabal library API. We would like to have this in by Cabal-3.8.0.0. The important bits this enables include:
cabal gen-bounds- will be able to updatebuild-dependsstanzas in a principled manner, so that bounds management will be automatic in the future if the user so desires.cabal format- we hope that a.cabalformat can have a canonical shape, so we can limit the amount of deviations from the norm, or ad-hoc formatting choices present in the ecosystem.cabal initbecomes much easier to manage, and we no longer have to dedicate redundant types, formatting, and pretty printing to this effort.- Transforming cabal files, with something like module discovery, becomes that much simpler.
Who's in charge of this?
I am currently overseeing @ptkato who has been tasked with taking this on. Please consider him and myself a point of contact for this work, and join us in libera.chat#hackage to discuss.
Activity
[-]RFC: Exact-printer Mega-issue[/-][+]Meta: Exact-printer Mega-issue[/+]gbaz commentedon Aug 14, 2021
We have an intermediate "lexed tree" structure in https://github.com/haskell/cabal/blob/1be581d5463adebf8dc8cc8a5375a9ca3051970a/Cabal/src/Distribution/Fields/Field.hs
One idea I would throw out is turning that structure into the main structure we get exactprinting working with (making it comment and whitespace preserving). Then transforms on cabal files involve parsing to that, then running the FieldGrammar parser to get the nice fully typed structure, modifying that and emitting it back to Fields, and then calculating the delta to apply the changes thus made to the original exact Fields.
emilypi commentedon Aug 14, 2021
There's good prior art here: - https://github.com/phadej/cabal-fmt
tonymorris commentedon Aug 14, 2021
I've written a few of these for other formats. If you need help @ptkato then @emilypi can hook us up.
Mikolaj commentedon Aug 14, 2021
See also the old github project about that: https://github.com/haskell/cabal/projects/9
I've updated it with related issues I've found, but then also marked all the issues with a label, so the indexing is available in both forms now. If the project is not going to be used, please remove it. The labeling will remain, preserving the data.
ptkato commentedon Sep 16, 2021
I combed through cabal-fmt and here, and I must say, I'm not a fan of how the comments would need to be dealt with if we use that approach. Yes, for solving it works great, it cuts down a considerable amount of time, but for an exact-printer it seems... odd (and potentially making it more complicated than it should be). I really do believe that that the right way would be to parse it all at once, as opposed to appending the comments afterwards.
55 remaining items
gbaz commentedon Oct 21, 2023
It changes slightly (in terms of additions of new fields) with almost every Cabal major release:
https://cabal.readthedocs.io/en/stable/file-format-changelog.html
tonyday567 commentedon Oct 21, 2023
I would love to collaborate on a project with this type of specification. Something that could start small - I like the idea of just learning a bit about the answers to these questions. An initial aim could be just to get a "rough guide" to cabal syntax, and the library, that could maybe help the next generation of intrepid explorers.
I don't know too much about the code base, but I have parsed cabal files using the lbrary and some (flatparse) logic.
jappeace commentedon Oct 22, 2023
@gbaz
I get the impression this is somewhat demotivating to potential new contributors, so I wish to suggest a path to replacing cabal parser:
ExactAst -> [Field Position]etc.Once the maintainers feel confident the exact print parser is good enough in terms of correctness and performance,
we can swap out the legacy legacy parser for the exact print one with help of the mapping.
BurningWitness commentedon Oct 24, 2023
Having thought about it for a bit more, curly bracket syntax forces parsing to be depth-first, as any deeper level may introduce a breaking offset change:
foo bar { baz this { that } end }As such streaming is limited to one top-level element at a time, and to prove the element ends at a correct point its entire skeleton must be parsed.
For any future format-inventors:
I think the sane approach would be to instead use a special literal at the start of the line, only interpreting it as an offset break if its offset is smaller than of the element its inside of:
Same rule would make sense for comments inside fields:
andreabedini commentedon Oct 25, 2023
@jappeace
I understand how @gbaz's comment might have come across as demotivating. But "evolve it" and "replace it" are not necessarily in opposition.
Just like Theseus's ship, we can evolve it by replacing one part at the time (or should I say replace it by evolving one part at the time? 🤔).
I appreciate you have been spending time one this and I will comment on some technical aspects of it; but what I want to understand first is: are you willing to do the work?
This is done, here is a freshly baked new parser written by @VeryMilkyJoe. I also notice that @liamzee has already contributed to it.
I might disagree with some of the technical choices but let's just roll with the
CabalASTas it is implemented there.To have a chance of being able to use it Cabal-syntax, it has to depend only on boot packages so you have to replace megaparsec with parsec.
You are welcome to do this.
Which version you are talking about here? The
Fieldsyntax does not depend on any version. I am not sure whetherCabalASTmakes more assumptions thanField.At the cost of impoving anything. If Cabal operated on
Field Position, we would have lost all the annotations thatCabalASTprovides.Also, you will realise that
CabalASTdoes not mentionbuild-dependsor anything like that. Why is that? It's becauseField(which I believeCabalASTwas modelled from) is more basic syntax structure.The actual fields definition is in the various
FieldGrammars, a static definition of the fields that is used for both parsing and pretty-printing. It is general enough it is also used to generate the field syntax reference.GenericPackageDescriptionis parsed fromFieldusing this infrastructure. I assume you could parse it fromCabalASTinstead; but how are you going to keep the annotations?Define "output":
Field Position,Fields,GenericPackageDescription?(while considering myself only a contributor) Yes please!
You can also use the test-suite
hackage-testsinCabal-tests.Again which legacy AST you are referring to here?
In terms of benchmarks I believe we have only
cabal-benchmarks/bench/CabalBenchmarks.hs.As far as I understand:
hackage-testsmight be enoughOnce the problems above have been resolved, I believe nobody will stand in the way.
I apologise if I have come across a bit rude. By looking at the codebase, I think these are the problems we have to face. I am eager to see them discussed or better still solved.
Of course, I would be also happy to be called out if someone thinks that I am too entrenched in the current architecture and everything can be easily simplied. Show me!
jappeace commentedon Oct 29, 2023
No the intention was to hire @LemonjamesD to do this, but she has to know what to do.
I feel your comment makes it a lot more clear what the actual issue is. Thanks for that :)
Yes so I get the impression the best approach is to modify types such as
FieldsandGenericPackageDescriptionin place to add exact printing support (1).And deal with the obnoxious amount of compile errors.
Any of those would work depending on what you're mapping into,
if you'd do the seperate AST approach.
I perhaps mistakenly assumed the current way of parsing needed replacing.
It's fine you provided a goldmine of information, at least I can now make a sorta scoped contract rather then "oh go do this thing".
New approach based on (1):
prettypackage could be used)This approach makes the work easier to split as well because you can just make test by test pass. Where a test is a cabal file being roundtripped.
BurningWitness commentedon Nov 8, 2023
Seems like the last idiosyncrasy on my list is section name parsing, allowing
No manifests on Hackage utilize this as a format escape, only ds-kanren/metric patches do.
foo:barI assume clashes directly with the existing console command format ofpkg:test:name, whilefoo baralways has to be quoted. As such I don't see a good reason to maintain any of this.