36

I recently ran into a Cabal issue that I only managed to solve by manually installing transformers-compat with the -f transformers3 flag in my cabal sandbox before running cabal install for my project.

Is there any way to indicate in my application's .cabal file that I depend on a library so that it is built with the specific build flag?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
shang
  • 24,642
  • 3
  • 58
  • 86
  • At a glance, it looks like transformers-compat is supposed to be a shim used for old projects with old dependencies. Can you update your package to depend on the newer version of transformers? – Daniel Wagner May 07 '14 at 17:55
  • 5
    Aside: calling any kind of technical problem with Cabal "Cabal hell" is not very informative. It'd be easier to help you if you described which error message you were getting. – Mikhail Glushenkov May 08 '14 at 02:17

6 Answers6

18

Newer versions of Cabal let you specify constraints in your cabal.project.local or cabal.project file. For example:

constraints: hmatrix +openblas

Is there any way to indicate in my application's .cabal file that I depend on a library so that it is built with the specific build flag?

No, but in your case this is not actually a problem in the solver and is rather and uninformative error (caused by someone's less than judicious uses of flags).

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • The original link doesn't seem to work but got me close enough to find: https://cabal.readthedocs.io/en/3.6/cabal-project.html#cfg-field-constraints – imoverclocked May 10 '22 at 00:00
17

Looks like it's not possible to specify such a dependency via the build-depends field in your .cabal file. buildDepends is defined as [Dependency], where data Dependency = Dependency PackageName VersionRange. You can use cabal install --constraint="transformers-compat +transformers3", though.

Looking at the transformers-compat.cabal file, I think that the solver should be able to figure out the correct flag assignment if you constrain your dependency on transformers appropriately. E.g. build-depends: transformers >= 0.3 && < 0.4 should force the solver to choose transformers-compat +transformers3. If this doesn't work, it may be a bug in the solver.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Mikhail Glushenkov
  • 14,928
  • 3
  • 52
  • 65
11

I also struggled for a long time to find a solution to this problem. I just found one! You have to modify the global cabal configuration file at ~/.cabal/config. Add a constraints line like this to the initial section of the file:

constraints: hmatrix +openblas

This enables the openblas flag for the hmatrix package. It will be used automatically the next time the package is installed. If there is a way to set such a flag locally for a sandbox, I could not find it.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Carlos Reyes
  • 111
  • 1
  • 3
7

You cannot do this with Cabal.

One way to do this is to use Stack. Edit your stack.yaml to include

flags:
  transformers-compat:
    transformers3: true
    

See also the section on flags.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
  • Hi, is the first sentence here this still valid? Apparently cabal got the same functionality quite recently. – exa Jun 28 '22 at 18:30
5

cabal now supports an elegant way to do this similar to stack, through cabal.project configuration options.

package transformers-compat
    flags: +transformers3

will add the flag transformers3 when building the package transformers-compat.

Chase
  • 5,315
  • 2
  • 15
  • 41
-1

There are a couple of ways to constrain the version for installation.

  1. Add lower and upper bounds to package versions in the cabal file like Mikhail mentioned above, example of such a file here

  2. Additionally, you can override the settings in the .cabal file with the flag cabal install --constraint="bar-2.1"

To remove a specific version of a package:

  • In a sandbox you can unregister a version with cabal sandbox hc-pkg unregister bar-2.1
  • Global unregistering can be done with this command outside of sandbox ghc-pkg unregister bar-2.1
Community
  • 1
  • 1
katychuang
  • 1,018
  • 8
  • 18