Skip to content

Commit 9e9b99b

Browse files
committed
Take CWD into account in preprocessor hs-boot hack
This commit fixes an oversight in runPreProcessorWithHsBootHack, which did not correctly take into account the working directory. This could lead to situations in which we failed to apply the hack. Recall that this hack is for situations when one has both an hs-boot file and a file in need of preprocessing, such as: Foo.y Foo.hs-boot or Bar.hsc Bar.hs-boot Failing to apply the hack essentially meant that GHC would not be able to see the hs-boot file, which caused build failures.
1 parent ffaa46b commit 9e9b99b

File tree

8 files changed

+69
-10
lines changed

8 files changed

+69
-10
lines changed

Cabal/src/Distribution/Simple/PreProcess.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ preprocessFile mbWorkDir searchLoc buildLoc forSDist baseFile verbosity builtinS
345345
createDirectoryIfMissingVerbose verbosity True destDir
346346
runPreProcessorWithHsBootHack
347347
pp
348-
(getSymbolicPath $ psrcLoc, getSymbolicPath $ psrcRelFile)
349-
(getSymbolicPath $ buildLoc, srcStem <.> "hs")
348+
(psrcLoc, getSymbolicPath $ psrcRelFile)
349+
(buildLoc, srcStem <.> "hs")
350350
where
351351
i = interpretSymbolicPath mbWorkDir -- See Note [Symbolic paths] in Distribution.Utils.Path
352352
buildAsSrcLoc :: SymbolicPath Pkg (Dir Source)
@@ -361,20 +361,25 @@ preprocessFile mbWorkDir searchLoc buildLoc forSDist baseFile verbosity builtinS
361361
pp
362362
(inBaseDir, inRelativeFile)
363363
(outBaseDir, outRelativeFile) = do
364+
-- Preprocessors are expected to take into account the working
365+
-- directory, e.g. using runProgramCwd with a working directory
366+
-- computed with mbWorkDirLBI.
367+
-- Hence the use of 'getSymbolicPath' here.
364368
runPreProcessor
365369
pp
366-
(inBaseDir, inRelativeFile)
367-
(outBaseDir, outRelativeFile)
370+
(getSymbolicPath $ inBaseDir, inRelativeFile)
371+
(getSymbolicPath $ outBaseDir, outRelativeFile)
368372
verbosity
369373

370-
exists <- doesFileExist inBoot
371-
when exists $ copyFileVerbose verbosity inBoot outBoot
372-
where
374+
-- Here we interact directly with the file system, so we must
375+
-- interpret symbolic paths with respect to the working directory.
376+
let
377+
inFile = normalise (i inBaseDir </> inRelativeFile)
378+
outFile = normalise (i outBaseDir </> outRelativeFile)
373379
inBoot = replaceExtension inFile "hs-boot"
374380
outBoot = replaceExtension outFile "hs-boot"
375-
376-
inFile = normalise (inBaseDir </> inRelativeFile)
377-
outFile = normalise (outBaseDir </> outRelativeFile)
381+
exists <- doesFileExist inBoot
382+
when exists $ copyFileVerbose verbosity inBoot outBoot
378383

379384
-- ------------------------------------------------------------
380385

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cabal-version: 3.0
2+
name: dep
3+
version: 0.1.0.0
4+
license: BSD-3-Clause
5+
author: sheaf
6+
maintainer: sheaf
7+
category: Testing
8+
build-type: Simple
9+
description: Testing the preprocessor hs-boot hack
10+
11+
library
12+
hs-source-dirs: src
13+
exposed-modules: DepA, DepB, DepC
14+
build-depends: base
15+
default-language: Haskell2010
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module DepA where
2+
3+
import {-# SOURCE #-} DepB
4+
5+
data A = A B
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module DepB where
2+
3+
data B
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module DepB where
2+
3+
import DepA
4+
5+
data B = B A
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module DepC where
2+
3+
import DepB
4+
5+
data C = C B
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Test.Cabal.Prelude
2+
3+
-- Test the hs-boot hack respects working directory
4+
main = setupTest . recordMode DoNotRecord $ withDirectory "dep" $ do
5+
void $ setup' "configure" []
6+
void $ setup' "build" []

changelog.d/pr-10991.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
synopsis: "Take --working-dir into account in runPreProcessorWithHsBootHack"
3+
packages: [Cabal]
4+
prs: 10991
5+
---
6+
7+
The preprocessor hs-boot hack handles the situation in which a file to be
8+
preprocessed is supplied alongside an hs-boot file, e.g. Foo.x and Foo.hs-boot
9+
are given together.
10+
11+
This code now respects the --working-dir Cabal global argument. This should
12+
fix build failures of the form:
13+
14+
attempting to use module `Foo` (Foo.hs-boot) which is not loaded
15+

0 commit comments

Comments
 (0)