From 309b14821f452474a54b6ed7b51b19adc1de231b Mon Sep 17 00:00:00 2001 From: Thomas Tuegel Date: Fri, 3 Nov 2017 14:42:27 -0500 Subject: [PATCH] readGlobalConfig: Also consider cabal.config files in project root If there is a cabal.config.local file in the project root directory, readGlobalConfig reads it after ~/.cabal/config. This is intended to allow the user to specify site-specific global options on a per-project basis. Global options cannot be specified in cabal.project.local, which applies options only to packages in the project. See also: #3883 #4646 --- .gitignore | 1 + Cabal/doc/nix-local-build.rst | 12 ++++++-- .../Distribution/Client/CmdInstall.hs | 2 +- .../Distribution/Client/ProjectConfig.hs | 30 +++++++++++++------ cabal-install/changelog | 2 ++ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index a9213ae9c7f..0afbc7c0e1c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .cabal-sandbox/ cabal.sandbox.config cabal.project.local +cabal.config.local .ghc.environment.* cabal-dev/ .hpc/ diff --git a/Cabal/doc/nix-local-build.rst b/Cabal/doc/nix-local-build.rst index 4488118e268..57eca67ac06 100644 --- a/Cabal/doc/nix-local-build.rst +++ b/Cabal/doc/nix-local-build.rst @@ -481,11 +481,17 @@ following sources (later entries override earlier ones): 1. ``~/.cabal/config`` (the user-wide global configuration) -2. ``cabal.project`` (the project configuratoin) +2. ``cabal.config.local`` (the project-wide global configuration) -3. ``cabal.project.freeze`` (the output of ``cabal new-freeze``) +3. ``cabal.project`` (the project configuration) -4. ``cabal.project.local`` (the output of ``cabal new-configure``) +4. ``cabal.project.freeze`` (the output of ``cabal new-freeze``) + +5. ``cabal.project.local`` (the output of ``cabal new-configure``) + +The project configuration files ``cabal.project`` and ``cabal.project.local`` +specify options for project packages only, while the global configuration files +``~/.cabal/config`` and ``cabal.config.local`` affect *all* packages. Specifying the local packages diff --git a/cabal-install/Distribution/Client/CmdInstall.hs b/cabal-install/Distribution/Client/CmdInstall.hs index 197124b4422..36ed427ecab 100644 --- a/cabal-install/Distribution/Client/CmdInstall.hs +++ b/cabal-install/Distribution/Client/CmdInstall.hs @@ -242,7 +242,7 @@ establishDummyProjectBaseContext verbosity cliConfig tmpDir localPackages = do createDirectoryIfMissingVerbose verbosity True $ distProjectCacheDirectory distDirLayout globalConfig <- runRebuild "" - $ readGlobalConfig verbosity + $ readGlobalConfig verbosity distDirLayout $ projectConfigConfigFile $ projectConfigShared cliConfig let projectConfig = globalConfig <> cliConfig diff --git a/cabal-install/Distribution/Client/ProjectConfig.hs b/cabal-install/Distribution/Client/ProjectConfig.hs index ca67237157f..8598515b335 100644 --- a/cabal-install/Distribution/Client/ProjectConfig.hs +++ b/cabal-install/Distribution/Client/ProjectConfig.hs @@ -66,7 +66,7 @@ import Distribution.Client.GlobalFlags import Distribution.Client.BuildReports.Types ( ReportLevel(..) ) import Distribution.Client.Config - ( loadConfig, getConfigFilePath ) + ( getConfigFilePath, loadConfig, loadExactConfig ) import Distribution.Solver.Types.SourcePackage import Distribution.Solver.Types.Settings @@ -413,7 +413,7 @@ renderBadProjectRoot (BadProjectRootExplicitFile projectFile) = -- readProjectConfig :: Verbosity -> Flag FilePath -> DistDirLayout -> Rebuild ProjectConfig readProjectConfig verbosity configFileFlag distDirLayout = do - global <- readGlobalConfig verbosity configFileFlag + global <- readGlobalConfig verbosity distDirLayout configFileFlag local <- readProjectLocalConfig verbosity distDirLayout freeze <- readProjectLocalFreezeConfig verbosity distDirLayout extra <- readProjectLocalExtraConfig verbosity distDirLayout @@ -542,13 +542,25 @@ writeProjectConfigFile file = writeFile file . showProjectConfig --- | Read the user's @~/.cabal/config@ file. --- -readGlobalConfig :: Verbosity -> Flag FilePath -> Rebuild ProjectConfig -readGlobalConfig verbosity configFileFlag = do - config <- liftIO (loadConfig verbosity configFileFlag) - configFile <- liftIO (getConfigFilePath configFileFlag) - monitorFiles [monitorFileHashed configFile] +-- | Read the user's @~/.cabal/config@ file and any @cabal.config.local@ file in the +-- project root directory. +-- +readGlobalConfig + :: Verbosity + -> DistDirLayout + -> Flag FilePath + -> Rebuild ProjectConfig +readGlobalConfig verbosity layout configFileFlag = do + userConfig <- liftIO (loadConfig verbosity configFileFlag) + userConfigFile <- liftIO (getConfigFilePath configFileFlag) + let localConfigFile = distProjectRootDirectory layout "cabal.config.local" + localConfig <- liftIO (loadExactConfig verbosity localConfigFile) + monitorFiles + [ + monitorFileHashed userConfigFile, + monitorFileHashed localConfigFile + ] + let config = userConfig <> fromMaybe mempty localConfig return (convertLegacyGlobalConfig config) reportParseResult :: Verbosity -> String -> FilePath -> ParseResult a -> IO a diff --git a/cabal-install/changelog b/cabal-install/changelog index 274d0491df8..f512b13f975 100644 --- a/cabal-install/changelog +++ b/cabal-install/changelog @@ -35,6 +35,8 @@ stay within the load command size limits of macOSs mach-o linker. * Use [lfxtb] letters to differentiate component kind instead of opaque "c" in dist-dir layout. + * New config file 'cabal.config.local' to specify project-wide + global options for project commands. 2.0.0.1 Mikhail Glushenkov October 2017 * Support for GHC's numeric -g debug levels (#4673).