haskell / haskell-ide-engine

The engine for haskell ide-integration. Not an IDE
BSD 3-Clause "New" or "Revised" License
2.38k stars 213 forks source link

Can not load test/Main.hs with a hie-bios cabal cradle #1727

Closed mouse07410 closed 4 years ago

mouse07410 commented 4 years ago

MacOS Catalina 10.15.4, Xcode-11.4, current ghcup that installed GHC 8.8.3 and Cabal-3.2.0.0. Current HIE master, built with Cabal for target hie-8.8.3.

Current VSCode with Haskell Language Server plugin 0.0.37 (and a few more plugins).

I'm trying to add HSpec framework to the tests.

Problem: the very first import Test.Hspec is highlighted as an error. See the screenshot:

Screen Shot 2020-04-08 at 17 20 44

Here's the error message from the "Problems" tab:

{
    "resource": "/Users/ur20980/Src/Str2Split/test/Spec.hs",
    "owner": "Haskell HIE (Str2Split)",
    "severity": 8,
    "message": "Fail on initialisation for \"/Users/ur20980/Src/Str2Split/test/Spec.hs\".\nFailed to parse result of calling cabal\n\ncabal: Unknown target '/Users/ur20980/Src/Str2Split/test/Spec.hs'.\nThe package Str2Split has no file target 'test/Spec.hs'.\n\n\n\n\n",
    "source": "bios",
    "startLineNumber": 1,
    "startColumn": 1,
    "endLineNumber": 2,
    "endColumn": 1
}

You see the complete content of test/Spec.hs above. Here's Str2Split.cabal:

cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.31.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: 295f93990a5fee525c421b111658ccb55aa40dfb968670dede78f92d405738ba

name:           Str2Split
version:        0.1.0.0
description:    Please see the README on GitHub at <https://github.com/mouse07410/Str2Split#readme>
homepage:       https://github.com/mouse07410/Str2Split#readme
bug-reports:    https://github.com/mouse07410/Str2Split/issues
author:         Mouse
copyright:      Copyright (C) 2019 Mouse
license:        BSD3
license-file:   LICENSE
build-type:     Simple
extra-source-files:
    README.md
    ChangeLog.md

source-repository head
  type: git
  location: https://github.com/mouse07410/Str2Split

library
  exposed-modules:
      Lib
  other-modules:
      Paths_Str2Split
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
  default-language: Haskell2010

executable Str2Split-exe
  main-is: Main.hs
  other-modules:
      Paths_Str2Split
  hs-source-dirs:
      app
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      Str2Split
    , base >=4.7 && <5
  default-language: Haskell2010

test-suite Str2Split-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Paths_Str2Split
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      Str2Split
    , base >=4.7 && <5
    , hspec >=2.7
    , QuickCheck >=2.13
  default-language: Haskell2010

Both Cabal and Stack seem to be able to build both executables Str2Split-exe and Str2Split-test fine, despite the error messages. I can run both of them.

If you'd like to see the complete project - please see https://github.com/mouse07410/Str2Split.git

I do not claim that it's a bug - likely a pilot error. But would appreciate any help, please.

fendor commented 4 years ago

Thank you for your awesomely detailed bug report! To me, it looks like this is the manifestation of a known bug in hie-bios and cabal, see https://github.com/mpickering/hie-bios/issues/164 and the underlying cause https://github.com/haskell/cabal/issues/6622. Fix is https://github.com/haskell/cabal/pull/6623

Workaround: provide a full mapping from path to component.

mouse07410 commented 4 years ago

Thank you for your awesomely detailed bug report!

My pleasure - hope I can maintain that level!

To me, it looks like this is the manifestation of a known bug in hie-bios and cabal ... Fix is haskell/cabal#6623

So, this bug is not in HIE, but in Cabal itself? But the builds and runs seem to work, somehow...?

Workaround: provide a full mapping from path to component

Please pardon my ignorance - could you show what you mean/what the workaround should look like, and in what file(s)?

Update

Also, if I don't want to wait an unpredictable amount of time before the 6623 ends up in a released version of Cabal: a. Should I just clone Cabal repo, merge that PR, build, and install in place of a released Cabal? b. To get HIE employ this fix - probably having cabal-install executable is not good enough? How do I build HIE to incorporate this fix? Thanks!

Update 2

Please take a look at https://github.com/haskell/cabal/issues/6659# - looks like I cannot build a decent cabal-install from its Git repo.

fendor commented 4 years ago

So, this bug is not in HIE, but in Cabal itself? But the builds and runs seem to work, somehow...?

That is unrelated. What essentially happens in hie-bios (quite literally), with your hie.yaml, is the invocation cabal repl test/Spec.hs, which you can test in your terminal. In this case, it ought to fail with the same message as returned by your lsp message (piped through putStrLn):

Fail on initialisation for "/Users/ur20980/Src/Str2Split/test/Spec.hs".
Failed to parse result of calling cabal

cabal: Unknown target '/Users/ur20980/Src/Str2Split/test/Spec.hs'.
The package Str2Split has no file target 'test/Spec.hs'.

Please pardon my ignorance - could you show what you mean/what the workaround should look like, and in what file(s)?

Like what we do in haskell-ide-engine: https://github.com/haskell/haskell-ide-engine/blob/master/hie.yaml.cbl

a. Should I just clone Cabal repo, merge that PR, build, and install in place of a released Cabal? b. To get HIE employ this fix - probably having cabal-install executable is not good enough? How do I build HIE to incorporate this fix? Thanks!

It should be enough to just checkout the pr and install it, via cabal install exe:cabal (assuming cabal-install 3.0.0.0 or newer). HIE is not built against Cabal, it is enough if this modified version is on your PATH, e.g. cabal repl test/Spec.hs succeeds in your terminal, then it works with HIE.

mouse07410 commented 4 years ago

@fendor unfortunately, it looks like I cannot build a working cabal-install from local sources. It builds fine, but any attempt to actually install something with it results in abort on failed assertion.

As I said, my setup is building shared libraries and dynamic executables. Is there a way to build cabal-install with static libraries (it's impossible to force static executables on MacOS because some of the system libraries are shared only)? Or can you see another possible cause for this failed assertion? It's now 100% consistent, while downloaded cabal-install binaries appear to work fine...

Also, I'm confused with hie.yaml.cbl - what are "components"? Targets in <project>.cabal? Names of the executable or library files? Or...?

mouse07410 commented 4 years ago

@fendor I give up. I don't know Haskell well enough to either succeed building Cabal from source, or even enjoy the process. Nor do I understand what should go into hie.yaml - what is considered a "component"? When should "path" point at a directory (like - path: "./test/dispatcher/") and when at a specific file (like - path: "./app/MainHie.hs")? So far, no matter what I'm doing I get

$ cabal repl test/Spec.hs 
cabal: Unknown target 'test/Spec.hs'.
The package Str2Split has no file target 'test/Spec.hs'.

Here's my latest experimental hie.yaml, which unquestionably is incorrect:

cradle:
  cabal:
    - path: "./"
      component: "lib:Str2Split"

    - path: "./test/Spec.hs"
      component: "test-suite"

    - path: "./app/Main.hs"
      component: "executable"

Could you do me a favor? My sample project https://github.com/mouse07410/Str2Split.git seems small and simple enough. Would you be so kind to post the correct hie.yaml for this project? It has three "targets", as far as I understand - "library", "executable", and "test-suite".

Thanks in advance!

fendor commented 4 years ago

When should "path" point at a directory (like - path: "./test/dispatcher/") and when at a specific file (like - path: "./app/MainHie.hs")

You only provide an exact file match if there is an overlap in components. E.g. the main for the executable is defined in the library.

Could you do me a favor? My sample project https://github.com/mouse07410/Str2Split.git seems small and simple enough. Would you be so kind to post the correct hie.yaml for this project? It has three "targets", as far as I understand - "library", "executable", and "test-suite".

I made a PR

mouse07410 commented 4 years ago

@fendor Thank you!

mouse07410 commented 4 years ago

As your workaround works (thank you!) I'm closing this ticket.

If it's possible to make hie.yaml support both Cabal and Stack builds on that same project - I'd appreciate if you could post the updated variant here or make another PR.

Thanks again!

fendor commented 4 years ago

If it's possible to make hie.yaml support both Cabal and Stack builds on that same project - I'd appreciate if you could post the updated variant here or make another PR.

No, it isnt

fendor commented 4 years ago

Please open another issue and dont hijack the existing issue!