Pass stack install `--flag` to stack run? - haskell-stack

How do I pass a flag to stack run like I'd do with stack install --flag asd:qwe?

I couldn't find a way to do this, so here is my suggested workaround:
Instead of using stack run ..., build your application using stack build and run the program directly.
So, instead of using
# The Problem: This doesn't work because run doesn't recognize --flag
> stack run --flag asd:qwe
use the following two-step approach:
stack build --flag asd:qwe
./path/to/your/executable/<your executable>
If you're using stack install instead of stack build, stack puts the compiled binary in the local-bin directory.¹ The documentation suggests that you add this directory to your $PATH variable, which means you can just call <your-executable>.
The rest of this response assumes that you are using stack build.
On my system (Arch linux, May '19, stack 1.9.3.1), stack puts the compiled binary in the bin-folder inside the local-install-root. (Show it using stack path --local-install-root.)
The complete path to the executable should be <local-install-root>/bin/<executable-name>
¹ According to The User guide there is regular confusion about the meaning of stack install. It's an alias to stack build --copy-bins, which just copies the binary to local-bin-path after building, which makes it generally available if your $PATH includes local-bin-path

Related

How do I run a GO executable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I was trying not to use the default go workspace, but to create a separate workspace location. So I did the following:
Created a folder, say "/Users/user_name/some_path/go_files/"
Created 3 directories in that folder - src, pkg and bin
Created a go file called "hello_world.go" inside "/Users/user_name/some_path/go_files/src"
Exported the GOPATH variable to "/Users/user_name/some_path/go_files"
Built the executable in 2 ways: go build src/hello_world.go and cd src go build hello_word.go
The above generated 2 executables - 1 in go_files/src and the other in go_files
However, running either of them gives an error saying: package hello_world is not in GOROOT (/usr/local/go/src/hello_world)
I tried to run the executable using go run hello_world. How do I resolve this? How do I run an executable?
Please note that I can do a go run hello_world.go from inside src or a go run src/hello_world.go, but I want to know how to run the executable.
However, running either of them gives an error saying: package hello_world is not in GOROOT (/usr/local/go/src/hello_world)
You haven't actually said what you're doing to provoke this error, but it sounds like you're almost certainly running go run hello_world. Once you've build an executable, Go (the language) and go (the command) are no longer involved. Binaries run independently of their compiler, which may be surprising to you if you're coming from an interpreted language background.
You run any given executable by typing its name (if it's in your path) or by typing the path to it (ie ./hello_world) and then pressing "return".

How to install `runhaskell`

Where is runhaskell supposed to come from? It is not on my path, and when I run stack build runhaskell, I get Unknown package: runhaskell.
stack exec -- runhaskell --help sounds like the right tool, though I'm not sure what the best answer is if another tool expects it to be available unqualified.

What does `stack haddock --skip <pkgname>` do?

After being unable to run stack haddock on my project because one package causes it to hang (see How to show progress of `stack haddock`? ) I tried to skip that package with
stack haddock --skip haskell-src-exts
But it still tries to haddock haskell-src-exts anyway (and hangs). What is --skip supposed to do?
--skip was introduced in stack 1.6.1 to avoid building a specific component of a project. A component corresponded to a test suite, an executable or a benchmark suite. As backpack support is implemented, this will have to include libraries, too.
From stack build --help
--skip ARG Skip given component, can be specified multiple times
The docs go into more detail.
--skip, to skip building components of a local package. It allows
you to skip test suites and benchmark without specifying other components
(e.g. stack test --skip long-test-suite will run the tests without the
long-test-suite test suite). Be aware that skipping executables won't work
the first time the package is built due to
an issue in cabal.
This option can be specified multiple times to skip multiple components.
For example, a package my-package might have a library, an executable my-executable, and two test suites (unit-tests and integration-tests). To build and run tests, you may run stack test. Adding --skip integration-tests will cause the integration-tests component not to be built (nor run).
Unfortunately, the current version of stack (1.9 branch) does not support skipping haddock for individual dependencies, though there is something like this due in the near future. I encountered this exact same problem (with haskell-src-exts). For now, I suggest skipping all haddock dependencies (--no-haddock-deps).
You may wish to invoke haddock with stack exec -- haddock if you wish to pass arguments to haddock which do not work with the stack flag --haddock-arguments.

Using stack ghc as replacement of ghc

Packages that are distributed without using stack or cabal usually have a set of implied dependencies. This sometimes mean that running ghc directly on said packages will not work without installing packages into ghcs global package database.
I would like to use stack ghc as a replacement for ghc to this end I have defined the following in my shells init file:
export PATH="`stack path --compiler-bin`:$PATH"
Now the command ghc will use stack's ghc - it does not however do the same as stack ghc. For one, it does not load the "snapshot" and "global-project" package database as can be seen with the following commands:
$ ghc -v
Glasgow Haskell Compiler, Version 8.0.1, stage 2 booted by GHC version 7.10.3
Using binary package database: ~/.stack/programs/x86_64-linux/ghc-nopie-8.0.1/lib/ghc-8.0.1/package.conf.d/package.cache
...
$ stack ghc -- -v
Glasgow Haskell Compiler, Version 8.0.1, stage 2 booted by GHC version 7.10.3
Using binary package database: ~/.stack/programs/x86_64-linux/ghc-nopie-8.0.1/lib/ghc-8.0.1/package.conf.d/package.cache
Using binary package database: ~/.stack/snapshots/x86_64-linux-nopie/lts-7.9/8.0.1/pkgdb/package.cache
Using binary package database: ~/.stack/global-project/.stack-work/install/x86_64-linux-nopie/lts-7.9/8.0.1/pkgdb/package.cache
...
Does anyone have guidance on how to achieve using stack ghc as a replacement for ghc.
The use-case I currently have is that I have been given a make-file that refers to ghc - but I do not have this command on my machine.
If you execute the makefile within stack exec, it will set the GHC_PACKAGE_PATH environment variable. It will also extend PATH appropriately.
Alternatively, to have this set in your shell you could do
export GHC_PACKAGE_PATH=`stack path --ghc-package-path`

is there a way to use the interactive `cabal init` from stack

Let's say I have a folder with some .hs files and I want to wrap it in a project
If I have cabal install and stack it's no big deal:
use cabal init (interactive choosing stuff)
go on with stack init
but when I just go the easy - just install stack route I don't have cabal install available - only stack
Now stack init does not work without a .cabal (or .package) file and for stack new I know of no template that does not set up additional files/folders.
Probably I'm missing something obvious and there is a stack parameter/command for this?
If not: What's the proper way to handle this? Install cabal-install?

Resources