Skip to content

Take working directory into account in preprocessor hs-boot hack #10991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions Cabal/src/Distribution/Simple/PreProcess.hs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ preprocessFile mbWorkDir searchLoc buildLoc forSDist baseFile verbosity builtinS
createDirectoryIfMissingVerbose verbosity True destDir
runPreProcessorWithHsBootHack
pp
(getSymbolicPath $ psrcLoc, getSymbolicPath $ psrcRelFile)
(getSymbolicPath $ buildLoc, srcStem <.> "hs")
(psrcLoc, getSymbolicPath $ psrcRelFile)
(buildLoc, srcStem <.> "hs")
where
i = interpretSymbolicPath mbWorkDir -- See Note [Symbolic paths] in Distribution.Utils.Path
buildAsSrcLoc :: SymbolicPath Pkg (Dir Source)
Expand All @@ -361,20 +361,25 @@ preprocessFile mbWorkDir searchLoc buildLoc forSDist baseFile verbosity builtinS
pp
(inBaseDir, inRelativeFile)
(outBaseDir, outRelativeFile) = do
-- Preprocessors are expected to take into account the working
-- directory, e.g. using runProgramCwd with a working directory
-- computed with mbWorkDirLBI.
-- Hence the use of 'getSymbolicPath' here.
runPreProcessor
pp
(inBaseDir, inRelativeFile)
(outBaseDir, outRelativeFile)
(getSymbolicPath $ inBaseDir, inRelativeFile)
(getSymbolicPath $ outBaseDir, outRelativeFile)
verbosity

exists <- doesFileExist inBoot
when exists $ copyFileVerbose verbosity inBoot outBoot
where
-- Here we interact directly with the file system, so we must
-- interpret symbolic paths with respect to the working directory.
let
inFile = normalise (i inBaseDir </> inRelativeFile)
outFile = normalise (i outBaseDir </> outRelativeFile)
inBoot = replaceExtension inFile "hs-boot"
outBoot = replaceExtension outFile "hs-boot"

inFile = normalise (inBaseDir </> inRelativeFile)
outFile = normalise (outBaseDir </> outRelativeFile)
exists <- doesFileExist inBoot
when exists $ copyFileVerbose verbosity inBoot outBoot

-- ------------------------------------------------------------

Expand Down
15 changes: 15 additions & 0 deletions cabal-testsuite/PackageTests/HsBootHack/dep/dep.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cabal-version: 3.0
name: dep
version: 0.1.0.0
license: BSD-3-Clause
author: sheaf
maintainer: sheaf
category: Testing
build-type: Simple
description: Testing the preprocessor hs-boot hack

library
hs-source-dirs: src
exposed-modules: DepA, DepB, DepC
build-depends: base
default-language: Haskell2010
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/HsBootHack/dep/src/DepA.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module DepA where

import {-# SOURCE #-} DepB

data A = A B
3 changes: 3 additions & 0 deletions cabal-testsuite/PackageTests/HsBootHack/dep/src/DepB.hs-boot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module DepB where

data B
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/HsBootHack/dep/src/DepB.hsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module DepB where

import DepA

data B = B A
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/HsBootHack/dep/src/DepC.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module DepC where

import DepB

data C = C B
6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/HsBootHack/setup.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Test.Cabal.Prelude

-- Test the hs-boot hack respects working directory
main = setupTest . recordMode DoNotRecord $ withDirectory "dep" $ do
void $ setup' "configure" []
void $ setup' "build" []
15 changes: 15 additions & 0 deletions changelog.d/pr-10991.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
synopsis: "Take --working-dir into account in runPreProcessorWithHsBootHack"
packages: [Cabal]
prs: 10991
issues: [11000]
---

The preprocessor hs-boot hack handles the situation in which a file to be
preprocessed is supplied alongside an hs-boot file, e.g. Foo.x and Foo.hs-boot
are given together.

This code now respects the --working-dir Cabal global argument. This fixes
build failures of the form:

attempting to use module `Foo` (Foo.hs-boot) which is not loaded
Loading