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

else/elif seem unsupported? #109

Closed
hseg opened this issue Sep 12, 2024 · 9 comments
Closed

else/elif seem unsupported? #109

hseg opened this issue Sep 12, 2024 · 9 comments
Labels
question Further information is requested

Comments

@hseg
Copy link

hseg commented Sep 12, 2024

Consider the following MWE:

cabal-version: 2.4
if os(mingw32)
  constraints: tar +static
else if os(mingw32)
  constraints: tar -static
elif os(mingw32)
  constraints: tar +static +win32

It is currently formatted as

cabal-version: 2.4

if os(windows)
  constraints: tar +static
else if os ( mingw32 )
  constraints: tar -static

elif os ( mingw32 )
  constraints: tar +static +win32

where I would expect it to be formatted as

cabal-version: 2.4

if os(windows)
  constraints: tar +static
elif os(windows)
  constraints: tar -static
elif os(windows)
  constraints: tar +static +win32

(note the spacing!)

@tfausak tfausak added the question Further information is requested label Sep 12, 2024
@tfausak
Copy link
Owner

tfausak commented Sep 12, 2024

Thanks for reporting this issue! I think there's some confusion about how conditionals work in package descriptions. Prior to Cabal 2.2, elif wasn't supported at all. There was only if and else. So your input would be formatted like this:

if flag(a)
  constraints: tar +static
else
  if flag(b)
    constraints: tar -static
  else
    if flag(c)
      constraints: tar +static +win32

Note that nothing else can go on the line with else.

Starting with Cabal 2.2, the new elif section was allowed. So you could write your input like this:

cabal-version: 2.2

if flag(a)
  constraints: tar +static
elif flag(b)
  constraints: tar -static
elif flag(c)
  constraints: tar +static +win32

@hseg
Copy link
Author

hseg commented Sep 12, 2024 via email

@tfausak
Copy link
Owner

tfausak commented Sep 12, 2024

Gild is very intentionally only a formatter. It does not aim to provide any validation or linting. else foo is syntactically valid, so Gild formats it. Similarly, elif is syntactically valid even for older Cabal versions, so Gild formats it.

Cabal itself will emit an error if you provide any arguments to else. For example:

cabal-version: 2.0
name: example
version: 0
build-type: Simple

library
  if False
    x-whatever: 0
  else if False
    x-whatever: 1
Errors encountered when parsing cabal file ./example.cabal:

example.cabal:9:3: error:
`else` section has section arguments [SecArgName (Position 9 8) "if",SecArgName (Position 9 11) "False"]

    8 |     x-whatever: 0
    9 |   else if False
      |   ^

And it will warn about using elif with an unsupported version of Cabal:

cabal-version: 2.0
name: example
version: 0
build-type: Simple

library
  if False
    x-whatever: 0
  elif False
    x-whatever: 1
...
Warning: example.cabal:9:3: invalid subsection "elif". You should set
cabal-version: 2.2 or larger to use elif-conditionals.
...

@hseg
Copy link
Author

hseg commented Sep 12, 2024 via email

@tfausak
Copy link
Owner

tfausak commented Sep 12, 2024

Are you asking for Gild to always format elif, even if the Cabal version is less than 2.2? It certainly could do that, but it deliberately doesn't: #83 (comment)

Once the user notices the warning from Cabal, presumably they'll edit their package description to use a recent-enough Cabal version. Then all they have to do is re-run Gild and it should be formatted correctly.

It's also worth noting that elif os(mingw32) and elif os ( mingw32 ) are semantically the same. The extra spaces don't change the meaning at all.

@hseg
Copy link
Author

hseg commented Sep 12, 2024

I'm saying that elif is never formatted, even if Cabal is at least 2.2, which it seems you believe is not what's happening? How is Gild detecting the cabal version, if so?
I was suggesting Gild always format elif for simplicity's sake, and to leave the correctness concern to Cabal.
And yes, elif os ( mingw32 ) is semantically identical, but I want my tools to improve the formatting, not worsen it.

@tfausak
Copy link
Owner

tfausak commented Sep 12, 2024

But elif does get formatted?

-- input
cabal-version: 2.2
elif flag ( BLAH )
  x-blah: true
-- output
cabal-version: 2.2
elif flag(blah)
  x-blah: true

There's also a test for this case:

Hspec.it "formats new elif" $ do
expectGilded
"cabal-version: 2.2\nelif flag ( x )"
"cabal-version: 2.2\nelif flag(x)\n"

@hseg
Copy link
Author

hseg commented Sep 14, 2024 via email

@tfausak
Copy link
Owner

tfausak commented Sep 14, 2024

No problem! Sorry for the confusion.

@tfausak tfausak closed this as completed Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants