`cabal init -n` fails when `cabal` is not in `$PATH`
Describe the bug
cabal init -n fails when cabal is not in $PATH. seems like it wants to call itself?
cabal: readCreateProcessWithExitCode: posix_spawnp: does not exist (No such file or directory)
To Reproduce Steps to reproduce the behavior:
Ensure cabal is not in $PATH
$ mkdir mypackage
$ cd mypackage
$ /path/to/cabal init -n
Expected behavior
The package is initialized
System information
- NixOS
cabal3ae04882fde1b344150581eebca7291800ec3dd0GHC9.4.8
Additional context
Hello! A Haskell newbie here (so my findings could be totally wrong :smiling_face_with_tear:).
I've pinpointed the location of the bug to be at: https://github.com/haskell/cabal/blob/a6b99b83e2819b7d0c940971f08c88023dfad88e/cabal-install/src/Distribution/Client/Init/NonInteractive/Heuristics.hs#L66
A workaround to make this command (when cabal is not present in the PATH) work is to supply --cabal-version flag with a default Cabal spec version, i.e:
cloudyluna@suse:/tmp> cabal-3.10.3.0 init -n --cabal-version=3.0 apple
[Log] Guessing dependencies...
[Log] Using cabal specification: 3.0
[Warning] unknown license type, you must put a copy in LICENSE yourself.
[Log] Creating fresh file CHANGELOG.md...
[Log] Creating fresh directory ./app...
[Log] Creating fresh file app/Main.hs...
[Log] Creating fresh file apple.cabal...
[Warning] No synopsis given. You should edit the .cabal file and add one.
[Info] You may want to edit the .cabal file and add a Description field.
One way to solve this that comes to my mind is:
- We check for
readProcessWithExitCodefor successful exit code and continue on normally, else we go straight to returningdefaultCabalVersion.
Update 1
I found that readProcessWithExitCode will fail with the same error if the command it calls doesn't exist anyways. So we should check for the existence of cabal in PATH first beforehand and then we proceed with deciding whether we wanna skip directly to return defaultCabalVersion if it doesn't exist in PATH or continue on with the existing version heuristic normally otherwise. Perhaps simple findExecutable from directory package or reusing findProgramOnSearchPath could work here.
Do let me know if anyone have any better suggestions :slightly_smiling_face:
- We check for
readProcessWithExitCodefor successful exit code and continue on normally, else we go straight to returningdefaultCabalVersion.
Sounds good, but I'm also wondering why don't we just use defaultCabalVersion in all cases (cc @emilypi)
Yeah that sounds fine to me. Thanks for the issue!
I also have the same problem in GHC in version 9.4.8 in Ubuntu
Hey @shkrjnv ! This is likely independent of GHC version and is not hard to fix as discussed above, so we welcome contributions…
Moreover, we recently fixed a very similar bug when git isn't in the PATH. The solution was to catch exception from readProcessWithExitCode: https://github.com/haskell/cabal/commit/e7bc62be2ed8abbf80431f25a675c38eda786401#diff-7dd9364e5f55e8886924461848483dec67381a52ba9147b693abd9c9d9abd519R394