haskell-mode with sandboxes - haskell

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.

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.

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

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

How do I add the "containers" package to my .cabal file (without getting overwritten by stack at compile time)?

I am working on the "roman-numerals" task from the exercism Haskell track and followed their instructions to installing stack. I am working on a Fedora 24 box.
As long as I was working with Haskell modules from base, I didn't have a problem. Now I am trying to import the Data.Map module. It works fine using the ghci command line:
$ ghci
GHCi, version 7.8.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Data.Map
Prelude Data.Map>
However, when I try to import it from inside my src file with the command:
import qualified Data.Map as M (foldlWithKey, fromList)
I am running into problems when I try to run the test:
$ stack test
roman-numerals-0.0.0: build (lib + test)
Preprocessing library roman-numerals-0.0.0...
[2 of 2] Compiling Roman (...)
(...) /roman-numerals/src/Roman.hs:3:1: error:
Failed to load interface for ‘Data.Map’
It is a member of the hidden package ‘containers-0.5.7.1’.
Perhaps you need to add ‘containers’ to the build-depends in your .cabal file.
Use -v to see a list of the files searched for.
Progress: 1/2
(...)
I googled the problem and found a straightforward solution at the Cabal FAQ at haskell.org:
What you need to do is to add containers to the build-depends in your .cabal file.
I am assuming they mean the file roman-numerals.cabal that is in my working directory. The contents are:
-- This file has been generated from package.yaml by hpack version 0.14.0.
--
-- see: https://github.com/sol/hpack
name: roman-numerals
version: 0.0.0
build-type: Simple
cabal-version: >= 1.10
library
hs-source-dirs:
src
build-depends:
base
exposed-modules:
Roman
other-modules:
Paths_roman_numerals
default-language: Haskell2010
test-suite test
type: exitcode-stdio-1.0
main-is: Tests.hs
hs-source-dirs:
test
build-depends:
base
, roman-numerals
, hspec
default-language: Haskell2010
I tried to add "containers" to the build-depends in either and both the "library" and "test-suite" sections, but when I run
$ stack test
the error persists, and the .cabal file is reverted to the same contents shown above.
Any pointers? Much appreciated!
This is hinting at the problem:
-- This file has been generated from package.yaml by hpack version 0.14.0.
--
-- see: https://github.com/sol/hpack
hpack is an alternative, YAML-based specification format for Haskell packages which can be used instead of the traditional cabal format. The hpack program can then be used to convert a specification from the hpack format to the cabal format to be able to integrate with the rest of the Haskell toolchain.
Some basic support for hpack was added to stack some time ago. It checks for a file called package.yaml in the current directory, which is the standard name for hpack format package specifications, and if it exists, it runs hpack to convert it to a cabal file and then proceeds building as normal. This is what's trampling over your .cabal file.
To solve this, either:
Modify package.yaml instead of roman-numerals.cabal to achieve the same effect.
Delete package.yaml and continue working directly with roman-numerals.cabal.
The syntax for adding dependencies in the hpack format is:
dependencies:
- base
- containers

Dependency issues running "cabal test" for Haskell

I'm running my first "cabal test" for Haskell, but I get the error:
Package has never been configured. Configuring with default flags. If this
fails, please run configure manually.
Resolving dependencies...
Configuring sample-0.1.0.0...
cabal: At least the following dependencies are missing:
base ==4.7.*
sampel.cabal:
-- Initial sample.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: sample
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
executable SampleTest
main-is: SampleTest.hs
build-depends: base >= 4.7 && <4.8, HUnit >=1.2 && <1.3
hs-source-dirs: test, src
default-language: Haskell2010
Any help appreciated.
Versions of GHC come bundled with versions of base. GHC 7.10.2 uses base 4.8.1.0.
Your cabal must be using a slightly out of date template....
You should either change the range of acceptable base version (as you did in your comment above), or use a different version of GHC.

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.

Resources