|
| 1 | +--- |
| 2 | +title: Run whole executable |
| 3 | +summary: Cabal runs executables but not modules |
| 4 | +severity: error |
| 5 | +introduced: 3.12.1.0 |
| 6 | +--- |
| 7 | + |
| 8 | +Self-contained, single-file Cabal scripts cannot also be part of a package, as |
| 9 | +an executable or listed as a module of any package component. |
| 10 | + |
| 11 | +Let's say we have a Cabal script, `myscript.hs`: |
| 12 | + |
| 13 | +```haskell |
| 14 | +#!/usr/bin/env cabal |
| 15 | +{- cabal: |
| 16 | +build-depends: |
| 17 | + base, |
| 18 | + haskell-say ^>=1.0.0.0 |
| 19 | +-} |
| 20 | + |
| 21 | +import HaskellSay (haskellSay) |
| 22 | + |
| 23 | +main :: IO () |
| 24 | +main = haskellSay "Hello, Haskell!" |
| 25 | +``` |
| 26 | + |
| 27 | +We can run this with `cabal run` or with `./myscript.hs`. |
| 28 | + |
| 29 | +``` |
| 30 | +$ cabal run myscript.hs |
| 31 | + ________________________________________________________ |
| 32 | + / \ |
| 33 | + | Hello, Haskell! | |
| 34 | + \____ _____________________________________________/ |
| 35 | + \ / |
| 36 | + \ / |
| 37 | + \/ |
| 38 | + _____ _____ |
| 39 | + \ \ \ \ |
| 40 | + \ \ \ \ |
| 41 | + \ \ \ \ |
| 42 | + \ \ \ \ \-----------| |
| 43 | + \ \ \ \ \ | |
| 44 | + \ \ \ \ \---------| |
| 45 | + / / / \ |
| 46 | + / / / \ \-------| |
| 47 | + / / / ^ \ \ | |
| 48 | + / / / / \ \ \ ----| |
| 49 | + / / / / \ \ |
| 50 | + /____/ /____/ \____\ |
| 51 | +``` |
| 52 | + |
| 53 | +Once we add this script to a package then we lose the ability to run the script |
| 54 | +by specifying its module name. An executable name from the package must be used |
| 55 | +instead. Moreover, any module included in a package is not runnable by itself |
| 56 | +with `cabal run`. |
| 57 | + |
| 58 | +``` |
| 59 | +$ cat script-exclusivity.cabal |
| 60 | +cabal-version: 3.0 |
| 61 | +name: script-exclusitivity |
| 62 | +version: 1 |
| 63 | +
|
| 64 | +executable my-script-exe |
| 65 | + build-depends: |
| 66 | + base, |
| 67 | + haskell-say |
| 68 | + main-is: myscript.hs |
| 69 | +
|
| 70 | +$ ./myscript.hs |
| 71 | +Error: [Cabal-7070] |
| 72 | +The run command can only run an executable as a whole, not files or modules |
| 73 | +within them, but the target 'myscript.hs' refers to the file myscript.hs in the |
| 74 | +executable my-script-exe. |
| 75 | +
|
| 76 | +$ cabal run myscript.hs |
| 77 | +Error: [Cabal-7070] |
| 78 | +The run command can only run an executable as a whole, not files or modules |
| 79 | +within them, but the target 'myscript.hs' refers to the file myscript.hs in the |
| 80 | +executable my-script-exe. |
| 81 | +``` |
0 commit comments