From bdaf21b43b2306b9d67846c5217fe25fac0ebce5 Mon Sep 17 00:00:00 2001 From: Swini Lohani Date: Sat, 5 Dec 2026 00:04:44 +0530 Subject: [PATCH 1/2] Report a source directory that contains the search path separator GHC splits every -i flag on the search path separator, and there is no way to escape it. So with a build directory like --builddir=foo:bar, GHC could not find the modules that Cabal generates in it, such as Paths_pkg. Stop before running GHC with an error that names the directory instead. Fixes #12620. --- Cabal/src/Distribution/Simple/Errors.hs | 13 +++++++++++++ Cabal/src/Distribution/Simple/Program/GHC.hs | 12 +++++++++++- .../BuildDirSearchPathSeparator/cabal.project | 1 + .../BuildDirSearchPathSeparator/cabal.test.hs | 9 +++++++++ .../BuildDirSearchPathSeparator/pkg.cabal | 11 +++++++++++ .../BuildDirSearchPathSeparator/src/Lib.hs | 7 +++++++ changelog.d/12620.md | 12 ++++++++++++ 7 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.project create mode 100644 cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.test.hs create mode 100644 cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/pkg.cabal create mode 100644 cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/src/Lib.hs create mode 100644 changelog.d/12620.md diff --git a/Cabal/src/Distribution/Simple/Errors.hs b/Cabal/src/Distribution/Simple/Errors.hs index 47ad6b3df45..bbf3cca556c 100644 --- a/Cabal/src/Distribution/Simple/Errors.hs +++ b/Cabal/src/Distribution/Simple/Errors.hs @@ -35,6 +35,7 @@ import Distribution.System (OS) import Distribution.Types.MissingDependency (MissingDependency) import Distribution.Types.VersionRange.Internal () import Distribution.Version +import System.FilePath (searchPathSeparator) import Text.PrettyPrint data FailedDependency @@ -170,6 +171,7 @@ data CabalException | SetupHooksException SetupHooksException | MultiReplDoesNotSupportComplexReexportedModules PackageName ComponentName | StandaloneBytecodeNotSupportedYet + | SearchPathSeparatorInSourceDir FilePath deriving (Show) data WhatToFind @@ -320,6 +322,7 @@ exceptionCode e = case e of setupHooksExceptionCode err MultiReplDoesNotSupportComplexReexportedModules{} -> 9355 StandaloneBytecodeNotSupportedYet -> 9356 + SearchPathSeparatorInSourceDir{} -> 9357 versionRequirement :: VersionRange -> String versionRequirement range @@ -810,3 +813,13 @@ exceptionMessage e = case e of [ "The ecosystem doesn't support building packages with just bytecode libraries yet." , "If you have run into this error, please comment on issue #" ] + SearchPathSeparatorInSourceDir dir -> + "The directory " + ++ dir + ++ " contains '" + ++ [searchPathSeparator] + ++ "', which GHC takes as a separator between source directories," + ++ " so it cannot find the modules in it. If this directory is in the" + ++ " build directory, choose a build directory whose path has no '" + ++ [searchPathSeparator] + ++ "'." diff --git a/Cabal/src/Distribution/Simple/Program/GHC.hs b/Cabal/src/Distribution/Simple/Program/GHC.hs index 7a7909029fc..8d04b58a795 100644 --- a/Cabal/src/Distribution/Simple/Program/GHC.hs +++ b/Cabal/src/Distribution/Simple/Program/GHC.hs @@ -34,13 +34,14 @@ import Distribution.ModuleName import Distribution.PackageDescription import Distribution.Pretty import Distribution.Simple.Compiler +import Distribution.Simple.Errors import Distribution.Simple.Flag import Distribution.Simple.GHC.ImplInfo import Distribution.Simple.Program.Find (getExtraPathEnv) import Distribution.Simple.Program.ResponseFile import Distribution.Simple.Program.Run import Distribution.Simple.Program.Types -import Distribution.Simple.Utils (TempFileOptions, infoNoWrap) +import Distribution.Simple.Utils (TempFileOptions, dieWithException, infoNoWrap) import Distribution.System import Distribution.Types.ComponentId import Distribution.Types.ParStrat @@ -58,6 +59,7 @@ import Data.Monoid (All (..), Any (..), Endo (..)) import qualified Data.Set as Set import Data.Text (Text) import qualified Data.Text as Text +import System.FilePath (searchPathSeparator) import qualified System.Process as Process normaliseGhcArgs :: Maybe Version -> PackageDescription -> [String] -> [String] @@ -757,6 +759,14 @@ ghcInvocation -> GhcOptions -> IO ProgramInvocation ghcInvocation verbosity ghcProg comp platform mbWorkDir opts = do + -- GHC splits every @-i@ flag on the search path separator, and there is no + -- way to escape it. So a source directory that contains the separator, like + -- the ones in a build directory given as @--builddir=foo:bar@, would be + -- split in two, and GHC would not find the modules in it (#12620). + for_ (fromNubListR (ghcOptSourcePath opts)) $ \dir -> + when (searchPathSeparator `elem` getSymbolicPath dir) $ + dieWithException verbosity $ + SearchPathSeparatorInSourceDir (getSymbolicPath dir) -- NOTE: GHC is the only program whose path we modify with more values than -- the standard @extra-prog-path@, namely the folders of the executables in -- the components, see @componentGhcOptions@. diff --git a/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.project b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.test.hs b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.test.hs new file mode 100644 index 00000000000..4724770da61 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/cabal.test.hs @@ -0,0 +1,9 @@ +import System.FilePath (searchPathSeparator) +import Test.Cabal.Prelude + +-- GHC splits its source directories on the search path separator, so it +-- cannot find the modules in a build directory whose path contains one, such as +-- Paths_pkg. Cabal reports that instead of leaving GHC to fail (#12620). +main = cabalTest $ recordMode DoNotRecord $ do + r <- fails $ cabal' "v2-build" ["--builddir=dist" ++ [searchPathSeparator] ++ "sep"] + assertOutputContains "Cabal-9357" r diff --git a/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/pkg.cabal b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/pkg.cabal new file mode 100644 index 00000000000..c3969b3678b --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/pkg.cabal @@ -0,0 +1,11 @@ +cabal-version: 2.4 +name: pkg +version: 0.1.0.0 + +library + exposed-modules: Lib + other-modules: Paths_pkg + autogen-modules: Paths_pkg + hs-source-dirs: src + build-depends: base + default-language: Haskell2010 diff --git a/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/src/Lib.hs b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/src/Lib.hs new file mode 100644 index 00000000000..11188fd26a0 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildDirSearchPathSeparator/src/Lib.hs @@ -0,0 +1,7 @@ +module Lib (v) where + +import Data.Version (Version) +import Paths_pkg (version) + +v :: Version +v = version diff --git a/changelog.d/12620.md b/changelog.d/12620.md new file mode 100644 index 00000000000..1dfb2487af9 --- /dev/null +++ b/changelog.d/12620.md @@ -0,0 +1,12 @@ +--- +synopsis: Report a source directory that contains the search path separator +packages: [Cabal] +issues: 12620 +prs: 0000 +--- + +GHC splits every source directory given with `-i` on the search path separator +(`:` on Unix and `;` on Windows), and there is no way to escape it. So with a +build directory like `--builddir=foo:bar`, GHC could not find the modules that +Cabal generates there, such as `Paths_pkg`, and only said that they could not be +found. Cabal now stops with an error that names the directory and explains why. From 4252c709699dfa988a41001de81d0ac5c5a2e954 Mon Sep 17 00:00:00 2001 From: Swini Lohani Date: Sat, 5 Dec 2026 00:06:33 +0530 Subject: [PATCH 2/2] Add PR number to changelog entry --- changelog.d/12620.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/12620.md b/changelog.d/12620.md index 1dfb2487af9..941cd626f10 100644 --- a/changelog.d/12620.md +++ b/changelog.d/12620.md @@ -2,7 +2,7 @@ synopsis: Report a source directory that contains the search path separator packages: [Cabal] issues: 12620 -prs: 0000 +prs: 12697 --- GHC splits every source directory given with `-i` on the search path separator