Skip to content

Commit e49dc81

Browse files
authored
Merge pull request #10830 from haskell/wip/t10717
Set <pkgname_datadir> to an absolute path
2 parents 9d4b21a + 5a3c22e commit e49dc81

File tree

14 files changed

+161
-24
lines changed

14 files changed

+161
-24
lines changed

Cabal/src/Distribution/Simple/Bench.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ bench
5656
-- ^ flags sent to benchmark
5757
-> IO ()
5858
bench args pkg_descr lbi flags = do
59+
curDir <- LBI.absoluteWorkingDirLBI lbi
5960
let verbosity = fromFlag $ benchmarkVerbosity flags
6061
benchmarkNames = args
6162
pkgBenchmarks = PD.benchmarks pkg_descr
@@ -71,6 +72,7 @@ bench args pkg_descr lbi flags = do
7172
{ -- Include any build-tool-depends on build tools internal to the current package.
7273
LBI.withPrograms =
7374
addInternalBuildTools
75+
curDir
7476
pkg_descr
7577
lbi
7678
(benchmarkBuildInfo bm)

Cabal/src/Distribution/Simple/Build.hs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,15 @@ build_setupHooks
187187
-- dumped.
188188
dumpBuildInfo verbosity distPref (configDumpBuildInfo (configFlags lbi)) pkg_descr lbi flags
189189

190+
curDir <- absoluteWorkingDirLBI lbi
191+
190192
-- Now do the actual building
191193
(\f -> foldM_ f (installedPkgs lbi) componentsToBuild) $ \index target -> do
192194
let comp = targetComponent target
193195
clbi = targetCLBI target
194196
bi = componentBuildInfo comp
195197
-- Include any build-tool-depends on build tools internal to the current package.
196-
progs' = addInternalBuildTools pkg_descr lbi bi (withPrograms lbi)
198+
progs' = addInternalBuildTools curDir pkg_descr lbi bi (withPrograms lbi)
197199
lbi' =
198200
lbi
199201
{ withPrograms = progs'
@@ -375,17 +377,20 @@ repl_setupHooks
375377

376378
internalPackageDB <- createInternalPackageDB verbosity lbi distPref
377379

378-
let lbiForComponent comp lbi' =
379-
lbi'
380-
{ withPackageDB = withPackageDB lbi ++ [internalPackageDB]
381-
, withPrograms =
382-
-- Include any build-tool-depends on build tools internal to the current package.
383-
addInternalBuildTools
384-
pkg_descr
385-
lbi'
386-
(componentBuildInfo comp)
387-
(withPrograms lbi')
388-
}
380+
let lbiForComponent comp lbi' = do
381+
curDir <- absoluteWorkingDirLBI lbi'
382+
return $
383+
lbi'
384+
{ withPackageDB = withPackageDB lbi' ++ [internalPackageDB]
385+
, withPrograms =
386+
-- Include any build-tool-depends on build tools internal to the current package.
387+
addInternalBuildTools
388+
curDir
389+
pkg_descr
390+
lbi'
391+
(componentBuildInfo comp)
392+
(withPrograms lbi')
393+
}
389394
runPreBuildHooks :: LocalBuildInfo -> TargetInfo -> IO ()
390395
runPreBuildHooks lbi2 tgt =
391396
let inputs =
@@ -403,7 +408,7 @@ repl_setupHooks
403408
[ do
404409
let clbi = targetCLBI subtarget
405410
comp = targetComponent subtarget
406-
lbi' = lbiForComponent comp lbi
411+
lbi' <- lbiForComponent comp lbi
407412
preBuildComponent runPreBuildHooks verbosity lbi' subtarget
408413
buildComponent
409414
(mempty{buildCommonFlags = mempty{setupVerbosity = toFlag verbosity}})
@@ -420,7 +425,7 @@ repl_setupHooks
420425
-- REPL for target components
421426
let clbi = targetCLBI target
422427
comp = targetComponent target
423-
lbi' = lbiForComponent comp lbi
428+
lbi' <- lbiForComponent comp lbi
424429
preBuildComponent runPreBuildHooks verbosity lbi' target
425430
replComponent flags verbosity pkg_descr lbi' suffixHandlers comp clbi distPref
426431

@@ -925,12 +930,13 @@ createInternalPackageDB verbosity lbi distPref = do
925930
-- 'progOverrideEnv', so that any programs configured from now on will be
926931
-- able to invoke these build tools.
927932
addInternalBuildTools
928-
:: PackageDescription
933+
:: AbsolutePath (Dir Pkg)
934+
-> PackageDescription
929935
-> LocalBuildInfo
930936
-> BuildInfo
931937
-> ProgramDb
932938
-> ProgramDb
933-
addInternalBuildTools pkg lbi bi progs =
939+
addInternalBuildTools pwd pkg lbi bi progs =
934940
prependProgramSearchPathNoLogging
935941
internalToolPaths
936942
[pkgDataDirVar]
@@ -949,13 +955,11 @@ addInternalBuildTools pkg lbi bi progs =
949955
buildDir lbi
950956
</> makeRelativePathEx (toolName' </> toolName' <.> exeExtension (hostPlatform lbi))
951957
]
952-
mbWorkDir = mbWorkDirLBI lbi
953-
rawDataDir = dataDir pkg
954-
dataDirPath
955-
| null $ getSymbolicPath rawDataDir =
956-
interpretSymbolicPath mbWorkDir sameDirectory
957-
| otherwise =
958-
interpretSymbolicPath mbWorkDir rawDataDir
958+
959+
-- This is an absolute path, so if a process changes directory, it can still
960+
-- find the datadir (#10717)
961+
dataDirPath :: FilePath
962+
dataDirPath = interpretSymbolicPathAbsolute pwd (dataDir pkg)
959963

960964
-- TODO: build separate libs in separate dirs so that we can build
961965
-- multiple libs, e.g. for 'LibTest' library-style test suites

Cabal/src/Distribution/Simple/Haddock.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,13 @@ haddock_setupHooks
331331
createInternalPackageDB verbosity lbi (flag $ setupDistPref . haddockCommonFlags)
332332

333333
(\f -> foldM_ f (installedPkgs lbi) targets') $ \index target -> do
334+
curDir <- absoluteWorkingDirLBI lbi
334335
let
335336
component = targetComponent target
336337
clbi = targetCLBI target
337338
bi = componentBuildInfo component
338339
-- Include any build-tool-depends on build tools internal to the current package.
339-
progs' = addInternalBuildTools pkg_descr lbi bi (withPrograms lbi)
340+
progs' = addInternalBuildTools curDir pkg_descr lbi bi (withPrograms lbi)
340341
lbi' =
341342
lbi
342343
{ withPrograms = progs'

Cabal/src/Distribution/Simple/Test.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ test
7070
-- ^ flags sent to test
7171
-> IO ()
7272
test args pkg_descr lbi0 flags = do
73+
curDir <- LBI.absoluteWorkingDirLBI lbi0
7374
let common = testCommonFlags flags
7475
verbosity = fromFlag $ setupVerbosity common
7576
distPref = fromFlag $ setupDistPref common
@@ -96,6 +97,7 @@ test args pkg_descr lbi0 flags = do
9697
{ withPrograms =
9798
-- Include any build-tool-depends on build tools internal to the current package.
9899
addInternalBuildTools
100+
curDir
99101
pkg_descr
100102
lbi
101103
(PD.testBuildInfo suite)

cabal-install/src/Distribution/Client/Run.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import Distribution.Simple.Flag (fromFlag)
3535
import Distribution.Simple.LocalBuildInfo
3636
( ComponentName (..)
3737
, LocalBuildInfo (..)
38+
, absoluteWorkingDirLBI
3839
, buildDir
3940
, depLibraryPaths
4041
, interpretSymbolicPathLBI
@@ -142,6 +143,7 @@ splitRunArgs verbosity lbi args =
142143
-- | Run a given executable.
143144
run :: Verbosity -> LocalBuildInfo -> Executable -> [String] -> IO ()
144145
run verbosity lbi exe exeArgs = do
146+
curDir <- absoluteWorkingDirLBI lbi
145147
let distPref = fromFlag $ configDistPref $ configFlags lbi
146148
buildPref = buildDir lbi
147149
pkg_descr = localPkgDescr lbi
@@ -154,6 +156,7 @@ run verbosity lbi exe exeArgs = do
154156
, -- Include any build-tool-depends on build tools internal to the current package.
155157
withPrograms =
156158
addInternalBuildTools
159+
curDir
157160
pkg_descr
158161
lbi
159162
(buildInfo exe)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Setup configure
2+
Configuring datadir-test-0.1.0.0...
3+
# Setup build
4+
Preprocessing library for datadir-test-0.1.0.0...
5+
Building library for datadir-test-0.1.0.0...
6+
Preprocessing test suite 'datadir-test' for datadir-test-0.1.0.0...
7+
Building test suite 'datadir-test' for datadir-test-0.1.0.0...
8+
# Setup test
9+
Running 1 test suites...
10+
Test suite datadir-test: RUNNING...
11+
Test suite datadir-test: PASS
12+
Test suite logged to: cabal.cabal.dist/work/dist/test/datadir-test-0.1.0.0-datadir-test.log
13+
1 of 1 test suites (1 of 1 test cases) passed.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Setup configure
2+
Configuring datadir-test-0.1.0.0...
3+
# Setup build
4+
Preprocessing library for datadir-test-0.1.0.0...
5+
Building library for datadir-test-0.1.0.0...
6+
Preprocessing test suite 'datadir-test' for datadir-test-0.1.0.0...
7+
Building test suite 'datadir-test' for datadir-test-0.1.0.0...
8+
# Setup test
9+
Running 1 test suites...
10+
Test suite datadir-test: RUNNING...
11+
Test suite datadir-test: PASS
12+
Test suite logged to: cabal.dist/work/dist/test/datadir-test-0.1.0.0-datadir-test.log
13+
1 of 1 test suites (1 of 1 test cases) passed.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Test.Cabal.Prelude
2+
3+
main = setupAndCabalTest $ do
4+
setup_build ["--enable-tests"]
5+
setup "test" ["--show-details=streaming"]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cabal-version: 2.4
2+
name: datadir-test
3+
version: 0.1.0.0
4+
synopsis: Test for datadir environment variable
5+
license: BSD-3-Clause
6+
author: Cabal Test Suite
7+
maintainer: cabal-dev@haskell.org
8+
build-type: Simple
9+
10+
data-files:
11+
testdata/sample.txt
12+
13+
library
14+
exposed-modules: MyLib
15+
build-depends: base >=4.7 && <5
16+
other-modules: Paths_datadir_test
17+
hs-source-dirs: src
18+
default-language: Haskell2010
19+
20+
test-suite datadir-test
21+
type: exitcode-stdio-1.0
22+
main-is: DataDirTest.hs
23+
hs-source-dirs: test
24+
build-depends: base >=4.7 && <5,
25+
datadir-test,
26+
directory
27+
default-language: Haskell2010
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module MyLib (getDataFileName) where
2+
3+
import qualified Paths_datadir_test as Paths
4+
5+
getDataFileName :: FilePath -> IO FilePath
6+
getDataFileName = Paths.getDataFileName
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
2+
module Main where
3+
4+
import Control.Monad (when)
5+
import System.Directory (createDirectory, doesFileExist, getCurrentDirectory, setCurrentDirectory)
6+
import System.Environment (getEnv)
7+
import System.Exit (exitFailure, exitSuccess)
8+
import System.IO (hPutStrLn, stderr)
9+
import MyLib (getDataFileName)
10+
import Control.Exception
11+
12+
main :: IO ()
13+
main = do
14+
-- Print the datadir environment variable
15+
dataDirEnv <- getEnv "datadir_test_datadir"
16+
putStrLn $ "datadir_test_datadir: " ++ dataDirEnv
17+
18+
-- Get path to our test data file
19+
dataFilePath <- getDataFileName "testdata/sample.txt"
20+
putStrLn $ "Data file path: " ++ dataFilePath
21+
22+
-- Check that we can access the file
23+
fileExists <- doesFileExist dataFilePath
24+
putStrLn $ "File exists: " ++ show fileExists
25+
26+
-- Create a subdirectory and change into it
27+
currentDir <- getCurrentDirectory
28+
putStrLn $ "Current directory: " ++ currentDir
29+
createDirectory "subdir" `catch` \(_ :: SomeException) -> pure ()
30+
setCurrentDirectory "subdir"
31+
newDir <- getCurrentDirectory
32+
putStrLn $ "New directory: " ++ newDir
33+
34+
-- Try to access the data file again after changing directory
35+
dataFilePathAfterCd <- getDataFileName "testdata/sample.txt"
36+
putStrLn $ "Data file path after cd: " ++ dataFilePathAfterCd
37+
38+
fileExistsAfterCd <- doesFileExist dataFilePathAfterCd
39+
putStrLn $ "File exists after cd: " ++ show fileExistsAfterCd
40+
41+
-- Exit with error if we can't find the file
42+
when (not fileExistsAfterCd) $ do
43+
hPutStrLn stderr "ERROR: Could not find data file after changing directory!"
44+
hPutStrLn stderr $ "datadir_test_datadir was set to: " ++ dataDirEnv
45+
exitFailure
46+
47+
putStrLn "SUCCESS: Data file found correctly even after changing directory!"
48+
exitSuccess
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is test data for the datadir test.

changelog.d/pr-10830.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
synopsis: Set <pkgname>_datadir to an absolute path when running tests
3+
packages: [Cabal]
4+
prs: 10828
5+
issues: [10717]
6+
---
7+
8+
Fix a regression where `<pkgname>_datadir` was set to a relative path. This
9+
caused issues when running testsuites which changed the working directory and
10+
accessed datafiles.

0 commit comments

Comments
 (0)