haskell / haskell/cabal

Incorrectly allows trailing comments after section headers

Open
#9,677 3 comments 0 reactions 0 assignees View on GitHub
Cabal: file format type: enhancement
Dominant language
Haskell
Stars
1.7k
Forks
750
Avg merge
4d 3h
Merged PRs (30d)
28

Description

**Describe the bug**
Typically Cabal does not allow trailing comments. All comments must be on their own line. They cannot follow a field name or value. However if you put an trailing comment after a section header, it is accepted by Cabal.

**To Reproduce**
Create a package description like this:

``` cabal
cabal-version: >= 1.2
name: example
version: 0
build-type: Simple
executable example -- foo
main-is: Main.hs
build-depends: base
```

The only important line is `executable example -- foo`. Using `executable` specifically isn't necessary. Any section (like `library`) behaves the same. I used `executable` to show that the comment isn't being interpreted as an argument to the section. In other words, we're not defining an executable called `"example -- foo"`.

Similarly the `cabal-version` does not appear to be important. Setting it to, say, `cabal-version: 3.8` exhibits the same behavior.

To let Cabal build this example package successfully, also create a `Main.hs` file. It's not important what's in it. Something like this will work:

``` hs
main = pure ()
```

Finally, run `cabal build`.

**Expected behavior**
The build should fail due to invalid syntax in the package description. For example, consider the following package description:

``` cabal
name: example -- foo
```

That fails with the following error:

```
Errors encountered when parsing cabal file ./example.cabal:

example.cabal:1:15: error:
unexpected '-'
expecting space or end of input

1 | name: example -- foo
| ^
```

**System information**
```
$ cabal --version
cabal-install version 3.10.2.1
compiled using version 3.10.2.1 of the Cabal library
```

**Additional context**
It seems like either trailing comments should be allowed everywhere or prohibited everywhere. I think the intent is for them to be prohibited. Various pieces of documentation and code say that:

https://github.com/haskell/cabal/blob/46a7bfc922d62477cc9de061a6dc0d3c91cdd9ee/doc/how-to-package-haskell-code.rst#L140-L142

https://github.com/haskell/cabal/blob/46a7bfc922d62477cc9de061a6dc0d3c91cdd9ee/Cabal-syntax/src/Distribution/Parsec.hs#L276-L278

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.