intellij-haskforce
intellij-haskforce copied to clipboard
Existing Stack project cannot be imported as module
On macOS, in IntelliJ IDEA 2017.2.5, I can't import an existing Stack module into an empty project.
Steps to reproduce:
- Create a new IDEA project (Empty project, not a Haskell or other type of project), e.g. located at
~/Desktop/project-name. - Terminal to that dir and
stack new module-name,cd module-name,stack setup & stack build. - In IntelliJ, hit ⌘; to open the Project Settings > Modules > Click the + or press ⌘N > Import Module > Locate
module-name> Open > now either:- Select Create module from existing sources > make sure the checkmark is selected on the path to the
module-namedir > click Next > issue: IDEA says No framework detected, is that correct I'd expect the plugin detects a Haskell Stack project > click Finish. - Select Import module from external model > Select Stack > click Next > make sure the paths to Stack and the YAML are correct > click Next > select the Stack project to import (it shows up as '.', is that correct?) > Finish > issue: nothing happens!
- Select Create module from existing sources > make sure the checkmark is selected on the path to the
Now, if you import the module via method 3.i. above, the module is loaded, albeit in a somewhat superficial state an possible not entirely correctly configured. For example, go to Run > Edit configurations... > click + or press ⌘N > select Haskell Stack Run > in the right pane, enter module-name-exe as the Executable > OK > Run the configuration with ⌃R > issue: /usr/local/bin/stack exec module-name-exe -- Executable named module-name-exe not found on path: {contents of $PATH}.
Does this happen because Stack's working dir is project-name instead of project-name/module-name? How can this be solved, or how should I import an existing Stack project as a module?
The following line maybe should not use getEnvironment.getProject.getBasePath, but some methods to get the module's base path (or more specifically, the module's content root, which can be more than one!). Possibly, this solves the problem of stack not finding module-name, but not the other issues marked in bold.
https://github.com/carymrobbins/intellij-haskforce/blob/40c0ba08f50eb3d9613eda571aed435adff47ff7/src/com/haskforce/run/stack/StackRunCommandLineState.scala#L14
@erikhuizinga That's a good point. Note however that each cabal package in your project (listed as packages in the stack.yaml) will become their own module in IntelliJ. We really need Framework support for Stack, then we can look up the Stack module that corresponds to whichever file(s) you are working with.
To make a Haskell Stack module in another IntelliJ IDEA project, do the following:
stack new [project name]- Copy all the created files in the
projectdir to where you want the module, except forstack.yaml. - Edit
stack.yamland underpackages:change.to the relative or absolute path to the module. - With your shell in the root of the Stack project, run
stack setup & stack build. - In IntelliJ IDEA, open the Project Structure (⌘; on macOS) > Modules > click + to add a module > New Module > Haskell > enter a name, set the content root to the module directory and the module file location to wherever you want > Finish.
Now there's a Haskell Stack module.
To auto-build the Haskell files using stack, do the following:
- Add a run configuration: Run menu > Edit Configurations... > click + > Haskell Stack Run
- In the right pane, enter name
- Set the Executable to the project name (and depending on the project template, append
-exeto the name or not). - Remove the Build task to run before launch if you don't need it; it won't build the Haskell Stack module, but other modules in your project.
- Add a Before launch task: click + > Run External tool
- Add an external tool by clicking + > name it > describe it (optionally) > deselect Open Console (optionally) > enter the path to
shorbash(find it usingwhich shorwhich bashin your shell) in the Program field > enterstack buildin the Parameters field > enter$ProjectFileDir$(this points to the project root dir, i.e., wherestack.yamlshould be) > OK > OK > OK - Run and take a look at the pane in IntelliJ IDEA for the output.
Thanks for providing a detailed workaround! I'll keep this open since we still need Stack Framework support to make this work more seamless in IntelliJ.