Skip to content

Commit

Permalink
Fix #6195 Move code from executable to library component
Browse files Browse the repository at this point in the history
Also exposes all modules.

Also consolidates functions in module `Stack.BuildInfo`, deleting module `Stack.Internal.BuildInfo`.
  • Loading branch information
mpilgrem committed Jul 25, 2023
1 parent 61f9081 commit 286bd5a
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 87 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ that contain the link text See the
## Error messages

Stack catches exceptions thrown by its dependencies or by Stack itself in
`Main.main`. In addition to exceptions that halt Stack's execution, Stack logs
`Stack.main`. In addition to exceptions that halt Stack's execution, Stack logs
certain other matters as 'errors'.

To support the Haskell Foundation's
Expand Down
12 changes: 12 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{-# LANGUAGE NoImplicitPrelude #-}

module Main
( main
) where

import RIO ( IO )
import qualified Stack

-- | The entry point for the Stack executable.
main :: IO ()
main = Stack.main
21 changes: 10 additions & 11 deletions doc/maintainers/stack_errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
In connection with considering Stack's support of the
[Haskell Error Index](https://errors.haskell.org/) initiative, this page seeks
to take stock of the errors that Stack itself can raise, by reference to the
`master` branch of the Stack repository. Last updated: 2023-07-10.
`master` branch of the Stack repository. Last updated: 2023-07-24.

* `Main.main`: catches exceptions from action `commandLineHandler`.
* `Stack.main`: catches exceptions from action `commandLineHandler`.

`ExitCode`
- `ExitCode`
- `throwIO`

`throwIO`
* `Stack.StackException`

~~~haskell
[S-2186] = InvalidReExecVersion String String
~~~

* `Main.main`: catches exceptions from action `run`:
* `Stack.main`: catches exceptions from action `run`:

- `ExitCode` (`exitWith`)
- `PrettyException` (`exitFailure`)
Expand All @@ -29,12 +34,6 @@ to take stock of the errors that Stack itself can raise, by reference to the
[S-2816] = InconsistentDependenciesBug
~~~

- `Main.MainException`

~~~haskell
[S-2186] = InvalidReExecVersion String String
~~~

- `Options.Applicative.Builder.Extra.OptionsApplicativeExtraException`

~~~haskell
Expand Down
38 changes: 24 additions & 14 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ when:
else:
cpp-options: -DSTACK_DEVELOPER_MODE_DEFAULT=False
library:
source-dirs: src/
source-dirs: src
ghc-options:
- -Widentities
generated-exposed-modules:
- Build_stack
- Paths_stack
exposed-modules:
- Control.Concurrent.Execute
Expand All @@ -160,8 +161,10 @@ library:
- Options.Applicative.Builder.Extra
- Options.Applicative.Complicated
- Path.CheckInstall
- Path.Extended
- Path.Extra
- Path.Find
- Stack
- Stack.Build
- Stack.Build.Cache
- Stack.Build.ConstructPlan
Expand All @@ -171,14 +174,18 @@ library:
- Stack.Build.Source
- Stack.Build.Target
- Stack.BuildPlan
- Stack.BuildInfo
- Stack.CLI
- Stack.Clean
- Stack.ComponentFile
- Stack.Config
- Stack.Config.Build
- Stack.Config.Docker
- Stack.Config.Nix
- Stack.ConfigCmd
- Stack.Constants
- Stack.Constants.Config
- Stack.Constants.StackProgName
- Stack.Coverage
- Stack.DefaultColorWhen
- Stack.Docker
Expand Down Expand Up @@ -231,6 +238,7 @@ library:
- Stack.Options.Utils
- Stack.Package
- Stack.PackageDump
- Stack.PackageFile
- Stack.Path
- Stack.Prelude
- Stack.Query
Expand All @@ -254,6 +262,8 @@ library:
- Stack.Types.BuildConfig
- Stack.Types.BuildOpts
- Stack.Types.CabalConfigKey
- Stack.Types.Cache
- Stack.Types.Casa
- Stack.Types.ColorWhen
- Stack.Types.CompilerBuild
- Stack.Types.CompilerPaths
Expand All @@ -263,6 +273,7 @@ library:
- Stack.Types.ConfigMonoid
- Stack.Types.ConfigureOpts
- Stack.Types.Curator
- Stack.Types.Dependency
- Stack.Types.Docker
- Stack.Types.DockerEntrypoint
- Stack.Types.DownloadInfo
Expand All @@ -283,6 +294,7 @@ library:
- Stack.Types.NamedComponent
- Stack.Types.Nix
- Stack.Types.Package
- Stack.Types.PackageFile
- Stack.Types.PackageName
- Stack.Types.ParentMap
- Stack.Types.Platform
Expand All @@ -296,6 +308,7 @@ library:
- Stack.Types.SetupInfo
- Stack.Types.SourceMap
- Stack.Types.StackYamlLoc
- Stack.Types.Storage
- Stack.Types.TemplateName
- Stack.Types.UnusedFlags
- Stack.Types.Version
Expand All @@ -316,13 +329,19 @@ library:
else:
source-dirs: src/unix/
c-sources: src/unix/cbits/uname.c
- condition: ! '!(flag(disable-git-info))'
cpp-options: -DUSE_GIT_INFO
dependencies:
- githash
- optparse-simple
- condition: flag(hide-dependency-versions)
cpp-options: -DHIDE_DEP_VERSIONS
- condition: flag(supported-build)
cpp-options: -DSUPPORTED_BUILD
executables:
stack:
main: Main.hs
source-dirs: src/main
generated-other-modules:
- Build_stack
- Paths_stack
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
Expand All @@ -333,15 +352,6 @@ executables:
ld-options:
- -static
- -pthread
- condition: ! '!(flag(disable-git-info))'
cpp-options: -DUSE_GIT_INFO
dependencies:
- githash
- optparse-simple
- condition: flag(hide-dependency-versions)
cpp-options: -DHIDE_DEP_VERSIONS
- condition: flag(supported-build)
cpp-options: -DSUPPORTED_BUILD
stack-integration-test:
main: IntegrationSpec.hs
source-dirs:
Expand Down
12 changes: 7 additions & 5 deletions src/main/Main.hs → src/Stack.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

-- | Main Stack tool entry point.

module Main (main) where
module Stack
( main
) where

import BuildInfo ( versionString' )
import GHC.IO.Encoding ( mkTextEncoding, textEncodingName )
import Options.Applicative.Builder.Extra ( execExtraHelp )
import Stack.BuildInfo ( versionString' )
import Stack.CLI ( commandLineHandler )
import Stack.Constants ( stackProgName )
import Stack.Docker ( dockerCmdName, dockerHelpOptName )
Expand All @@ -30,12 +32,12 @@ import System.Environment ( getArgs, getProgName )
import System.IO ( hGetEncoding, hPutStrLn, hSetEncoding )
import System.Terminal ( hIsTerminalDeviceOrMinTTY )

-- | Type representing exceptions thrown by functions in the "Main" module.
data MainException
-- | Type representing exceptions thrown by functions in the "Stack" module.
data StackException
= InvalidReExecVersion String String
deriving (Show, Typeable)

instance Exception MainException where
instance Exception StackException where
displayException (InvalidReExecVersion expected actual) = concat
[ "Error: [S-2186]\n"
, "When re-executing '"
Expand Down
17 changes: 14 additions & 3 deletions src/main/BuildInfo.hs → src/Stack/BuildInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
{-# LANGUAGE TemplateHaskell #-}
#endif

-- Extracted from Main so that the Main module does not use CPP or TH,
-- Extracted from "Stack" so that module does not use CPP or Template Haskell,
-- and therefore doesn't need to be recompiled as often.
module BuildInfo
module Stack.BuildInfo
( versionString'
, hpackVersion
, maybeGitHash
) where

#ifndef HIDE_DEP_VERSIONS
Expand All @@ -20,7 +21,7 @@ import Data.Version ( versionBranch )
import Distribution.System ( buildArch )
import qualified Distribution.Text as Cabal ( display )
#ifdef USE_GIT_INFO
import GitHash ( giCommitCount, tGitInfoCwdTry )
import GitHash ( giCommitCount, giHash, tGitInfoCwdTry )
import Options.Applicative.Simple ( simpleVersion )
#endif
import qualified Paths_stack as Meta
Expand Down Expand Up @@ -76,3 +77,13 @@ versionString' = showStackVersion ++ afterVersion
-- | Hpack version we're compiled against
hpackVersion :: String
hpackVersion = VERSION_hpack

-- | If USE_GIT_INFO is enabled, the Git hash in the build directory, otherwise
-- Nothing.
maybeGitHash :: Maybe String
maybeGitHash =
#ifdef USE_GIT_INFO
(either (const Nothing) (Just . giHash) $$tGitInfoCwdTry)
#else
Nothing
#endif
3 changes: 2 additions & 1 deletion src/main/Stack/CLI.hs → src/Stack/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Stack.CLI
( commandLineHandler
) where

import BuildInfo ( hpackVersion, versionString' )
import Data.Attoparsec.Interpreter ( getInterpreterArgs )
import Data.Char ( toLower )
import qualified Data.List as L
Expand All @@ -20,6 +19,7 @@ import Options.Applicative.Complicated
import qualified RIO.Process ( exec )
import RIO.Process ( withProcessContextNoLogging )
import Stack.Build ( buildCmd )
import Stack.BuildInfo ( hpackVersion, versionString' )
import Stack.Clean ( CleanCommand (..), cleanCmd )
import Stack.ConfigCmd as ConfigCmd
import Stack.Constants ( globalFooter, osIsWindows, stackProgName )
Expand Down Expand Up @@ -82,6 +82,7 @@ import qualified System.Directory as D
import System.Environment ( getProgName, withArgs )
import System.FilePath ( pathSeparator, takeDirectory )

-- | Stack's command line handler.
commandLineHandler ::
FilePath
-> String
Expand Down
25 changes: 0 additions & 25 deletions src/Stack/Internal/BuildInfo.hs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Stack/Upgrade.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import Path ( (</>), parseRelDir )
import RIO.Process ( proc, runProcess_, withWorkingDir )
import Stack.Build ( build )
import Stack.Build.Target ( NeedTargets (..) )
import Stack.BuildInfo ( maybeGitHash )
import Stack.Constants ( relDirStackProgName, stackDotYaml )
import Stack.Internal.BuildInfo ( maybeGitHash )
import Stack.Prelude hiding ( force, Display (..) )
import Stack.Runners
( ShouldReexec (..), withConfig, withEnvConfig
Expand Down
Loading

0 comments on commit 286bd5a

Please sign in to comment.