|
1 | | -<div class="hidden-warning"><a href="https://docs.haskellstack.org/"><img src="https://cdn.jsdelivr.net/gh/commercialhaskell/stack/doc/img/hidden-warning.svg"></a></div> |
| 1 | + <div class="hidden-warning"><a href="https://docs.haskellstack.org/"><img src="https://cdn.jsdelivr.net/gh/commercialhaskell/stack/doc/img/hidden-warning.svg"></a></div> |
2 | 2 |
|
3 | 3 | # User guide (introductory) |
4 | 4 |
|
@@ -1396,336 +1396,6 @@ instead of creating an entire Cabal package for it. You can use `stack exec ghc` |
1396 | 1396 | or `stack exec runghc` for that. As simple helpers, we also provide the |
1397 | 1397 | `stack ghc` and `stack runghc` commands, for these common cases. |
1398 | 1398 |
|
1399 | | -## The `stack script` command |
1400 | | - |
1401 | | -The `stack script` command also either runs a specified Haskell source file |
1402 | | -(using GHC's `runghc`) or, optionally, compiles a specified Haskell source file |
1403 | | -(using GHC) and, by default, runs it. |
1404 | | - |
1405 | | -However, unlike `stack ghc` and `stack runghc`, the command ignores all Stack |
1406 | | -YAML configuration files. A snapshot must be specified on the command line (with |
1407 | | -the `--resolver` option). For example: |
1408 | | - |
1409 | | -~~~text |
1410 | | -stack --resolver lts-19.28 MyScript.hs |
1411 | | -~~~ |
1412 | | - |
1413 | | -or, equivalently: |
1414 | | - |
1415 | | -~~~text |
1416 | | -stack script --resolver lts-19.28 MyScript.hs |
1417 | | -~~~ |
1418 | | - |
1419 | | -Everything after `--` on the command line is interpreted as a command line |
1420 | | -argument to be passed to what is run. |
1421 | | - |
1422 | | -A package can be added to the snapshot on the command line with the |
1423 | | -`--extra-dep` option (which can be specified multiple times). |
1424 | | - |
1425 | | -Each required package can be specified by name on the command line with the |
1426 | | -`--package` option (which can be specified multiple times). A single `--package` |
1427 | | -option can also refer to a list of package names, separated by a space or comma |
1428 | | -character. If the package is not in the snapshot, the most recent version on |
1429 | | -Hackage will be obtained. If no packages are specified in that way, all the |
1430 | | -required packages that are in the snapshot will be deduced by reference to the |
1431 | | -`import` statements in the source file. The `base` package associated with the |
1432 | | -version of GHC specified by the snapshot is always available. |
1433 | | - |
1434 | | -The source file can be compiled by passing either the `--compile` flag (no |
1435 | | -optimization) or the `--optimize` flag (compilation with optimization). If the |
1436 | | -file is compiled, passing the `--no-run` flag will mean the compiled code is not |
1437 | | -run. |
1438 | | - |
1439 | | -Additional options can be passed to GHC using the `--ghc-options` option. |
1440 | | - |
1441 | | -For example, `MyScript.hs`: |
1442 | | - |
1443 | | -~~~haskell |
1444 | | -module Main (main) where |
1445 | | - |
1446 | | -import Data.List (intercalate) |
1447 | | -import System.Environment (getArgs) |
1448 | | - |
1449 | | -import Acme.Missiles (launchMissiles) |
1450 | | - |
1451 | | -main :: IO () |
1452 | | -main = do |
1453 | | - advices <- getArgs |
1454 | | - launchMissiles |
1455 | | - putStrLn $ intercalate "\n" advices |
1456 | | -~~~ |
1457 | | - |
1458 | | -can be compiled and run, with arguments, with: |
1459 | | - |
1460 | | -~~~text |
1461 | | -stack --resolver lts-19.28 script --package acme-missiles --compile MyScript.hs -- "Don't panic!" "Duck and cover!" |
1462 | | -~~~ |
1463 | | - |
1464 | | -## Stack's script interpreter |
1465 | | - |
1466 | | -Stack also offers a very useful feature for running files: a script interpreter. |
1467 | | -For too long have Haskellers felt shackled to bash or Python because it's just |
1468 | | -too hard to create reusable source-only Haskell scripts. Stack attempts to solve |
1469 | | -that. |
1470 | | - |
1471 | | -You can use `stack <file_name>` to execute a Haskell source file. Usually, the |
1472 | | -Stack command to be applied is specified using a special Haskell comment (the |
1473 | | -Stack interpreter options comment) at the start of the source file. That command |
1474 | | -is most often `stack script` but it can be, for example, `stack runghc`. If |
1475 | | -there is no Stack interpreter options comment, Stack will warn that one was |
1476 | | -expected. |
1477 | | - |
1478 | | -An example will be easiest to understand. Consider the Haskell source file |
1479 | | -`turtle-example.hs` with contents: |
1480 | | - |
1481 | | -~~~haskell |
1482 | | -#!/usr/bin/env stack |
1483 | | --- stack script --resolver lts-19.28 --package turtle |
1484 | | -{-# LANGUAGE OverloadedStrings #-} |
1485 | | -import Turtle (echo) |
1486 | | -main = echo "Hello World!" |
1487 | | -~~~ |
1488 | | - |
1489 | | -=== "Unix-like" |
1490 | | - |
1491 | | - The first line beginning with the 'shebang' (`#!`) tells Unix to use Stack |
1492 | | - as a script interpreter, if the file's permissions mark it as executable. A |
1493 | | - shebang line is limited to a single argument, here `stack`. |
1494 | | - |
1495 | | - The file's permissions can be set with command `chmod` and then it can be |
1496 | | - run: |
1497 | | - |
1498 | | - ~~~text |
1499 | | - chmod +x turtle-example.hs |
1500 | | - ./turtle-example.hs |
1501 | | - ~~~ |
1502 | | - |
1503 | | - !!! note |
1504 | | - |
1505 | | - On macOS: |
1506 | | - |
1507 | | - - Avoid `{-# LANGUAGE CPP #-}` in Stack scripts; it breaks the shebang |
1508 | | - line ([GHC #6132](https://gitlab.haskell.org/ghc/ghc/issues/6132)) |
1509 | | - |
1510 | | - - Use a compiled executable, not another script, in the shebang line. |
1511 | | - Eg `#!/usr/bin/env runhaskell` will work but |
1512 | | - `#!/usr/local/bin/runhaskell` would not. |
1513 | | - |
1514 | | - Alternatively, the script can be run with command: |
1515 | | - |
1516 | | - ~~~text |
1517 | | - stack turtle-example.hs |
1518 | | - ~~~ |
1519 | | - |
1520 | | -=== "Windows (with PowerShell)" |
1521 | | - |
1522 | | - The first line beginning with the 'shebang' (`#!`) has a meaning on |
1523 | | - Unix-like operating systems but will be ignored by PowerShell. It can be |
1524 | | - omitted on Windows. The script can be run with command: |
1525 | | - |
1526 | | - ~~~text |
1527 | | - stack turtle-example.hs |
1528 | | - ~~~ |
1529 | | - |
1530 | | -In both cases, the command yields: |
1531 | | - |
1532 | | -~~~text |
1533 | | -Hello World! |
1534 | | -~~~ |
1535 | | - |
1536 | | -the first time after a little delay (as GHC is downloaded, if necessary, and |
1537 | | -dependencies are built) and subsequent times more promptly (as the runs are |
1538 | | -able to reuse everything already built). |
1539 | | - |
1540 | | -The second line of the source code is the Stack interpreter options comment. In |
1541 | | -this example, it specifies the `stack script` command with the options of a |
1542 | | -LTS Haskell 19.28 snapshot (`--resolver lts-19.28`) and ensuring the |
1543 | | -[`turtle` package](https://hackage.haskell.org/package/turtle) is available |
1544 | | -(`--package turtle`). The version of the package will be that in the specified |
1545 | | -snapshot (`lts-19.28` provides `turtle-1.5.25`). |
1546 | | - |
1547 | | -### Arguments and interpreter options and arguments |
1548 | | - |
1549 | | -Arguments for the script can be specified on the command line after the file |
1550 | | -name: `stack <file_name> <arg1> <arg2> ...`. |
1551 | | - |
1552 | | -The Stack interpreter options comment must specify what would be a single valid |
1553 | | -Stack command at the command line if the file name were included as an argument, |
1554 | | -starting with `stack`. It can include `--` followed by arguments. In particular, |
1555 | | -the Stack command `stack <arg1> MyScript.hs <arg4>` with |
1556 | | -Stack interpreter options comment: |
1557 | | - |
1558 | | -~~~haskell |
1559 | | --- stack <arg2> <command> <arg3> -- <arg5> |
1560 | | -~~~ |
1561 | | - |
1562 | | -is equivalent to the following command at the command line: |
1563 | | - |
1564 | | -~~~text |
1565 | | -stack <arg1> <arg2> <command> <arg3> -- MyScript.hs <arg4> <arg5> |
1566 | | -~~~ |
1567 | | - |
1568 | | -The Stack interpreter options comment must be the first line of the file, unless |
1569 | | -a shebang line is the first line, when the comment must be the second line. The |
1570 | | -comment must start in the first column of the line. |
1571 | | - |
1572 | | -When many options are needed, a block style comment that splits the command over |
1573 | | -more than one line may be more convenient and easier to read. |
1574 | | - |
1575 | | -For example, the command `stack MyScript.hs arg1 arg2` with `MyScript.hs`: |
1576 | | - |
1577 | | -~~~haskell |
1578 | | -#!/usr/bin/env stack |
1579 | | -{- stack script |
1580 | | - --resolver lts-19.28 |
1581 | | - -- |
1582 | | - +RTS -s -RTS |
1583 | | --} |
1584 | | -import Data.List (intercalate) |
1585 | | -import System.Environment (getArgs) |
1586 | | -import Turtle (echo, fromString) |
1587 | | - |
1588 | | -main = do |
1589 | | - args <- getArgs |
1590 | | - echo $ fromString $ intercalate ", " args |
1591 | | -~~~ |
1592 | | - |
1593 | | -is equivalent to the following command at the command line: |
1594 | | - |
1595 | | -~~~text |
1596 | | -stack script --resolver lts-19.28 -- MyScript.hs arg1 arg2 +RTS -s -RTS |
1597 | | -~~~ |
1598 | | - |
1599 | | -where `+RTS -s -RTS` are some of GHC's |
1600 | | -[runtime system (RTS) options](https://downloads.haskell.org/~ghc/latest/docs/users_guide/runtime_control.html). |
1601 | | - |
1602 | | -### Just-in-time compilation |
1603 | | - |
1604 | | -As with using `stack script` at the command line, you can pass the `--compile` |
1605 | | -flag to make Stack compile the script, and then run the compiled executable. |
1606 | | -Compilation is done quickly, without optimization. To compile with optimization, |
1607 | | -pass the `--optimize` flag instead. Compilation is done only if needed; if the |
1608 | | -executable already exists, and is newer than the script, Stack just runs the |
1609 | | -executable directly. |
1610 | | - |
1611 | | -This feature can be good for speed (your script runs faster) and also for |
1612 | | -durability (the executable remains runnable even if the script is disturbed, eg |
1613 | | -due to changes in your installed GHC/snapshots, changes to source files during |
1614 | | -git bisect, etc.) |
1615 | | - |
1616 | | -### Using multiple packages |
1617 | | - |
1618 | | -As with using `stack script` at the command line, you can also specify multiple |
1619 | | -packages, either with multiple `--package` options, or by providing a comma or |
1620 | | -space separated list. For example: |
1621 | | - |
1622 | | -~~~haskell |
1623 | | -#!/usr/bin/env stack |
1624 | | -{- stack script |
1625 | | - --resolver lts-19.28 |
1626 | | - --package turtle |
1627 | | - --package "stm async" |
1628 | | - --package http-client,http-conduit |
1629 | | --} |
1630 | | -~~~ |
1631 | | - |
1632 | | -### Stack configuration for scripts |
1633 | | - |
1634 | | -With the `stack script` command, all Stack YAML configuration files are ignored. |
1635 | | - |
1636 | | -With the `stack runghc` command, if the current working directory is inside a |
1637 | | -project then that project's Stack project-level YAML configuration is effective |
1638 | | -when running the script. Otherwise the script uses the global project |
1639 | | -configuration specified in `<Stack root>/global-project/stack.yaml`. |
1640 | | - |
1641 | | -### Testing scripts |
1642 | | - |
1643 | | -You can use the flag `--script-no-run-compile` on the command line to enable (it |
1644 | | -is disabled by default) the use of the `--no-run` option with `stack script` |
1645 | | -(and forcing the `--compile` option). The flag may help test that scripts |
1646 | | -compile in CI (continuous integration). |
1647 | | - |
1648 | | -For example, consider the following simple script, in a file named `Script.hs`, |
1649 | | -which makes use of the joke package |
1650 | | -[`acme-missiles`](https://hackage.haskell.org/package/acme-missiles): |
1651 | | - |
1652 | | -~~~haskell |
1653 | | -{- stack script |
1654 | | - --resolver lts-19.28 |
1655 | | - --package acme-missiles |
1656 | | --} |
1657 | | -import Acme.Missiles (launchMissiles) |
1658 | | - |
1659 | | -main :: IO () |
1660 | | -main = launchMissiles |
1661 | | -~~~ |
1662 | | - |
1663 | | -The command `stack --script-no-run-compile Script.hs` then behaves as if the |
1664 | | -command |
1665 | | -`stack script --resolver lts-19.28 --package acme-missiles --no-run --compile -- Script.hs` |
1666 | | -had been given. `Script.hs` is compiled (without optimisation) and the resulting |
1667 | | -executable is not run: no missiles are launched in the process! |
1668 | | - |
1669 | | -### Writing independent and reliable scripts |
1670 | | - |
1671 | | -The `stack script` command will automatically: |
1672 | | - |
1673 | | -* Install GHC and libraries if missing |
1674 | | -* Require that all packages used be explicitly stated on the command line |
1675 | | - |
1676 | | -This ensures that your scripts are _independent_ of any prior deployment |
1677 | | -specific configuration, and are _reliable_ by using exactly the same version of |
1678 | | -all packages every time it runs so that the script does not break by |
1679 | | -accidentally using incompatible package versions. |
1680 | | - |
1681 | | -In earlier versions of Stack, the `runghc` command was used for scripts and can |
1682 | | -still be used in that way. In order to achieve the same effect with the `runghc` |
1683 | | -command, you can do the following: |
1684 | | - |
1685 | | -1. Use the `--install-ghc` option to install the compiler automatically |
1686 | | -2. Explicitly specify all packages required by the script using the `--package` |
1687 | | - option. Use `-hide-all-packages` GHC option to force explicit specification |
1688 | | - of all packages. |
1689 | | -3. Use the `--resolver` Stack option to ensure a specific GHC version and |
1690 | | - package set is used. |
1691 | | - |
1692 | | -It is possible for configuration files to affect `stack runghc`. For that |
1693 | | -reason, `stack script` is strongly recommended. For those curious, here is an |
1694 | | -example with `runghc`: |
1695 | | - |
1696 | | -~~~haskell |
1697 | | -#!/usr/bin/env stack |
1698 | | -{- stack |
1699 | | - runghc |
1700 | | - --install-ghc |
1701 | | - --resolver lts-19.17 |
1702 | | - --package base |
1703 | | - --package turtle |
1704 | | - -- |
1705 | | - -hide-all-packages |
1706 | | - -} |
1707 | | -~~~ |
1708 | | - |
1709 | | -The `runghc` command is still very useful, especially when you're working on a |
1710 | | -project and want to access the package databases and configurations used by that |
1711 | | -project. See the next section for more information on configuration files. |
1712 | | - |
1713 | | -### Loading scripts in GHCi |
1714 | | - |
1715 | | -Sometimes you want to load your script in GHCi to play around with your program. |
1716 | | -In those cases, you can use `exec ghci` option in the script to achieve |
1717 | | -it. Here is an example: |
1718 | | - |
1719 | | -~~~haskell |
1720 | | -#!/usr/bin/env stack |
1721 | | -{- stack |
1722 | | - exec ghci |
1723 | | - --install-ghc |
1724 | | - --resolver lts-19.28 |
1725 | | - --package turtle |
1726 | | --} |
1727 | | -~~~ |
1728 | | - |
1729 | 1399 | ## Finding project configs, and the implicit global project |
1730 | 1400 |
|
1731 | 1401 | Whenever you run something with Stack, it needs a project-level configuration |
@@ -1922,21 +1592,3 @@ Cabal (the tool), and NixOS. In that sense, we're sharing the same ecosystem. |
1922 | 1592 | in favor of Stack in 2015. |
1923 | 1593 | * [cabal-dev](https://hackage.haskell.org/package/cabal-dev). Deprecated in |
1924 | 1594 | favor of Cabal (the tool) in 2013. |
1925 | | -
|
1926 | | -## More resources |
1927 | | -
|
1928 | | -There are lots of resources available for learning more about Stack: |
1929 | | -
|
1930 | | -* `stack` or `stack --help` — lists Stack's commands, and flags and options |
1931 | | - common to those commands |
1932 | | -* `stack <command> --help` — provides help on the particular Stack command, |
1933 | | - including flags and options specific to the command |
1934 | | -* `stack --version` — identify the version and Git hash of the Stack executable |
1935 | | -* `--verbose` (or `-v`) — much more info about internal operations (useful for |
1936 | | - bug reports) |
1937 | | -* The [home page](http://haskellstack.org) |
1938 | | -* The [Stack mailing list](https://groups.google.com/d/forum/haskell-stack) |
1939 | | -* The [FAQ](faq.md) |
1940 | | -* The [haskell-stack tag on Stack Overflow](http://stackoverflow.com/questions/tagged/haskell-stack) |
1941 | | -* [Another getting started with Stack tutorial](http://seanhess.github.io/2015/08/04/practical-haskell-getting-started.html) |
1942 | | -* [Why is Stack not Cabal?](https://www.fpcomplete.com/blog/2015/06/why-is-stack-not-cabal) |
0 commit comments