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 original YAML to preserve top-level order. #5813

Merged

Conversation

philderbeast
Copy link
Contributor

Sadly, we do not have a format-and-comment-preserving YAML parser/writer available that I'm aware of. There are other cases where Stack has to modify the the stack.yaml with the same problem. It's really annoying.
#3136 (comment)

Please include the following checklist in your pull request:

  • Any changes that could be relevant to users have been recorded in ChangeLog.md.
  • The documentation has been updated, if necessary

I tested with the stack.yaml file in this repo. Want to get this reviewed before adding tests. The commands I used for testing were:

> cabal run stack -- config set system-ghc true
> cabal run stack -- config set system-ghc false
> cabal run stack -- config set install-ghc true
> cabal run stack -- config set install-ghc false
> cabal run stack -- config set resolver lts-19.17
> cabal run stack -- config set resolver nightly

I added blanks lines at the start and end of the YAML document and these are preserved. I also moved the appended item to somewhere in the middle of the document.

Use comparer to preserve key order of lines.
Move guts of ordering to encodeInOrder.
Insert blank lines (badly).
Put blanks lines in the right place.
Preserve whole line comments.
Add part line comments.
Don't add line numbers on blank lines.
Move added code to YamlUpdate module.
Follow hlint suggestion: use find.
Pull out reindex function.
Preserve update key position if not appending.
Rename pegLines and add YamlLines.
Change to logDebug with a prefix.
Rename RawConfig to RawYaml.
Qualify as T, not RioT.
Get rid of "config" specific naming.
Rename keepBlanks to redress.
Make redress and encodeInOrder pure, stripping logging.
Rename ixMap.
Get rid of unneeded args now that compare appends.
Preserve leading space.
Use sentinels to ensure trailing newlines not swallowed.
Add data Pegged.
Don't wrap with YamlKey but use coerce.
Use either instead of case.
Don't use coerce this once, be more explicit.
Drop an unneeded type annotation.
Hide sentinels with mkRaw and unmkRaw.
Add yamlLines and more commentary.
Log lines that are blank.
@mpilgrem
Copy link
Member

mpilgrem commented Aug 4, 2022

Thanks! Forgive me if it takes me a little while to work through this. My immediate priority is elsewhere (I'm currently trying to get Stack to build with GHC 9.2.4).

@philderbeast
Copy link
Contributor Author

philderbeast commented Aug 4, 2022

For testing I copied stack.yaml to stack__.yaml and used a difftool to compare. This is the starting state of both files with blank lines at the start and end of the file and with system-ghc: true inserted already. The rest of the content is straight from the YAML content of the current stack.yaml in this repo:

system-ghc: true

resolver: lts-17.15

packages:
- .

extra-deps:
- hpack-0.35.0@rev:0
- pantry-0.5.6@rev:0 # https://github.com/commercialhaskell/pantry/pull/53
- rio-0.1.21.0@rev:0


docker:
  enable: false
  # repo: fpco/alpine-haskell-stack:8.10.4
  repo: fpco/alpine-haskell-stack@sha256:1024fe4b3b082a8df64d00e8563b3151220ed90af09604a8f7e1d44040500c30

nix:
  # --nix on the command-line to enable.
  packages:
  - zlib
  - unzip

flags:
  stack:
    developer-mode: true

ghc-options:
  "$locals": -fhide-source-paths

drop-packages:
# See https://github.com/commercialhaskell/stack/pull/4712
- cabal-install




Followed up by these 3 commands:

> cabal run stack -- config set resolver lts-19.17
> cabal run stack -- config set install-ghc false
> cabal run stack -- config set system-ghc false

After the fix:

Screen Shot 2022-08-04 at 7 23 15 PM

Before the fix:

Screen Shot 2022-08-04 at 7 28 41 PM

@mpilgrem
Copy link
Member

Thanks! If you are interested in the stack config set command, here is another bug that may interest you: resolver and snapshot are synonyms in project-level configuration files, but stack config set resolver does not respect the existence of a snapshot key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants