10
10
You can use ` stack <file_name> ` to execute a Haskell source file. Usually, the
11
11
Stack command to be applied is specified using a special Haskell comment (the
12
12
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.
16
16
17
17
An example will be easiest to understand. Consider the Haskell source file
18
18
` turtle-example.hs ` with contents:
@@ -77,11 +77,11 @@ dependencies are built) and subsequent times more promptly (as the runs are
77
77
able to reuse everything already built).
78
78
79
79
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 ` ).
85
85
86
86
## Arguments and interpreter options and arguments
87
87
@@ -140,12 +140,12 @@ where `+RTS -s -RTS` are some of GHC's
140
140
141
141
## Just-in-time compilation
142
142
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.
149
149
150
150
This feature can be good for speed (your script runs faster) and also for
151
151
durability (the executable remains runnable even if the script is disturbed, eg
@@ -154,9 +154,9 @@ git bisect, etc.)
154
154
155
155
## Using multiple packages
156
156
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:
160
160
161
161
~~~ haskell
162
162
#!/usr/bin/env stack
@@ -170,20 +170,27 @@ space separated list. For example:
170
170
171
171
## Stack configuration for scripts
172
172
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.
175
176
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.
180
187
181
188
## Testing scripts
182
189
183
190
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).
187
194
188
195
For example, consider the following simple script, in a file named ` Script.hs ` ,
189
196
which makes use of the joke package
@@ -208,20 +215,21 @@ executable is not run: no missiles are launched in the process!
208
215
209
216
## Writing independent and reliable scripts
210
217
211
- The ` stack script ` command will automatically:
218
+ The [ ` stack script ` ] ( script_command.md ) command will automatically:
212
219
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.
215
222
* Require that all packages used be explicitly stated on the command line.
216
223
217
224
This ensures that your scripts are _ independent_ of any prior deployment
218
225
specific configuration, and are _ reliable_ by using exactly the same version of
219
226
all packages every time it runs so that the script does not break by
220
227
accidentally using incompatible package versions.
221
228
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:
225
233
226
234
1 . Use the ` --install-ghc ` option to install the compiler automatically
227
235
2 . Explicitly specify all packages required by the script using the ` --package `
@@ -230,9 +238,10 @@ command, you can do the following:
230
238
3 . Use the ` --snapshot ` Stack option to ensure a specific GHC version and
231
239
package set is used.
232
240
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 ) :
236
245
237
246
~~~ haskell
238
247
#!/usr/bin/env stack
@@ -247,9 +256,10 @@ example with `runghc`:
247
256
-}
248
257
~~~
249
258
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.
253
263
254
264
## Loading scripts in GHCi
255
265
0 commit comments