emacs haskell-mode with cabal project. "Unrecognised target syntax for ` --ghc-option=ferror-spans`." - haskell

I'm trying to get emacs haskell-mode working with a cabal project. Whenever I try to compile or load a file interactively I get the following output in the haskell-process-log
compiling (via (C-c C-c)):
cabal.exe: No targets given and there is no package in the current directory.
and when loading (via (C-c C-l)):
cabal.exe: Unrecognised target syntax for ' --ghc-option=-ferror-spans'
Set up:
Windows 10,
GHC 8.10.2,
Cabal 3.2.0.0,
emacs 27.1,
haskell-mode 20201120.755
My emacs init file has the following:
(require 'haskell-interactive-mode)
(require 'haskell-process)
(add-hook 'haskell-mode-hook 'interactive-haskell-mode)
(eval-after-load "haskell-mode"
'(define-key haskell-mode-map (kbd "C-c C-c") 'haskell-compile))
(eval-after-load "haskell-cabal"
'(define-key haskell-cabal-mode-map (kbd "C-c C-c") 'haskell-compile))
With a simple Haskell project, created by running cabal init
i.e. with a Main.hs:
module Main where
main :: IO ()
main = putStrLn "Hello, Haskell!"
and my-project.cabal :
cabal-version: >=1.10
-- Initial package description 'my-project.cabal' generated by
-- 'cabal init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: my-project
version: 0.1.0.0
-- synopsis:
-- description:
-- bug-reports:
-- license:
license-file: LICENSE
author: ################
maintainer: ##################
-- copyright:
-- category:
build-type: Simple
extra-source-files: CHANGELOG.md, README.md
executable my-project
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.14 && <4.15
hs-source-dirs:
default-language: Haskell2010
If I try to compile via C-c C-c I get the following in the haskell-process-log buffer:
cabal.exe: No targets given and there is no package in the current directory.
Use the target 'all' for all packages in the project or specify packages or
components by name or location. See 'cabal build --help' for more details on
target options.
If I try to load via C-c C-l the haskell process immediately dies prompting me to restart and the haskell-process-log displays:
cabal.exe: Unrecognised target syntax for ' --ghc-option=-ferror-spans'.
with '(haskell-process-log t) and '(haskell-process-show-debug-tips t) set within my init file custom vars this changes to:
("Starting inferior `cabal repl' process using cabal ..." "my-project" nil "cabal" "repl" " --ghc-option=-ferror-spans")
-> Prelude.putStrLn ""
:set -v1
:set +c
-> :set prompt "\4"
-> :set prompt-cont "λ| "
<- cabal.exe: Unrecognised target syntax for ' --ghc-option=-ferror-spans'.
Event: "exited abnormally with code 1 "
Process reset.
Everything works fine in emacs when working on a lone haskell file, i.e. not inside a cabal project.
Compilation and cabal repl within the cabal project via command line also works fine.
I've been searching through the docs and googling for a solid day and a half now. Any help would be appreciated.

This turned out to be a bug in Cabal. Specifically in the cabal-install command-line tool that prevented the automatic detection of the primary package on Windows due to Windows denoting drive letters with a capital letter.
I've submitted a pull request:
https://github.com/haskell/cabal/pull/7310

Related

Cabal install package in local directory not reflecting while importing in file

I am currently self studying Haskell. I am just a beginner so I haven't yet had a need to use cabal or stack. But right now I need to test some of my code using QuickCheck.
From this link that I found https://github.com/haskell/cabal/blob/master/doc/cabal-commands.rst , I ran the command cabal install --lib QuickCheck --package-env . and got the following output :
axiom#pop-os:~/Desktop/Haskell-Learning/Course/Homework 10$ cabal install --lib QuickCheck --package-env .
Resolving dependencies...
Up to date
In the same directory, I have a .hs file and in that when I tried to import Test.QuickCheck the linter gives an error as the package doesnt seem to be available for importing.
Then I ran cabal repl --build-depends QuickCheck and then in ghci I was able to import it. But still it was not importing in the code file.
Then when I just opened ghci by firing the command ghci , the following shows up, which suggests that there is a package environment here in this directory :
GHCi, version 8.10.7: [https://www.haskell.org/ghc/](https://www.haskell.org/ghc/) :? for help
Loaded package environment from /home/axiom/Desktop/Haskell-Learning/Course/Homework 10/.ghc.environment.x86\_64-linux-8.10.7
Prelude> import Test.QuickCheck
Prelude Test.QuickCheck> :q
Even after above, that is, being able to import QuickCheck in GHCi, the import is still not working in the file.
After this, I tried the following :
axiom#pop-os:~/Desktop/Haskell-Learning/Course/Homework 10$ cabal install QuickCheck
Resolving dependencies...
Up to date
Warning:
############################################################
# WARNING: Installation might not be completed as desired! #
############################################################
The command "cabal install [TARGETS]" doesn't expose libraries.
* You might have wanted to add them as dependencies to your package. In this
case add "QuickCheck" to the build-depends field(s) of your package's .cabal
file.
* You might have wanted to add them to a GHC environment. In this case use
"cabal install --lib QuickCheck". The "--lib" flag is provisional: see
https://github.com/haskell/cabal/issues/6481 for more information.
axiom#pop-os:~/Desktop/Haskell-Learning/Course/Homework 10$ cabal install --lib QuickCheck
Resolving dependencies...
Up to date
The import still doesn't work.
Any help is appreciated !
The method I ended up finding is the following :
Run the following in the directory where you have your haskell file where you want to import an external package :
cabal init
This will general a small number of files and such in that directory.
Add the name of the package that you want to use in the .cabal file that was generated . For example, I wanted to use QuickCheck so my .cabal file looks like this :
cabal-version: 2.4
name: Homework10
version: 0.1.0.0
author: Name Surname
maintainer: name#email.com
extra-source-files: CHANGELOG.md
executable Homework10
main-is: Main.hs
build-depends: base ^>=4.14.3.0, QuickCheck
hs-source-dirs: app
default-language: Haskell2010
Then in the same directory, run the following :
cabal build
Then when you try to import the package in your .hs, you should be able to do so.

How to configure .ghci file to import all loaded modules

let say I have a project which is just a bunch of Haskell modules with exercises. I'd like to provide a .ghci which automatically imports all modules into ghci scope. The problem is, I can not run import nor :m +Module.Name within .ghci file.
Clearly cabal repl is reading the .ghci file because options like the prompt are readed. Also it loads modules correctly, but it doesn't bring them into scope (only one gets imported). If I try to add import OtherModule to .ghci file, then I get the error
module is member of hidden package fail-ghci
Perhaps you need to add ‘fail-ghci’ to the build-depends in your .cabal file.
But I can't add fail-ghci to the cabal file, because the library can't depend on itself!!
To reproduce. Create a simple cabal project. For example:
src
|- Module1.hs # include dummy function func1 :: Int
|- Module2.hs # include dummy function func2 :: Int
fail-ghci.cabal
.ghci
The content of fail-ghci.cabal is
name: fail-ghci
version: 0.1.0.0
license: BSD3
license-file: LICENSE
build-type: Simple
library
exposed-modules:
Module1
, Module2
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010
If you set .ghci as
:set prompt "> " -- Set this option to ensure .ghci is readed by cabal repl
It will work fine and will bring Module1.hs into scope, so func1 is available. But Module2.hs wont be in the scope, If I want to use it I'd need to execute import Module2 or equivalent.
Now, I'd like this to happen automatically when running cabal repl because my project has many modules. The obvious (to me) choice is to modify .ghci as
:set prompt "> " -- Set this option to ensure .ghci is readed by cabal repl
import Module2 -- Equivalent :m +Module2
But the ghci is unable to import the module, despite of that command woring correctly within ghci. What's the correct configuration to do this?
One workaround I like is to add a new module to your project, perhaps called DefaultRepl or similar, that imports everything you want. If you put this at the top of your exposed-modules list, then it will be loaded up when you run cabal repl and bring everything that it imports into scope.
I suppose the problem is simply that cabal executes the commands in your .ghci file before it loads the current project.
Indeed when I cabal repl in a similar project, the startup output is:
Build profile: -w ghc-9.0.2 -O1
In order, the following will be built (use -v for more details):
- fail-ghci-0.1.0.0 (first run)
Preprocessing library for fail-ghci-0.1.0.0..
GHCi, version 9.0.2: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /tmp/scratch/.ghci
[1 of 2] Compiling Module1 ( src/Module1.hs, interpreted )
[2 of 2] Compiling Module2 ( src/Module2.hs, interpreted )
Ok, two modules loaded.
ghci>
My guess would be that this is probably because cabal repl starts a ghci session (which loads the .ghci file during startup) and then uses its API to load the project environment. (I was able to confirm that a .ghci file can import modules from packages that are fully installed, rather than from a current project being cabal repld)
I don't know of any way to automatically execute commands after the project has been loaded. But you can have your .ghci file define a shortcut command that will import all the modules you want. That way you have at least have a single quick command to execute once you're in the ghci shell, instead of manually importing everything. Something like this:
:def loadall (const . pure) ":m + *Module1 *Module2"
-- or
:def loadall (const . pure) "import Module1 Module2"
-- or etc
Then you can just enter :loadall at the ghci prompt to get your modules imported.

Set up Haskell Project and run tests

I'm trying to set up a Haskell project (library) with tests that I can use to work through The Haskell Road to Logic, Maths, and Programming. There are three parts that I'd like to have:
The code that comes with the book, in a subdirectory
The code I write for exercises in the book; one file per chapter
The code I write for tests; one file per chapter
I have attempted a project setup here, but am getting the following cabal error:
Resolving dependencies...
Configuring haskell-road-0.1.0.0...
Building haskell-road-0.1.0.0...
Failed to install haskell-road-0.1.0.0
Build log ( /Users/stuart/.cabal/logs/haskell-road-0.1.0.0.log ):
cabal: Entering directory '.'
Configuring haskell-road-0.1.0.0...
Building haskell-road-0.1.0.0...
Preprocessing library haskell-road-0.1.0.0...
src/Chapter1.hs:1:1:
File name does not match module name:
Saw: ‘Main’
Expected: ‘Chapter1’
cabal: Leaving directory '.'
cabal: Error: some packages failed to install:
haskell-road-0.1.0.0 failed during the building phase. The exception was:
ExitFailure 1
I'd like to be able to run $ cabal test and have all of the tests run, and have the import paths work. Any help is appreciated. I think there are probably issues with the test structure, but I've had trouble finding definitive guides on the actual setup.
EDIT: More details
src/
Chapter1.hs
Book/
GS.hs
etc....
test/
Chapter1Test.hs
MainTestSuite.hs
TestHelper.hs
haskell-book.hs:
-- Initial haskell-road.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
-- The name of the package.
name: haskell-road
-- The package version. See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
-- https://wiki.haskell.org/Package_versioning_policy
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.0
-- A short (one-line) description of the package.
-- synopsis:
-- A longer description of the package.
-- description:
-- The license under which the package is released.
license: MIT
-- The file containing the license text.
license-file: LICENSE
-- The package author(s).
author: Stuart Terrett
-- An email address to which users can send suggestions, bug reports, and
-- patches.
maintainer: shterrett#gmail.com
-- A copyright notice.
-- copyright:
-- category:
build-type: Simple
-- Extra files to be distributed with the package, such as examples or a
-- README.
extra-source-files: ChangeLog.md
-- Constraint on the version of Cabal needed to build this package.
cabal-version: >=1.10
library
-- Modules exported by the library.
exposed-modules: Chapter1, Book.COR, Book.DB, Book.FAIS, Book.FCT, Book.GS, Book.Hierarchy, Book.IAR, Book.Nats, Book.POL, Book.Polynomials, Book.PowerSeries, Book.Query, Book.REL, Book.SetEq, Book.SetOrd, Book.STAL, Book.TAMO, Book.TUOLP, Book.WWN
-- Modules included in this library but not exported.
-- other-modules:
-- LANGUAGE extensions used by modules in this package.
other-extensions: FlexibleInstances
-- Other library packages from which modules are imported.
build-depends: base, random >=1.1 && <1.2, HUnit >=1.3 && <1.4
-- Directories containing source files.
hs-source-dirs: src
-- Base language which the package is written in.
default-language: Haskell2010
test-suite haskell-road-tests
type: exitcode-stdio-1.0
hs-source-dirs: tests, src
main-is: MainTestSuite.hs
build-depends: base,
HUnit,
QuickCheck,
test-framework,
test-framework-hunit,
test-framework-quickcheck2
MainTestSuite.hs
import Chapter1Test
exitProperly :: IO Counts -> IO ()
exitProperly m = do
counts <- m
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
allTests::[Test]
allTests = [Chapter1Test.itRuns]
main :: IO ()
main = exitProperly (runTestTT (TestList allTests))
Diff of all changes:
http://lpaste.net/5997592404872396800
Specific changes you need to make:
In Chapter1.hs make sure module Chapter1 appears before the import statement:
module Chapter1 where
import ...
In each of the Book modules you need to add the prefix Book. to
each of the module statements, e.g. in Book/COR.hs:
change: module COR
to: module Book.COR
Also, any import statement will also need the Book. prefix, i.e. in Book/STAL.hs:
change: import DB
to: import Book.DB
(It might easier just to leave the book's modules at the top-level of the module name space.)
To fix this compilation error:
src/Book/IAR.hs:131:7:
No instance for (Foldable t3) arising from a use of ‘foldr’
just add {-# LANGUAGE NoMonomorphismRestriction #-} to the top of Book/IAR.hs (it should be the very first line.)
To fix this compilation error:
src/Book/FAIS.hs:14:4: Parse error in pattern: n + 1
change: f (n+1) = True : f n to f n = True : f (n-1).
This is called an n+k pattern and more info about it (and why it has been deprecated) is available here: What are "n+k patterns" and why are they banned from Haskell 2010?
In the test-suite section you have:
hs-source-dirs: tests, src
To use the code in the src directory you tests should depend on the haskell-road library instead of compiling the source code. That is, use these lines in the test-suite section:
hs-source-dirs: tests
build-depends: base, haskell-road, HUnit, ...
File test/Chapter1Test.hs needs a module statement:
module Chapter1Test where
and also fix this import statement:
-import TestHelper.testCase
+import TestHelper (testCase)
File test/MainTestSuite.hs needs these import statements:
import System.Exit
import Test.HUnit
File test/testHelper.hs needs to be renamed to test/TestHelper.hs
and also needs this import statement:
import Test.HUnit
Cabal has
developing packages link and cabal file content structure is described there.
By looking the error message, it seems that your haskell library source file Chapter1 starts with module Main where. It should contain module Chapter1 where, as the error message says.
Libraries should not contain main while the test-executables should, which is why you state in the cabal file the test executables with main-is.
Hope this helps! (I didn't look at the github sources, just the error message.)

Cabal: Does not exists in Windows 8.1

Today I installed windows 8.1 and haskell on my laptop. I'm trying to build my own haskell library, but I got an error when I try to use cabal sdist. This is the error:
D:\Development\School\AFP\Assignments\Practice\Exercise\Project>cabal sdist
Distribution quality errors:
'license: NONE' is not a recognised license. The known licenses are: GPL,
GPL-2, GPL-3, LGPL, LGPL-2.1, LGPL-3, AGPL, AGPL-3, BSD2, BSD3, MIT, ISC,
MPL-2.0, Apache, Apache-2.0, PublicDomain, AllRightsReserved, OtherLicense
Distribution quality warnings:
No 'category' field.
No 'maintainer' field.
No 'synopsis' field.
A 'license-file' is not specified.
When distributing packages it is encouraged to specify source control
information in the .cabal file using one or more 'source-repository' sections.
See the Cabal user guide for details.
Note: the public hackage server would reject this package.
Warning: Cannot run preprocessors. Run 'configure' command first.
Building source dist for Project-0.1.0.0...
cabal: does not exist
Before I used the "cabal sdist" I used the following commands:
cabal init
cabal sandbox init
cabal install -j
Every command succeed, except for the cabal sdist. The cabal install only gives the following warning:
D:\Development\School\AFP\Assignments\Practice\Exercise\Project>cabal install -j
Resolving dependencies...
In order, the following will be installed:
Project-0.1.0.0 (reinstall)
Warning: Note that reinstalls are always dangerous. Continuing anyway...
Notice: installing into a sandbox located at
D:\Development\School\AFP\Assignments\Practice\Exercise\Project\.cabal-sandbox
Configuring Project-0.1.0.0...
Building Project-0.1.0.0...
Installed Project-0.1.0.0
This is my Project.cabal file:
-- Initial Project.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
-- Initial Project.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Project
version: 0.1.0.0
-- synopsis:
description: Education
license: NONE
-- license-file:
-- author:
-- maintainer:
-- copyright:
-- category:
build-type: Simple
extra-source-files: File6, File5, File4, File3, File2
cabal-version: >=1.10
library
exposed-modules: File1
-- other-modules:
-- other-extensions:
build-depends: base >=4.8 && <4.9, QuickCheck >=2.8 && <2.9
hs-source-dirs: src
default-language: Haskell2010
I tried google, but I can't find a good solution. I use the following versions:
Cabal version: 1.22.4.0
Haskell version: 7.10.2
If you need more information, please ask.
I don't know anything about sdist, but the problem is clear: you've specified 'NONE' as the license in your cabal file, but that is not allowed for the sdist option. configure, build, and init don't care about the specific license, but sdist apparently does.
For more info, I searched google for "cabal sdist" and found this.
This [cabal sdist] has the advantage that Cabal will do a bit more checking, and ensure that the tarball has the structure that HackageDB expects.
HackageDB probably expects a valid license, hence why "NONE" is not allowed.
The problem was that cabal could not find the files in extra-source-files. I thought I didn't had to add the extension of the haskell files, but this is required.
I also had another problem. The extra-source-files wasn't using the hs-source-dirs, so I had to explicitly write "src/" infront of a file.

haskell-mode with sandboxes

I have tried the following:
cabal sandbox init
Then making the following cabal file.
-- Initial hsource.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: hsource
version: 0.1.0.0
-- synopsis:
-- description:
-- license:
license-file: LICENSE
author: abc
maintainer: abc
-- copyright:
-- category:
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable hsource
main-is: main.hs
other-modules:
-- other-extensions:
build-depends: base >=4.7 && <4.8, csv
hs-source-dirs: src
default-language: Haskell2010
Now I install the CSV package with:
cabal install --only-dependencies
Now when I try import Text.CSV then then C-c C-l I get the following error:
Util/RandomTree.hs:7:8-15: Could not find module ‘Text.CSV’ …
Use -v to see a list of the files searched for.
Compilation failed.
So my question is if sandboxes are not supported in haskell-mode or am I missing some steps to get them to work?
Make sure you have:
(add-hook 'haskell-mode-hook 'interactive-haskell-mode)
in your emacs init file.
There are different GHCi process types supported by haskell-mode. You need one that uses cabal.
To find out what process type is currently applied, use M-x describe-variable and enter haskell-process-type.
I think the haskell-mode documentation is out of date; because, looking at the source code, the default is auto, which will use cabal-repl if it is able to locate a .cabal-sandbox directory. Otherwise, it will use ghci.
So, if your haskell-process-type is set to ghci or auto and it's unable to locate your cabal sandbox, you will see the error you posted. If it's currently set to ghci, change the haskell-process-type to cabal-repl by adding:
(custom-set-variables
'(haskell-process-type 'cabal-repl))
to your emacs init file and restarting the emacs process.
Also, you can always confirm that the problem is specific to emacs by opening a command line, navigating to the directory containing your .cabal file and entering cabal repl. If that works, then your cabal setup is fine.

Resources