intellij-haskforce icon indicating copy to clipboard operation
intellij-haskforce copied to clipboard

Existing Stack project cannot be imported as module

Open erikhuizinga opened this issue 8 years ago • 4 comments
trafficstars

On macOS, in IntelliJ IDEA 2017.2.5, I can't import an existing Stack module into an empty project.

Steps to reproduce:

  1. Create a new IDEA project (Empty project, not a Haskell or other type of project), e.g. located at ~/Desktop/project-name.
  2. Terminal to that dir and stack new module-name, cd module-name, stack setup & stack build.
  3. In IntelliJ, hit ⌘; to open the Project Settings > Modules > Click the + or press ⌘N > Import Module > Locate module-name > Open > now either:
    1. Select Create module from existing sources > make sure the checkmark is selected on the path to the module-name dir > click Next > issue: IDEA says No framework detected, is that correct I'd expect the plugin detects a Haskell Stack project > click Finish.
    2. 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!

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?

erikhuizinga avatar Oct 06 '17 18:10 erikhuizinga

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 avatar Oct 06 '17 19:10 erikhuizinga

@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.

carymrobbins avatar Oct 06 '17 21:10 carymrobbins

To make a Haskell Stack module in another IntelliJ IDEA project, do the following:

  1. stack new [project name]
  2. Copy all the created files in the project dir to where you want the module, except for stack.yaml.
  3. Edit stack.yaml and under packages: change . to the relative or absolute path to the module.
  4. With your shell in the root of the Stack project, run stack setup & stack build.
  5. 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:

  1. Add a run configuration: Run menu > Edit Configurations... > click + > Haskell Stack Run
  2. In the right pane, enter name
  3. Set the Executable to the project name (and depending on the project template, append -exe to the name or not).
  4. 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.
  5. Add a Before launch task: click + > Run External tool
  6. Add an external tool by clicking + > name it > describe it (optionally) > deselect Open Console (optionally) > enter the path to sh or bash (find it using which sh or which bash in your shell) in the Program field > enter stack build in the Parameters field > enter $ProjectFileDir$ (this points to the project root dir, i.e., where stack.yaml should be) > OK > OK > OK
  7. Run and take a look at the pane in IntelliJ IDEA for the output.

erikhuizinga avatar Oct 07 '17 18:10 erikhuizinga

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.

carymrobbins avatar Oct 07 '17 18:10 carymrobbins