-
Notifications
You must be signed in to change notification settings - Fork 733
Refactor before import warning #11773
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
Merged
mergify
merged 1 commit into
haskell:master
from
cabalism:refactor/before-import-warning
May 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
cabal-install/src/Distribution/Client/ProjectConfig/Import.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| {-# LANGUAGE TupleSections #-} | ||
| {-# LANGUAGE ViewPatterns #-} | ||
| {-# LANGUAGE NoImplicitPrelude #-} | ||
|
|
||
| -- | Project configuration imports. | ||
| module Distribution.Client.ProjectConfig.Import | ||
| ( -- * Parsing skeleton | ||
| ProjectConfigSkeleton | ||
| , projectSkeletonImports | ||
| , fetchImport | ||
|
|
||
| -- * Messages | ||
| , docProjectConfigFiles | ||
| , cyclicalImportMsg | ||
| , untrimmedUriImportMsg | ||
| ) where | ||
|
|
||
| import Control.Arrow (Kleisli (..), arr, (>>>)) | ||
| import qualified Data.ByteString.Char8 as BS | ||
| import Data.Coerce (coerce) | ||
| import Distribution.Client.Compat.Prelude hiding (empty, (<>)) | ||
| import Distribution.Client.HttpUtils | ||
| import Distribution.Client.ProjectConfig.Types | ||
| import Distribution.Compat.Lens (view) | ||
| import Distribution.PackageDescription (ConfVar (..)) | ||
| import Distribution.Simple.Utils (debug, ordNub) | ||
| import Distribution.Solver.Types.ProjectConfigPath | ||
| import Distribution.Types.CondTree (CondTree (..), traverseCondTreeA) | ||
| import Distribution.Utils.String (trim) | ||
| import Network.URI (URI (..), parseURI) | ||
| import System.Directory (createDirectoryIfMissing) | ||
| import System.FilePath (isAbsolute, isPathSeparator, makeValid, (</>)) | ||
| import Text.PrettyPrint | ||
|
|
||
| -- | ProjectConfigSkeleton is a tree of conditional blocks and imports wrapping | ||
| -- a config. It can be finalized by providing the conditional resolution info | ||
| -- and then resolving and downloading the imports | ||
| type ProjectConfigSkeleton = CondTree ConfVar ([ProjectConfigPath], ProjectConfig) | ||
|
|
||
| projectSkeletonImports :: ProjectConfigSkeleton -> [ProjectConfigPath] | ||
| projectSkeletonImports = fst . view traverseCondTreeA | ||
|
|
||
| -- | Fetch a local file import or remote URL import and parse it. | ||
| fetchImport | ||
| :: (ProjectConfigToParse -> IO a) | ||
| -> FilePath | ||
| -> HttpTransport | ||
| -> Verbosity | ||
| -> FilePath | ||
| -> ProjectConfigPath | ||
| -> IO a | ||
| fetchImport parser cacheDir httpTransport verbosity projectDir normLocPath = | ||
| fetchImportConfig normLocPath >>= runKleisli (arr ProjectConfigToParse >>> Kleisli parser) . snd | ||
| where | ||
| fetchImportConfig :: ProjectConfigPath -> IO (Maybe URI, BS.ByteString) | ||
| fetchImportConfig (ProjectConfigPath (pci :| _)) = do | ||
| debug verbosity $ "fetching import: " ++ pci | ||
| let mbUri = parseURI (trim pci) | ||
| (mbUri,) <$> case mbUri of | ||
| Just uri -> do | ||
| let fp = cacheDir </> map (\x -> if isPathSeparator x then '_' else x) (makeValid $ show uri) | ||
| createDirectoryIfMissing True cacheDir | ||
| _ <- downloadURI httpTransport verbosity uri fp | ||
| BS.readFile fp | ||
| Nothing -> | ||
| BS.readFile $ | ||
| if isAbsolute pci then pci else coerce projectDir </> pci | ||
|
|
||
| -- | Renders the paths as a list without showing which path imports another, | ||
| -- like this; | ||
| -- | ||
| -- >- cabal.project | ||
| -- >- project-cabal/constraints.config | ||
| -- >- project-cabal/ghc-latest.config | ||
| -- >- project-cabal/ghc-options.config | ||
| -- >- project-cabal/pkgs.config | ||
| -- >- project-cabal/pkgs/benchmarks.config | ||
| -- >- project-cabal/pkgs/buildinfo.config | ||
| -- >- project-cabal/pkgs/cabal.config | ||
| -- >- project-cabal/pkgs/install.config | ||
| -- >- project-cabal/pkgs/integration-tests.config | ||
| -- >- project-cabal/pkgs/tests.config | ||
| -- | ||
| -- | ||
| -- >>> :{ | ||
| -- do | ||
| -- let ps = | ||
| -- [ ProjectConfigPath ("cabal.project" :| []) | ||
| -- , ProjectConfigPath ("project-cabal/constraints.config" :| ["cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/ghc-latest.config" :| ["cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/ghc-options.config" :| ["cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/pkgs.config" :| ["cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/pkgs/benchmarks.config" :| ["project-cabal/pkgs.config","cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/pkgs/buildinfo.config" :| ["project-cabal/pkgs.config","cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/pkgs/cabal.config" :| ["project-cabal/pkgs.config","cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/pkgs/install.config" :| ["project-cabal/pkgs.config","cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/pkgs/integration-tests.config" :| ["project-cabal/pkgs.config","cabal.project"]) | ||
| -- , ProjectConfigPath ("project-cabal/pkgs/tests.config" :| ["project-cabal/pkgs.config","cabal.project"]) | ||
| -- ] | ||
| -- return . render $ docProjectConfigFiles ps | ||
| -- :} | ||
| -- "- cabal.project\n- project-cabal/constraints.config\n- project-cabal/ghc-latest.config\n- project-cabal/ghc-options.config\n- project-cabal/pkgs.config\n- project-cabal/pkgs/benchmarks.config\n- project-cabal/pkgs/buildinfo.config\n- project-cabal/pkgs/cabal.config\n- project-cabal/pkgs/install.config\n- project-cabal/pkgs/integration-tests.config\n- project-cabal/pkgs/tests.config" | ||
| -- | ||
| -- The listing puts projects first, URLs last and sorts the other paths | ||
| -- lexically, dropping any duplicates, like this: | ||
| -- | ||
| -- >- cabal.project | ||
| -- >- 0.config | ||
| -- >- 2.config | ||
| -- >- cfg/1.config | ||
| -- >- cfg/3.config | ||
| -- >- with-ghc.config | ||
| -- >- https://www.stackage.org/lts-21.25/cabal.config | ||
| -- | ||
| -- >>> let p = ProjectConfigPath $ "cabal.project" :| [] | ||
| -- >>> let a = ProjectConfigPath $ "0.config" :| ["cabal.project"] | ||
| -- >>> let b = ProjectConfigPath $ "cfg/1.config" :| ["0.config", "cabal.project"] | ||
| -- >>> let c = ProjectConfigPath $ "with.config" :| ["0.config", "cabal.project"] | ||
| -- >>> let d = ProjectConfigPath $ "2.config" :| ["cfg/1.config", "0.config", "cabal.project"] | ||
| -- >>> let e = ProjectConfigPath $ "cfg/3.config" :| ["2.config", "cfg/1.config", "0.config", "cabal.project"] | ||
| -- >>> let f = ProjectConfigPath $ "https://www.stackage.org/lts-21.25/cabal.config" :| ["2.config", "cfg/1.config", "0.config", "cabal.project"] | ||
| -- >>> let g = ProjectConfigPath $ "https://www.stackage.org/lts-21.25/cabal.config" :| ["cfg/3.config", "2.config", "cfg/1.config", "0.config", "cabal.project"] | ||
| -- >>> let ps = [p, a, b, c, d, e, f, g] | ||
| -- >>> render $ docProjectConfigFiles ps | ||
| -- "- cabal.project\n- 0.config\n- 2.config\n- cfg/1.config\n- cfg/3.config\n- with.config\n- https://www.stackage.org/lts-21.25/cabal.config" | ||
| docProjectConfigFiles :: [ProjectConfigPath] -> Doc | ||
| docProjectConfigFiles (sortBy compareLexicographically -> ps) = | ||
| vcat | ||
| [ text "-" <+> text p | ||
| | p <- ordNub [p | ProjectConfigPath (p :| _) <- ps] | ||
| ] | ||
|
|
||
| -- | A message for a cyclical import, a "cyclical import of". | ||
| cyclicalImportMsg :: ProjectConfigPath -> Doc | ||
| cyclicalImportMsg path@(ProjectConfigPath (duplicate :| _)) = | ||
| vcat | ||
| [ text "cyclical import of" <+> text duplicate <> semi | ||
| , nest 2 (docProjectConfigPath path) | ||
| ] | ||
|
|
||
| -- | A message for an import that has leading or trailing spaces. | ||
| untrimmedUriImportMsg :: Doc -> ProjectConfigPath -> Doc | ||
| untrimmedUriImportMsg intro path = | ||
| vcat | ||
| [ intro <+> text "import has leading or trailing whitespace" <> semi | ||
| , nest 2 (docProjectConfigPath path) | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.