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

Add more documentation to declaring conditionals. #141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,49 @@ becomes
extra-lib-dirs:
lib/darwin

Multiple conditionals must fall under one `when`, for example:

when:
- condition: os(darwin)
extra-lib-dirs: lib/darwin
- condition: os(linux)
extra-lib-dirs: /usr/lib/x64

becomes

if os(darwin)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note generally, but cabal library converts Darwin to osx

extra-lib-dirs:
lib/darwin
if os(linux)
extra-lib-dirs:
/usr/lib/x64

If conditionals are declared under different `when` clauses only the last one
will make it to the `.cabal` file:
Copy link
Owner

@sol sol Jan 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may not necessarily be the last one. This is again resulting from how YAML works. This is an object (think of it as a hash map), duplicate keys are not allowed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that this behaviour is on the YAML side of things (and is actually wrong according to the spec: http://yaml.org/spec/1.1/#id932806), but I find it very useful that it behaves as it does, and would actually propose to add tests for this behaviour and file bug reports to https://hackage.haskell.org/package/yaml if the behaviour ever changes. Here are two examples why:

Aliases with overriding:

executables:
    strava-gear-cmdline:
        &default_executable
        main: strava-gear-cmdline.hs
        dependencies: [strava-gear]
        ghc-options:
            - -threaded

    strava-gear-web:
        <<: *default_executable
        main: strava-gear-web.hs

Predefined aliases:

executables:
    &default_executable
    dependencies: [strava-gear]
    ghc-options:
        - -threaded

executables:
    strava-gear-cmdline:
        <<: *default_executable
        main: strava-gear-cmdline.hs

    strava-gear-web:
        <<: *default_executable
        main: strava-gear-web.hs

The second example is a massive hack, but I'm not aware of any other way to do this given that hpack insists on printing warnings for unused fields even if I prefix them with underscores.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liskin Hey, interesting! How about we don't emit warnings for unknown top-level field that starts with an underscore? That way you can use fields that start with underscores for building kind of a hpack library.

I would certainly accept a patch. So if you think thats the way to go, then feel free to open a PR!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you go: #146 :-)


when:
- condition: os(darwin)
extra-lib-dirs: lib/darwin
when:
- condition: os(linux)
extra-lib-dirs: /usr/lib/x64

becomes:

if os(linux)
extra-lib-dirs:
/usr/lib/x64

Unlike with `.cabal` files compound predicates need to be in parens, for example:

when:
- condition: (!os(darwin) && !os(windows))
Copy link
Owner

@sol sol Jan 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the issue here is not that you have a "compound predicate" here. It's merely the !, which in YAML must be quoted when at the beginning of a string, say:

condition: "!os(darwin) && !os(windows)"

ghc-options: ...

If they are not the YAML parser will throw this mysterious error:

./package.yaml:41:31: did not find expected alphabetic or numeric character while scanning an anchor

Conditionals with an else branch:

- Must have a `condition` field
Expand Down