Skip to content
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

Deprecate 'stack path --global-stack-root', add '--stack-root' options – #1148 #1983

Merged
merged 2 commits into from
Apr 12, 2016
Merged
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
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ Behavior changes:
`-rtsopts` was enabled, stack would process `+RTS` options even when intended
for some other program, such as when used with `stack exec -- prog +RTS`.
See [#2022](https://github.com/commercialhaskell/stack/issues/2022).
* For consistency with the `$STACK_ROOT` environment variable, the
`stack path --global-stack-root` flag and the `global-stack-root` field
in the output of `stack path` are being deprecated and replaced with the
`stack-root` flag and output field.
Additionally, the stack root can now be specified via the
`--stack-root` command-line flag. See
[#1148](https://github.com/commercialhaskell/stack/issues/1148).

Other enhancements:

Expand Down
6 changes: 5 additions & 1 deletion doc/GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,7 @@ useful.
```
michael@d30748af6d3d:~/wai$ stack path
global-stack-root: /home/michael/.stack
stack-root: /home/michael/.stack
project-root: /home/michael/wai
config-location: /home/michael/wai/stack.yaml
bin-path: /home/michael/.stack/snapshots/x86_64-linux/lts-2.17/7.8.4/bin:/home/michael/.stack/programs/x86_64-linux/ghc-7.8.4/bin:/home/michael/.stack/programs/x86_64-linux/ghc-7.10.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Expand All @@ -1480,11 +1481,14 @@ extra-include-dirs:
extra-library-dirs:
snapshot-pkg-db: /home/michael/.stack/snapshots/x86_64-linux/lts-2.17/7.8.4/pkgdb
local-pkg-db: /home/michael/wai/.stack-work/install/x86_64-linux/lts-2.17/7.8.4/pkgdb
global-pkg-db: /home/michael/.stack/programs/x86_64-linux/ghc-7.8.4/lib/ghc-7.8.4/package.conf.d
ghc-package-path: /home/michael/wai/.stack-work/install/x86_64-linux/lts-2.17/7.8.4/pkgdb:/home/michael/.stack/snapshots/x86_64-linux/lts-2.17/7.8.4/pkgdb:/home/michael/.stack/programs/x86_64-linux/ghc-7.8.4/lib/ghc-7.8.4/package.conf.d
snapshot-install-root: /home/michael/.stack/snapshots/x86_64-linux/lts-2.17/7.8.4
local-install-root: /home/michael/wai/.stack-work/install/x86_64-linux/lts-2.17/7.8.4
snapshot-doc-root: /home/michael/.stack/snapshots/x86_64-linux/lts-2.17/7.8.4/doc
local-doc-root: /home/michael/wai/.stack-work/install/x86_64-linux/lts-2.17/7.8.4/doc
dist-dir: .stack-work/dist/x86_64-linux/Cabal-1.18.1.5
local-hpc-root: /home/michael/wai/.stack-work/install/x86_64-linux/lts-2.17/7.8.4/hpc
```

In addition, `stack path` accepts command line arguments to state which of
Expand All @@ -1504,7 +1508,7 @@ what needs to be removed:

1. The stack executable itself
2. The stack root, e.g. `$HOME/.stack` on non-Windows systems.
* See `stack path --global-stack-root`
* See `stack path --stack-root`
* On Windows, you will also need to delete `stack path --programs-paths`
3. Any local `.stack-work` directories inside a project

Expand Down
21 changes: 13 additions & 8 deletions src/Stack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ loadConfig :: (MonadLogger m,MonadIO m,MonadMask m,MonadThrow m,MonadBaseControl
-- ^ Override resolver
-> m (LoadConfig m)
loadConfig configArgs mstackYaml mresolver = do
(stackRoot, userOwnsStackRoot) <- determineStackRootAndOwnership
(stackRoot, userOwnsStackRoot) <- determineStackRootAndOwnership configArgs
userConfigPath <- getDefaultUserConfigPath stackRoot
extraConfigs0 <- getExtraConfigs userConfigPath >>= mapM loadYaml
let extraConfigs =
Expand Down Expand Up @@ -645,19 +645,24 @@ resolvePackageLocation menv projRoot (PLRemote url remotePackageType) = do
-- On Windows, the second value is always 'True'.
determineStackRootAndOwnership
:: (MonadIO m, MonadCatch m)
=> m (Path Abs Dir, Bool)
determineStackRootAndOwnership = do
=> ConfigMonoid
-- ^ Parsed command-line arguments
-> m (Path Abs Dir, Bool)
determineStackRootAndOwnership clArgs = do
stackRoot <- do
mstackRoot <- liftIO $ lookupEnv stackRootEnvVar
case mstackRoot of
Nothing -> getAppUserDataDir stackProgName
Just x -> parseAbsDir x
case configMonoidStackRoot clArgs of
Just x -> return x
Nothing -> do
mstackRoot <- liftIO $ lookupEnv stackRootEnvVar
case mstackRoot of
Nothing -> getAppUserDataDir stackProgName
Just x -> parseAbsDir x

(existingStackRootOrParentDir, userOwnsIt) <- do
mdirAndOwnership <- findInParents getDirAndOwnership stackRoot
case mdirAndOwnership of
Just x -> return x
Nothing -> throwM (BadStackRootEnvVar stackRoot)
Nothing -> throwM (BadStackRoot stackRoot)

when (existingStackRootOrParentDir /= stackRoot) $
if userOwnsIt
Expand Down
15 changes: 15 additions & 0 deletions src/Stack/Constants.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Stack.Constants
,rawGithubUrl
,stackDotYaml
,stackRootEnvVar
,stackRootOptionName
,deprecatedStackRootOptionName
,inContainerEnvVar
,configCacheFile
,configCabalMod
Expand Down Expand Up @@ -220,6 +222,19 @@ stackDotYaml = $(mkRelFile "stack.yaml")
stackRootEnvVar :: String
stackRootEnvVar = "STACK_ROOT"

-- | Option name for the global stack root.
stackRootOptionName :: String
stackRootOptionName = "stack-root"

-- | Deprecated option name for the global stack root.
--
-- Deprecated since stack-1.0.5.
--
-- TODO: Remove occurences of this variable and use 'stackRootOptionName' only
-- after an appropriate deprecation period.
deprecatedStackRootOptionName :: String
deprecatedStackRootOptionName = "global-stack-root"

-- | Environment variable used to indicate stack is running in container.
inContainerEnvVar :: String
inContainerEnvVar = stackProgNameUpper ++ "_IN_CONTAINER"
Expand Down
3 changes: 0 additions & 3 deletions src/Stack/Constants.hs-boot

This file was deleted.

28 changes: 23 additions & 5 deletions src/Stack/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Stack.Options
) where

import Control.Monad.Logger (LogLevel (..))
import Data.Char (isSpace, toLower)
import Data.Char (isSpace, toLower, toUpper)
import Data.List (intercalate)
import Data.List.Split (splitOn)
import qualified Data.Map as Map
Expand All @@ -45,11 +45,12 @@ import Options.Applicative
import Options.Applicative.Args
import Options.Applicative.Builder.Extra
import Options.Applicative.Types (fromM, oneM, readerAsk)
import Path
import Stack.Build (splitObjsWarning)
import Stack.Clean (CleanOpts (..))
import Stack.Config (packagesParser)
import Stack.ConfigCmd
import Stack.Constants (stackProgName)
import Stack.Constants
import Stack.Coverage (HpcReportOpts (..))
import Stack.Docker
import qualified Stack.Docker as Docker
Expand Down Expand Up @@ -201,8 +202,9 @@ cleanOptsParser = CleanShallow <$> packages <|> doFullClean
-- | Command-line arguments parser for configuration.
configOptsParser :: GlobalOptsContext -> Parser ConfigMonoid
configOptsParser hide0 =
(\workDir buildOpts dockerOpts nixOpts systemGHC installGHC arch os ghcVariant jobs includes libs skipGHCCheck skipMsys localBin modifyCodePage allowDifferentUser -> mempty
{ configMonoidWorkDir = workDir
(\stackRoot workDir buildOpts dockerOpts nixOpts systemGHC installGHC arch os ghcVariant jobs includes libs skipGHCCheck skipMsys localBin modifyCodePage allowDifferentUser -> mempty
{ configMonoidStackRoot = stackRoot
, configMonoidWorkDir = workDir
, configMonoidBuildOpts = buildOpts
, configMonoidDockerOpts = dockerOpts
, configMonoidNixOpts = nixOpts
Expand All @@ -220,7 +222,14 @@ configOptsParser hide0 =
, configMonoidModifyCodePage = modifyCodePage
, configMonoidAllowDifferentUser = allowDifferentUser
})
<$> optional (strOption
<$> optional (option readAbsDir
( long stackRootOptionName
<> metavar (map toUpper stackRootOptionName)
<> help ("Absolute path to the global stack root directory " ++
"(Overrides any STACK_ROOT environment variable)")
<> hide
))
<*> optional (strOption
( long "work-dir"
<> metavar "WORK-DIR"
<> help "Override work directory (default: .stack-work)"
Expand Down Expand Up @@ -294,6 +303,15 @@ configOptsParser hide0 =
hide
where hide = hideMods (hide0 /= OuterGlobalOpts)

readAbsDir :: ReadM (Path Abs Dir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can buy that this is needed for good errors. However, parseAbsDir directly will also throw a not-so-bad looking error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadM sadly isn't an instance of MonadThrow, which parseAbsDir requires.

That might change with pcapriotti/optparse-applicative#196 though.

readAbsDir = do
s <- readerAsk
case parseAbsDir s of
Just p -> return p
Nothing ->
readerError
("Failed to parse absolute path to directory: '" ++ s ++ "'")

buildOptsMonoidParser :: Bool -> Parser BuildOptsMonoid
buildOptsMonoidParser hide0 =
transform <$> trace <*> profile <*> options
Expand Down
27 changes: 14 additions & 13 deletions src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ import Distribution.Version (anyVersion)
import Network.HTTP.Client (parseUrl)
import Path
import qualified Paths_stack as Meta
import {-# SOURCE #-} Stack.Constants (stackRootEnvVar)
import Stack.Types.BuildPlan (SnapName, renderSnapName, parseSnapName)
import Stack.Types.Compiler
import Stack.Types.Docker
Expand Down Expand Up @@ -738,7 +737,9 @@ instance HasBuildConfig BuildConfig where
-- Configurations may be "cascaded" using mappend (left-biased).
data ConfigMonoid =
ConfigMonoid
{ configMonoidWorkDir :: !(Maybe FilePath)
{ configMonoidStackRoot :: !(Maybe (Path Abs Dir))
-- ^ See: 'configStackRoot'
, configMonoidWorkDir :: !(Maybe FilePath)
-- ^ See: 'configWorkDir'.
, configMonoidBuildOpts :: !BuildOptsMonoid
-- ^ build options.
Expand Down Expand Up @@ -817,7 +818,8 @@ data ConfigMonoid =

instance Monoid ConfigMonoid where
mempty = ConfigMonoid
{ configMonoidWorkDir = Nothing
{ configMonoidStackRoot = Nothing
, configMonoidWorkDir = Nothing
, configMonoidBuildOpts = mempty
, configMonoidDockerOpts = mempty
, configMonoidNixOpts = mempty
Expand Down Expand Up @@ -855,7 +857,8 @@ instance Monoid ConfigMonoid where
, configMonoidAllowDifferentUser = Nothing
}
mappend l r = ConfigMonoid
{ configMonoidWorkDir = configMonoidWorkDir l <|> configMonoidWorkDir r
{ configMonoidStackRoot = configMonoidStackRoot l <|> configMonoidStackRoot r
, configMonoidWorkDir = configMonoidWorkDir l <|> configMonoidWorkDir r
, configMonoidBuildOpts = configMonoidBuildOpts l <> configMonoidBuildOpts r
, configMonoidDockerOpts = configMonoidDockerOpts l <> configMonoidDockerOpts r
, configMonoidNixOpts = configMonoidNixOpts l <> configMonoidNixOpts r
Expand Down Expand Up @@ -902,6 +905,8 @@ instance FromJSON (WithJSONWarnings ConfigMonoid) where
-- warnings for missing fields.
parseConfigMonoidJSON :: Object -> WarningParser ConfigMonoid
parseConfigMonoidJSON obj = do
-- Parsing 'stackRoot' from 'stackRoot'/config.yaml would be nonsensical
let configMonoidStackRoot = Nothing
configMonoidWorkDir <- obj ..:? configMonoidWorkDirName
configMonoidBuildOpts <- jsonSubWarnings (obj ..:? configMonoidBuildOptsName ..!= mempty)
configMonoidDockerOpts <- jsonSubWarnings (obj ..:? configMonoidDockerOptsName ..!= mempty)
Expand Down Expand Up @@ -1104,7 +1109,7 @@ data ConfigException
| ResolverPartial Resolver String
| NoSuchDirectory FilePath
| ParseGHCVariantException String
| BadStackRootEnvVar (Path Abs Dir)
| BadStackRoot (Path Abs Dir)
| Won'tCreateStackRootInDirectoryOwnedByDifferentUser (Path Abs Dir) (Path Abs Dir) -- ^ @$STACK_ROOT@, parent dir
| UserDoesn'tOwnDirectory (Path Abs Dir)
deriving Typeable
Expand Down Expand Up @@ -1182,17 +1187,13 @@ instance Show ConfigException where
[ "Invalid ghc-variant value: "
, v
]
show (BadStackRootEnvVar envStackRoot) = concat
[ "Invalid $"
, stackRootEnvVar
, ": '"
, toFilePath envStackRoot
show (BadStackRoot stackRoot) = concat
[ "Invalid stack root: '"
, toFilePath stackRoot
, "'. Please provide a valid absolute path."
]
show (Won'tCreateStackRootInDirectoryOwnedByDifferentUser envStackRoot parentDir) = concat
[ "Preventing creation of $"
, stackRootEnvVar
, " '"
[ "Preventing creation of stack root '"
, toFilePath envStackRoot
, "'. Parent directory '"
, toFilePath parentDir
Expand Down
20 changes: 16 additions & 4 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,20 @@ pathCmd keys go =
distDir <- distRelativeDir
hpcDir <- hpcReportDir
compilerPath <- getCompilerPath =<< getWhichCompiler
when (T.pack deprecatedStackRootOptionName `elem` keys) $
liftIO $ forM_
[ ""
, "'--" <> deprecatedStackRootOptionName <> "' will be removed in a future release."
, "Please use '--" <> stackRootOptionName <> "' instead."
, ""
]
(hPutStrLn stderr)
forM_
-- filter the chosen paths in flags (keys),
-- or show all of them if no specific paths chosen.
(filter
(\(_,key,_) ->
null keys || elem key keys)
(null keys && key /= T.pack deprecatedStackRootOptionName) || elem key keys)
paths)
(\(_,key,path) ->
liftIO $ T.putStrLn
Expand Down Expand Up @@ -583,7 +591,7 @@ data PathInfo = PathInfo
-- | The paths of interest to a user. The first tuple string is used
-- for a description that the optparse flag uses, and the second
-- string as a machine-readable key and also for @--foo@ flags. The user
-- can choose a specific path to list like @--global-stack-root@. But
-- can choose a specific path to list like @--stack-root@. But
-- really it's mainly for the documentation aspect.
--
-- When printing output we generate @PathInfo@ and pass it to the
Expand All @@ -592,7 +600,7 @@ data PathInfo = PathInfo
paths :: [(String, Text, PathInfo -> Text)]
paths =
[ ( "Global stack root directory"
, "global-stack-root"
, T.pack stackRootOptionName
, T.pack . toFilePathNoTrailingSep . configStackRoot . bcConfig . piBuildConfig )
, ( "Project root (derived from stack.yaml file)"
, "project-root"
Expand Down Expand Up @@ -647,7 +655,11 @@ paths =
, T.pack . toFilePathNoTrailingSep . piDistDir )
, ( "Where HPC reports and tix files are stored"
, "local-hpc-root"
, T.pack . toFilePathNoTrailingSep . piHpcDir ) ]
, T.pack . toFilePathNoTrailingSep . piHpcDir )
, ( "DEPRECATED: Use '--" <> stackRootOptionName <> "' instead"
, T.pack deprecatedStackRootOptionName
, T.pack . toFilePathNoTrailingSep . configStackRoot . bcConfig . piBuildConfig )
]

data SetupCmdOpts = SetupCmdOpts
{ scoCompilerVersion :: !(Maybe CompilerVersion)
Expand Down