Stack: Redirecting output of `--exec` command

Created on 4 Feb 2019  ·  6Comments  ·  Source: commercialhaskell/stack

I'm running my program with stack build --file-watch --exec, but I can't figure out how to redirect its output. If I try

stack build --file-watch --exec "my-prog with some-args" > output.txt

then the output of stack is of course also sent there, and the output of subsequent runs of my program are appended to the file, leaving old runs there.

If I try

stack build --file-watch --exec "my-prog with some-args > output.txt"

then > and output.txt are also sent as arguments to my-prog, instead of used to redirect stdout.

This applies to stack 1.9.3, tested both on Windows (PowerShell) and on Ubuntu (Bash).

build documentation help wanted documentation

All 6 comments

Output redirection is a feature of shell so you could just run an extra shell, e.g.:

qrilka@qdesktop /tmp $ stack exec -v -- bash -c "ls > out"
Version 1.9.3, Git revision 40cf7b37526b86d1676da82167ea8758a854953b (6211 commits) x86_64 hpack-0.31.1
2019-02-04 11:27:02.029556: [debug] Checking for project config at: /tmp/stack.yaml
2019-02-04 11:27:02.075213: [debug] Checking for project config at: /stack.yaml
2019-02-04 11:27:02.075294: [debug] No project config file found, using defaults.
2019-02-04 11:27:02.076583: [debug] Run from outside a project, using implicit global project config
2019-02-04 11:27:02.076868: [debug] Using resolver: ghc-8.2.2 from implicit global project's config file: /home/qrilka/.stack/global-project/stack.yaml
2019-02-04 11:27:02.078031: [debug] Run process: /sbin/ldconfig -p
2019-02-04 11:27:02.080057: [debug] Process finished in 2ms: /sbin/ldconfig -p
2019-02-04 11:27:02.084587: [debug] Did not find shared library libtinfo.so.5
2019-02-04 11:27:02.084693: [debug] Found shared library libtinfo.so.6 in 'ldconfig -p' output
2019-02-04 11:27:02.084753: [debug] Found shared library libncursesw.so.6 in 'ldconfig -p' output
2019-02-04 11:27:02.084823: [debug] Found shared library libgmp.so.10 in 'ldconfig -p' output
2019-02-04 11:27:02.084933: [debug] Did not find shared library libgmp.so.3
2019-02-04 11:27:02.084966: [debug] Potential GHC builds: tinfo6, ncurses6
2019-02-04 11:27:02.085013: [debug] Found already installed GHC builds: tinfo6
2019-02-04 11:27:02.087997: [debug] Getting global package database location
2019-02-04 11:27:02.088115: [debug] Getting Cabal package version
2019-02-04 11:27:02.088160: [debug] Asking GHC for its version
2019-02-04 11:27:02.088220: [debug] Run process: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc-pkg --no-user-package-db list --global
2019-02-04 11:27:02.088623: [debug] Run process: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc-pkg --no-user-package-db field --simple-output Cabal version
2019-02-04 11:27:02.091922: [debug] Run process: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc --numeric-version
2019-02-04 11:27:02.107769: [debug] Process finished in 19ms: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc-pkg --no-user-package-db list --global
2019-02-04 11:27:02.111347: [debug] Process finished in 23ms: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc-pkg --no-user-package-db field --simple-output Cabal version
2019-02-04 11:27:02.127958: [debug] Process finished in 35ms: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc --numeric-version
2019-02-04 11:27:02.128060: [debug] GHC version is: ghc-8.2.2
2019-02-04 11:27:02.128152: [debug] Resolving package entries
2019-02-04 11:27:02.128274: [debug] Trying to decode /home/qrilka/.stack/loaded-snapshot-cache/x86_64-linux/ghc-8.2.2/ghc-8.2.2.cache
2019-02-04 11:27:02.128392: [debug] Error while decoding /home/qrilka/.stack/loaded-snapshot-cache/x86_64-linux/ghc-8.2.2/ghc-8.2.2.cache: PeekException {peekExBytesFromEnd = 35336, peekExMessage = "Mismatch detected by Data.Store.Version - expected ls-v5 but got ls-v4"} (this might not be an error, when switching between stack versions)
2019-02-04 11:27:02.128445: [debug] Failure decoding /home/qrilka/.stack/loaded-snapshot-cache/x86_64-linux/ghc-8.2.2/ghc-8.2.2.cache
2019-02-04 11:27:02.128491: [debug] Run process: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc-pkg --global --no-user-package-db dump --expand-pkgroot
2019-02-04 11:27:02.157848: [debug] Process finished in 29ms: /home/qrilka/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/bin/ghc-pkg --global --no-user-package-db dump --expand-pkgroot
2019-02-04 11:27:02.158400: [debug] Encoding /home/qrilka/.stack/loaded-snapshot-cache/x86_64-linux/ghc-8.2.2/ghc-8.2.2.cache
2019-02-04 11:27:02.160939: [debug] Finished writing /home/qrilka/.stack/loaded-snapshot-cache/x86_64-linux/ghc-8.2.2/ghc-8.2.2.cache
2019-02-04 11:27:02.161315: [debug] Starting to execute command inside EnvConfig
2019-02-04 11:27:02.161677: [debug] Run process: /home/qrilka/.nix-profile/bin/bash -c "ls > out"
qrilka@qdesktop /tmp $ head out
emacs1000
ghc12399_0
ghc17809_0
ghc17967_0
ghc21508_0
ghc26067_0
ghc3681_0
mc-qrilka
nix-shell-23347-0
nix-shell-24296-0

Ah, yes - that works, of course. Thanks!

It would be nice if this was a special case that stack could handle, but I do understand that there are _very_ many file redirection variants supported by various shells, so choosing a (sub)set of them to implement is probably infeasible.

This belongs in the docs. PRs welcome!

Coming back to this, I can't get the options right for stack build --exec... What would the corresponding incantation be to redirect output in that case?

My recommendation in this case would be to put the calls, together with any output redirection, into a separate shell script and --exec that script. I find it too complicated myself to deal with all of the details of shell escaping that ensue otherwise.

I think my previous comment addresses this issue, closing.

Was this page helpful?
0 / 5 - 0 ratings