Skip to content

Commit ef8b2ec

Browse files
athasmergify[bot]
authored andcommitted
Ignore ~/.cabal if $XDG_CONFIG_HOME/cabal/config exists. (#8877)
* Ignore ~/.cabal if $XDG_CONFIG_HOME/cabal/config exists. * Also document this. * Slightly fewer warnings. * Use verbosity flag. * Better text. * Oops (cherry picked from commit 784d13b)
1 parent 5a83705 commit ef8b2ec

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

Diff for: cabal-install/src/Distribution/Client/Config.hs

+29-10
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ import Text.PrettyPrint
133133
import Text.PrettyPrint.HughesPJ
134134
( text, Doc )
135135
import System.Directory
136-
( createDirectoryIfMissing, getHomeDirectory, getXdgDirectory, XdgDirectory(XdgCache, XdgConfig, XdgState), renameFile, getAppUserDataDirectory, doesDirectoryExist )
136+
( createDirectoryIfMissing, getHomeDirectory, getXdgDirectory, XdgDirectory(XdgCache, XdgConfig, XdgState), renameFile, getAppUserDataDirectory, doesDirectoryExist, doesFileExist )
137137
import Network.URI
138138
( URI(..), URIAuth(..), parseURI )
139139
import System.FilePath
@@ -590,12 +590,28 @@ initialSavedConfig = do
590590
}
591591
}
592592

593-
-- | If @CABAL\_DIR@ is set or @~/.cabal@ exists, return that
594-
-- directory. Otherwise returns Nothing. If this function returns
595-
-- Nothing, then it implies that we are not using a single directory
596-
-- for everything, but instead use XDG paths. Fundamentally, this
597-
-- function is used to implement transparent backwards compatibility
598-
-- with pre-XDG versions of cabal-install.
593+
-- | Issue a warning if both @$XDG_CONFIG_HOME/cabal/config@ and
594+
-- @~/.cabal@ exists.
595+
warnOnTwoConfigs :: Verbosity -> IO ()
596+
warnOnTwoConfigs verbosity = do
597+
defaultDir <- getAppUserDataDirectory "cabal"
598+
dotCabalExists <- doesDirectoryExist defaultDir
599+
xdgCfg <- getXdgDirectory XdgConfig ("cabal" </> "config")
600+
xdgCfgExists <- doesFileExist xdgCfg
601+
when (dotCabalExists && xdgCfgExists) $
602+
warn verbosity $
603+
"Both " <> defaultDir <>
604+
" and " <> xdgCfg <>
605+
" exist - ignoring the former.\n" <>
606+
"It is advisable to remove one of them. In that case, we will use the remaining one by default (unless '$CABAL_DIR' is explicitly set)."
607+
608+
-- | If @CABAL\_DIR@ is set, return @Just@ its value. Otherwise, if
609+
-- @~/.cabal@ exists and @$XDG_CONFIG_HOME/cabal/config@ does not
610+
-- exist, return @Just "~/.cabal"@. Otherwise, return @Nothing@. If
611+
-- this function returns Nothing, then it implies that we are not
612+
-- using a single directory for everything, but instead use XDG paths.
613+
-- Fundamentally, this function is used to implement transparent
614+
-- backwards compatibility with pre-XDG versions of cabal-install.
599615
maybeGetCabalDir :: IO (Maybe FilePath)
600616
maybeGetCabalDir = do
601617
mDir <- lookupEnv "CABAL_DIR"
@@ -604,9 +620,11 @@ maybeGetCabalDir = do
604620
Nothing -> do
605621
defaultDir <- getAppUserDataDirectory "cabal"
606622
dotCabalExists <- doesDirectoryExist defaultDir
607-
return $ if dotCabalExists
608-
then Just defaultDir
609-
else Nothing
623+
xdgCfg <- getXdgDirectory XdgConfig ("cabal" </> "config")
624+
xdgCfgExists <- doesFileExist xdgCfg
625+
if dotCabalExists && not xdgCfgExists
626+
then return $ Just defaultDir
627+
else return Nothing
610628

611629
-- | The default behaviour of cabal-install is to use the XDG
612630
-- directory standard. However, if @CABAL_DIR@ is set, we instead use
@@ -759,6 +777,7 @@ defaultHackageRemoteRepoKeyThreshold = 3
759777
--
760778
loadConfig :: Verbosity -> Flag FilePath -> IO SavedConfig
761779
loadConfig verbosity configFileFlag = do
780+
warnOnTwoConfigs verbosity
762781
config <- loadRawConfig verbosity configFileFlag
763782
extendToEffectiveConfig config
764783

Diff for: changelog.d/issue-8577

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
synopsis: Existence of $XDG_CONFIG_HOME/cabal/config now overrides existence of $HOME/.cabal
2+
packages: cabal-install
3+
issues: #8577
4+
5+
description: {
6+
7+
To avoid pre-XDG backwards compatibility from triggering due to other
8+
tools accidentally creating a $HOME/.cabal directory, the presence of
9+
$XDG_CONFIG_HOME/cabal/config now disables pre-XDG backwards
10+
compatibility. Presumably $XDG_CONFIG_HOME/cabal/config will never be
11+
created by accident.
12+
13+
}

Diff for: doc/config.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ Various environment variables affect ``cabal-install``.
6161
.. note::
6262

6363
For backwards compatibility, if the directory ``~/.cabal`` on
64-
Unix or ``%APPDATA%\cabal`` on Windows exist and ``CABAL_DIR``
65-
is unset, ``cabal-install`` will behave as if ``CABAL_DIR`` was
66-
set to point at this directory.
64+
Unix or ``%APPDATA%\cabal`` on Windows exists, and
65+
``$XDG_CONFIG_HOME/cabal/config`` does not exist, and
66+
``CABAL_DIR`` is unset, ``cabal-install`` will behave as if
67+
``CABAL_DIR`` was set to point at this directory.
6768

6869
``CABAL_BUILDDIR``
6970

0 commit comments

Comments
 (0)