Skip to content

Commit 389917f

Browse files
committed
Re commercialhaskell#6603 Improve online docs as related to configuration files
1 parent 39d484d commit 389917f

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

doc/script_command.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ The `stack script` command either runs a specified Haskell source file (using
1313
GHC's `runghc`) or, optionally, compiles such a file (using GHC) and, by
1414
default, runs it.
1515

16-
Unlike `stack ghc` and `stack runghc`, the command ignores all Stack YAML
17-
configuration files (global and project-level). A snapshot must be specified on
18-
the command line (with the `--snapshot` option). For example:
16+
Unlike [`stack ghc`](ghc_command.md) and [`stack runghc`](runghc_command.md),
17+
the command ignores any project-level configuration file (`stack.yaml`, by
18+
default) (including in the `global-project` directory in the Stack root). A
19+
snapshot must be specified on the command line (with the `--snapshot` option).
20+
For example:
1921

2022
~~~text
2123
stack script --snapshot lts-22.21 MyScript.hs
2224
~~~
2325

26+
!!! info
27+
28+
Non-project level configuration options in global configuration files
29+
(`config.yaml`), are not ignored. Such options may be useful if
30+
[`allow-newer`](yaml_configuration.md#allow-newer) and/or
31+
[`allow-newer-deps`](yaml_configuration.md#allow-newer-deps) are required.
32+
2433
The `stack script` command behaves as if the `--install-ghc` flag had been
2534
passed at the command line.
2635

doc/scripts.md

+48-38
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ that.
1010
You can use `stack <file_name>` to execute a Haskell source file. Usually, the
1111
Stack command to be applied is specified using a special Haskell comment (the
1212
Stack interpreter options comment) at the start of the source file. That command
13-
is most often `stack script` but it can be, for example, `stack runghc`. If
14-
there is no Stack interpreter options comment, Stack will warn that one was
15-
expected.
13+
is most often [`stack script`](script_command.md) but it can be, for example,
14+
[`stack runghc`](runghc_command.md). If there is no Stack interpreter options
15+
comment, Stack will warn that one was expected.
1616

1717
An example will be easiest to understand. Consider the Haskell source file
1818
`turtle-example.hs` with contents:
@@ -77,11 +77,11 @@ dependencies are built) and subsequent times more promptly (as the runs are
7777
able to reuse everything already built).
7878

7979
The second line of the source code is the Stack interpreter options comment. In
80-
this example, it specifies the `stack script` command with the options of a
81-
LTS Haskell 22.21 snapshot (`--snapshot lts-22.21`) and ensuring the
82-
[`turtle` package](https://hackage.haskell.org/package/turtle) is available
83-
(`--package turtle`). The version of the package will be that in the specified
84-
snapshot (`lts-22.21` provides `turtle-1.6.2`).
80+
this example, it specifies the [`stack script`](script_command.md) command with
81+
the options of a LTS Haskell 22.21 snapshot (`--snapshot lts-22.21`) and
82+
ensuring the [`turtle` package](https://hackage.haskell.org/package/turtle) is
83+
available (`--package turtle`). The version of the package will be that in the
84+
specified snapshot (`lts-22.21` provides `turtle-1.6.2`).
8585

8686
## Arguments and interpreter options and arguments
8787

@@ -140,12 +140,12 @@ where `+RTS -s -RTS` are some of GHC's
140140

141141
## Just-in-time compilation
142142

143-
As with using `stack script` at the command line, you can pass the `--compile`
144-
flag to make Stack compile the script, and then run the compiled executable.
145-
Compilation is done quickly, without optimization. To compile with optimization,
146-
pass the `--optimize` flag instead. Compilation is done only if needed; if the
147-
executable already exists, and is newer than the script, Stack just runs the
148-
executable directly.
143+
As with using [`stack script`](script_command.md) at the command line, you can
144+
pass the `--compile` flag to make Stack compile the script, and then run the
145+
compiled executable. Compilation is done quickly, without optimization. To
146+
compile with optimization, pass the `--optimize` flag instead. Compilation is
147+
done only if needed; if the executable already exists, and is newer than the
148+
script, Stack just runs the executable directly.
149149

150150
This feature can be good for speed (your script runs faster) and also for
151151
durability (the executable remains runnable even if the script is disturbed, eg
@@ -154,9 +154,9 @@ git bisect, etc.)
154154

155155
## Using multiple packages
156156

157-
As with using `stack script` at the command line, you can also specify multiple
158-
packages, either with multiple `--package` options, or by providing a comma or
159-
space separated list. For example:
157+
As with using [`stack script`](script_command.md) at the command line, you can
158+
also specify multiple packages, either with multiple `--package` options, or by
159+
providing a comma or space separated list. For example:
160160

161161
~~~haskell
162162
#!/usr/bin/env stack
@@ -170,20 +170,27 @@ space separated list. For example:
170170

171171
## Stack configuration for scripts
172172

173-
With the `stack script` command, all Stack YAML configuration files (global and
174-
project-level) are ignored.
173+
As with using [`stack script`](script_command.md) at the command line, any
174+
project-level configuration file (`stack.yaml`, by default) (including in the
175+
`global-project` directory in the Stack root) is ignored.
175176

176-
With the `stack runghc` command, if the current working directory is inside a
177-
project then that project's Stack project-level YAML configuration is effective
178-
when running the script. Otherwise the script uses the global project
179-
configuration specified in `<Stack root>/global-project/stack.yaml`.
177+
!!! info
178+
179+
Non-project level configuration options in global configuration files
180+
(`config.yaml`), are not ignored.
181+
182+
With the [`stack runghc`](runghc_command.md) command, if the current working
183+
directory is inside a project then that project's Stack project-level
184+
configuration file is effective when running the script. Otherwise the script
185+
uses the project-level configuration file in the `global-project` directory of
186+
the Stack root.
180187

181188
## Testing scripts
182189

183190
You can use the flag `--script-no-run-compile` on the command line to enable (it
184-
is disabled by default) the use of the `--no-run` option with `stack script`
185-
(and forcing the `--compile` option). The flag may help test that scripts
186-
compile in CI (continuous integration).
191+
is disabled by default) the use of the `--no-run` option with
192+
[`stack script`](script_command.md) (and forcing the `--compile` option). The
193+
flag may help test that scripts compile in CI (continuous integration).
187194

188195
For example, consider the following simple script, in a file named `Script.hs`,
189196
which makes use of the joke package
@@ -208,20 +215,21 @@ executable is not run: no missiles are launched in the process!
208215

209216
## Writing independent and reliable scripts
210217

211-
The `stack script` command will automatically:
218+
The [`stack script`](script_command.md) command will automatically:
212219

213-
* Install GHC and libraries, if missing. `stack script` behaves as if the
214-
`--install-ghc` flag had been passed at the command line.
220+
* Install GHC and libraries, if missing. [`stack script`](script_command.md)
221+
behaves as if the `--install-ghc` flag had been passed at the command line.
215222
* Require that all packages used be explicitly stated on the command line.
216223

217224
This ensures that your scripts are _independent_ of any prior deployment
218225
specific configuration, and are _reliable_ by using exactly the same version of
219226
all packages every time it runs so that the script does not break by
220227
accidentally using incompatible package versions.
221228

222-
In earlier versions of Stack, the `runghc` command was used for scripts and can
223-
still be used in that way. In order to achieve the same effect with the `runghc`
224-
command, you can do the following:
229+
In earlier versions of Stack, the [`stack runghc`](runghc_command.md) command
230+
was used for scripts and can still be used in that way. In order to achieve the
231+
same effect with the [`stack runghc`](runghc_command.md) command, you can do the
232+
following:
225233

226234
1. Use the `--install-ghc` option to install the compiler automatically
227235
2. Explicitly specify all packages required by the script using the `--package`
@@ -230,9 +238,10 @@ command, you can do the following:
230238
3. Use the `--snapshot` Stack option to ensure a specific GHC version and
231239
package set is used.
232240

233-
It is possible for configuration files to affect `stack runghc`. For that
234-
reason, `stack script` is strongly recommended. For those curious, here is an
235-
example with `runghc`:
241+
It is possible for a project-level configuration file to affect
242+
[`stack runghc`](runghc_command.md). For that reason,
243+
[`stack script`](script_command.md) is strongly recommended. For those curious,
244+
here is an example with [`stack runghc`](runghc_command.md):
236245

237246
~~~haskell
238247
#!/usr/bin/env stack
@@ -247,9 +256,10 @@ example with `runghc`:
247256
-}
248257
~~~
249258

250-
The `runghc` command is still very useful, especially when you're working on a
251-
project and want to access the package databases and configurations used by that
252-
project. See the next section for more information on configuration files.
259+
The [`stack runghc`](runghc_command.md) command is still useful, especially when
260+
you're working on a project and want to access the package databases and
261+
configurations used by that project. See the next section for more information
262+
on configuration files.
253263

254264
## Loading scripts in GHCi
255265

0 commit comments

Comments
 (0)