3

ghc-mod works in a directory with just a haskell source file but if I run "cabal init" in that directory (/tmp/test), I get the following error:

(p1)dave@peach:/tmp/test$ !ghc
ghc-mod check Main.hs 
ghc-mod: /tmp/test/dist/setup-config: hGetContents: invalid argument (invalid byte sequence)

What's going wrong here?

I'm running ghc-mod 5.2.11, and cabal 1.21.1.0, on a 64 bit ubuntu 14.04 computer. the LANG environment variable is set to en_US.UTF-8 ( I saw responses to some other questions that suggest that the LANG setting may be important here ). I tried running cabal version 1.16.0 but it didn't make any difference.

Here's a transcript that shows more detail:

(p1)dave@peach:/tmp/test$ ls -la
total 80
drwxrwxr-x  2 dave dave  4096 Dec  4 07:07 .
drwxrwxrwt 35 root root 69632 Dec  4 07:06 ..
-rw-rw-r--  1 dave dave   143 Dec  4 07:04 Main.hs

(p1)dave@peach:/tmp/test$ set | grep LANG
GDM_LANG=en_US
LANG=en_US.UTF-8
LANGUAGE=en_US

(p1)dave@peach:/tmp/test$ ghc-mod version
ghc-mod version 5.2.1.1 compiled by GHC 7.6.3

(p1)dave@peach:/tmp/test$ cabal -V
cabal-install version 1.21.1.0
using version 1.21.1.0 of the Cabal library 

(p1)dave@peach:/tmp/test$ ghc-mod check Main.hs 
Main.hs:7:1:Warning: Top-level binding with no type signature:  haqify :: [Char] -> [Char]

(p1)dave@peach:/tmp/test$ cabal init
Package name? [default: test] 
Package version? [default: 0.1.0.0] 
Please choose a license:
 * 1) (none)
   2) GPL-2
   3) GPL-3
   4) LGPL-2.1
   5) LGPL-3
   6) AGPL-3
   7) BSD2
   8) BSD3
   9) MIT
  10) ISC
  11) MPL-2.0
  12) Apache-2.0
  13) PublicDomain
  14) AllRightsReserved
  15) Other (specify)
Your choice? [default: (none)] 2
Author name? [default: XXXXXXXXXXXX] 
Maintainer email? [default: XXXXXXXXXXXXXXXXXXXXXX] 
Project homepage URL? 
Project synopsis? 
Project category:
 * 1) (none)
   2) Codec
   3) Concurrency
   4) Control
   5) Data
   6) Database
   7) Development
   8) Distribution
   9) Game
  10) Graphics
  11) Language
  12) Math
  13) Network
  14) Sound
  15) System
  16) Testing
  17) Text
  18) Web
  19) Other (specify)
Your choice? [default: (none)] 
What does the package build:
   1) Library
   2) Executable
Your choice? 2
What is the main module of the executable:
 * 1) Main.hs
   2) Other (specify)
Your choice? [default: Main.hs] 
What base language is the package written in:
 * 1) Haskell2010
   2) Haskell98
   3) Other (specify)
Your choice? [default: Haskell2010] 
Include documentation on what each field means (y/n)? [default: n] 
Source directory:
 * 1) (none)
   2) src
   3) Other (specify)
Your choice? [default: (none)] 

Guessing dependencies...

Generating LICENSE...
Generating Setup.hs...
Generating test.cabal...

Warning: no synopsis given. You should edit the .cabal file and add one.
You may want to edit the .cabal file and add a Description field.

(p1)dave@peach:/tmp/test$ !ghc
ghc-mod check Main.hs 
ghc-mod: /tmp/test/dist/setup-config: hGetContents: invalid argument (invalid byte sequence)
(p1)dave@peach:/tmp/test$ 
Dave Compton
  • 1,421
  • 1
  • 11
  • 18
  • ghc-mod runs `cabal configure`, so I would capture the output of that command and see if you see anything suspicious. – ErikR Dec 04 '14 at 16:33
  • Also, what happens if you run `cabal configure` before running `ghc-mod check ...` ? – ErikR Dec 04 '14 at 16:35
  • 1
    Also check the file `dist/setup-config`. That's the file `ghc-mod` reads after running `cabal configure` – ErikR Dec 04 '14 at 16:39
  • cabal configure runs without any problems. dist/setup-config exists and appears to be a binary format - beyond that I don't know how to what to look for in dist/setup-config – Dave Compton Dec 04 '14 at 16:49
  • running "cabal configure" didn't affect the original problem. – Dave Compton Dec 04 '14 at 16:58
  • Does the file `dist/setup-config` contain any non-ASCII characters? Does the `.cabal` file contain any non-ASCII characters? – ErikR Dec 04 '14 at 17:05
  • There is a file being read which contains the "invalid byte sequence". I would use `strace` to trace all system calls being called so you can find out which files are being accessed. – ErikR Dec 04 '14 at 17:08
  • The dist/setup-config file contains many (mostly) non-ASCII characters. As I mentioned earlier it appears to be a binary file of some kind. There is no .cabal file. Should there be? – Dave Compton Dec 04 '14 at 17:32
  • strace shows that data is read from setup-config just before the "invalid byte sequence" message. That looks like the mostly likely suspect for the "invalid byte sequence" – Dave Compton Dec 04 '14 at 17:36
  • By `.cabal` file I mean the .cabal file created by `cabal init`. The `dist/setup-config` file should be an ASCII file, so the problem is with your `cabal` program. Example of a setup-config file: http://pastebin.com/dnFQnWfe – ErikR Dec 04 '14 at 17:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66228/discussion-between-dave-and-user5402). – Dave Compton Dec 04 '14 at 18:51
  • When I revert to cabal 1.20.0.2 the problem goes away - dist/setup-config is written in ascii and ghc-mod operates without any problems. My original bug report said that I had the same problem with 1.16.0 but that was incorrect - I had just switched to the 1.16.0 binary and was probably still using some libraries or config ( or something ) specific to cabal 1.21 . – Dave Compton Dec 04 '14 at 22:49
  • Note that the latest version on hackage is only 1.20.0.3 so perhaps 1.21 is not ready for general use. – ErikR Dec 04 '14 at 23:05
  • This appears to be explained here: https://github.com/kazu-yamamoto/ghc-mod/wiki/InconsistentCabalVersions And you may wish to subscribe to this issue: https://github.com/kazu-yamamoto/ghc-mod/issues/417 – ches Feb 02 '15 at 05:16

2 Answers2

3

ghc-mod doesn't work with GHC < 7.10 and Cabal-1.22.

If you are still on GHC 7.8.3 or 7.8.4, you may want to hold off on updating cabal or cabal-install.

Tonn
  • 106
  • 5
1

In short:

  1. Use sandbox for your project;
  2. Add to your PATH variable a local path for folders where sandbox is located;
  3. Install there Cabal version <= 1.20.0.6 (version 1.20.0.6 is the latest one which doesn't break ghc-mod);
  4. Add to your PATH variable a local path for folders where sandbox is located;
  5. Install to your sandbox required packages (ghc-mod etc).

Go to you project's root folder and create a sandbox:

cd ~/tmp/test       #go to you project's root directory
cabal sandbox init  #create a sandbox

Each time you run cabal sandbox init from your current directory, it creates a .cabal-sandbox folder in the given directory. If you install ghc-mod to your sandbox (i.e. isolated), it still will use not sandboxed version of cabal and cabal-install to install it, because it is in your PATH. To solve this problem, add to your ~/.bash_profile file the following line:

export PATH="./.cabal-sandbox/bin:$PATH"

The dot at the beginning of the path means that you want to search in your current directory a folder named .cabal-sandbox with subfolder named bin where your sandboxed packages are installed. If you run commands within folders which contain sandboxes, the system will run executable files from appropriate sandbox. If the folder contains no sandboxes, it will use usual path to your */.cabal/bin directory.

Make sure that your path to sandbox is located before your usual path to */.cabal/bin in your PATH variable. You can see the order of your paths with the terminal $PATH command.

Reload your ~/.bash_profile file with the terminal source ~/.bash_profile command or just close & reopen your terminal.

Next install Cabal version <= 1.20.0.6 which does not break ghc-mod:

cabal install cabal-install-1.20.0.6  #install the latest cabal supported by ghc-mod

Reload again your ~/.bash_profile file with the terminal source ~/.bash_profile command or just close & reopen your terminal.

Run cabal --version from your sandbox' folder. It should show that you are using cabal-install version 1.20.0.6 and version 1.20.0.3 of the Cabal library. Run cabal --version from the folder without sandbox to see that globally you still use your current versions of cabal-install and Cabal library.

Return to your project's root folder and install to the sandbox packages you need:

cd ~/tmp/test                 #go to you project's root directory
cabal install ghc-mod-5.2.1.2 #explicitly indicate version of ghc-mod you want to install, otherwise cabal will try to install an ancient version 0.3.0
Arthur Welf
  • 176
  • 1
  • 4
  • Thanks for the detaied info. Alas this didn't work for me on GHC 7.6.3. I still get the error hGetContents: invalid argument (invalid byte sequence) and I verified the versions of cabal, cabal-install, ghc-mod and that they were running the sandbox version (using which) – Martin Capodici Jun 03 '15 at 19:20
  • silly me I forgot to do cabal build again after the above instructions! now working – Martin Capodici Jun 03 '15 at 19:32