-
-
Notifications
You must be signed in to change notification settings - Fork 435
Re-run pre-build rules when necessary #4923
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
Draft
sheaf
wants to merge
2
commits into
haskell:master
Choose a base branch
from
sheaf:hooks-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| genVal :: Int | ||
| genVal = 42 |
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,4 @@ | ||
| module Lib where | ||
| import Gen (genVal) | ||
| libVal :: Int | ||
| libVal = genVal |
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,77 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DeriveAnyClass #-} | ||
| {-# LANGUAGE DeriveGeneric #-} | ||
| {-# LANGUAGE DerivingStrategies #-} | ||
| {-# LANGUAGE RecordWildCards #-} | ||
| {-# LANGUAGE StaticPointers #-} | ||
|
|
||
| module SetupHooks where | ||
|
|
||
| -- Cabal | ||
| import Distribution.Compat.Binary | ||
| import Distribution.Simple.LocalBuildInfo (interpretSymbolicPathLBI) | ||
| import Distribution.Simple.SetupHooks | ||
| import Distribution.Simple.Utils (rewriteFileEx) | ||
| import Distribution.Utils.Path | ||
| import Distribution.Verbosity (normal, mkVerbosity, defaultVerbosityHandles) | ||
|
|
||
| -- base | ||
| import Control.Monad.IO.Class (liftIO) | ||
| import Data.List (isSuffixOf) | ||
| import qualified Data.List.NonEmpty as NE (NonEmpty (..)) | ||
| import Data.String (fromString) | ||
| import GHC.Generics | ||
|
|
||
| -- directory | ||
| import System.Directory (listDirectory) | ||
|
|
||
| -- filepath | ||
| import System.FilePath (dropExtension) | ||
|
|
||
| -- This import is unnecessary, but it's kept around so that this file would | ||
| -- fail to compile were we to use a version of Cabal that doesn't write out | ||
| -- a pre-build rule manifest file. | ||
| import Distribution.Simple.BuildPaths (preBuildMonitorManifestFile) | ||
|
|
||
| -------------------------------------------------------------------------------- | ||
|
|
||
| setupHooks :: SetupHooks | ||
| setupHooks = noSetupHooks | ||
| { buildHooks = noBuildHooks | ||
| { preBuildComponentRules = Just $ rules (static ()) preBuildRules } } | ||
|
|
||
| preBuildRules :: PreBuildComponentInputs -> RulesM () | ||
| preBuildRules (PreBuildComponentInputs { localBuildInfo = lbi, targetInfo = tgt }) = do | ||
| let clbi = targetCLBI tgt | ||
| autogenDir = autogenComponentModulesDir lbi clbi | ||
| srcDir = sameDirectory | ||
| allFiles <- liftIO $ listDirectory (interpretSymbolicPathLBI lbi srcDir) | ||
| mapM_ (registerMyPP srcDir autogenDir) (filter (".myPP" `isSuffixOf`) allFiles) | ||
|
|
||
| registerMyPP | ||
| :: SymbolicPath Pkg (Dir Source) | ||
| -> SymbolicPath Pkg (Dir Source) | ||
| -> FilePath | ||
| -> RulesM () | ||
| registerMyPP srcDir autogenDir fileName = | ||
| let baseName = dropExtension fileName | ||
| in registerRule_ (fromString $ "myPP " ++ baseName) $ | ||
| staticRule | ||
| (mkCommand (static Dict) (static runMyPP) $ | ||
| MyPPInput { ppSrcDir = srcDir, ppAutogenDir = autogenDir, ppBaseName = baseName }) | ||
| [ FileDependency $ Location srcDir (makeRelativePathEx fileName) ] | ||
| ( Location autogenDir (makeRelativePathEx baseName <.> "hs") NE.:| [] ) | ||
|
|
||
| runMyPP :: MyPPInput -> IO () | ||
| runMyPP MyPPInput{..} = do | ||
| content <- readFile (getSymbolicPath ppSrcDir </> ppBaseName <.> "myPP") | ||
| rewriteFileEx (mkVerbosity defaultVerbosityHandles normal) | ||
| (getSymbolicPath ppAutogenDir </> ppBaseName <.> "hs") $ | ||
| "module " ++ ppBaseName ++ " where\n" ++ content | ||
|
|
||
| data MyPPInput = MyPPInput | ||
| { ppSrcDir :: SymbolicPath Pkg (Dir Source) | ||
| , ppAutogenDir :: SymbolicPath Pkg (Dir Source) | ||
| , ppBaseName :: String | ||
| } deriving stock (Show, Generic) | ||
| deriving anyclass Binary |
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,10 @@ | ||
| packages: . | ||
|
|
||
| -- Use a version of Cabal that writes the pre-build rule manifest. | ||
| -- | ||
| -- TODO: remove this. | ||
| source-repository-package | ||
| type: git | ||
| location: https://github.com/sheaf/cabal.git | ||
| subdir: Cabal-syntax Cabal Cabal-hooks | ||
| tag: 2a320cac93255683829768517ceadcd25cbf8f11 | ||
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,19 @@ | ||
| cabal-version: 3.14 | ||
| name: setup-hooks-test | ||
| version: 0.1.0.0 | ||
| synopsis: HLS test fixture for build-type: Hooks | ||
| build-type: Hooks | ||
|
|
||
| custom-setup | ||
| setup-depends: | ||
| base | ||
| , Cabal | ||
| , Cabal-hooks | ||
| , directory | ||
| , filepath | ||
|
|
||
| library | ||
| autogen-modules: Gen | ||
| exposed-modules: Gen, Lib | ||
| build-depends: base | ||
| default-language: Haskell2010 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| -- | Tests for @build-type: Hooks@ support in HLS. | ||
| module SetupHooksTests (tests) where | ||
|
|
||
| import Config (runWithExtraFiles) | ||
| import Control.Exception (bracket_) | ||
| import Control.Monad.IO.Class (liftIO) | ||
| import System.Environment.Blank (setEnv, unsetEnv) | ||
| import Development.IDE.GHC.Util (readFileUtf8) | ||
| import Development.IDE.Plugin.Test (WaitForIdeRuleResult (..)) | ||
| import Development.IDE.Test (expectCurrentDiagnostics, | ||
| waitForAction, | ||
| waitForTypecheck) | ||
| import Language.LSP.Protocol.Message | ||
| import Language.LSP.Protocol.Types hiding | ||
| (SemanticTokenAbsolute (..), | ||
| SemanticTokenRelative (..), | ||
| SemanticTokensEdit (..), | ||
| mkRange) | ||
| import Language.LSP.Test | ||
| import System.FilePath | ||
| import Test.Hls (waitForProgressDone, | ||
| waitForAllProgressDone) | ||
| import Test.Hls.FileSystem (atomicFileWriteString) | ||
| import Test.Tasty | ||
| import Test.Tasty.HUnit | ||
|
|
||
| tests :: TestTree | ||
| tests = testGroup "build-type: Hooks" | ||
| [ testCase "loads generated modules" hooksInitialLoad | ||
| , testCase "re-runs rules when rule input changes" hooksRuleInputChange | ||
| ] | ||
|
|
||
| -- | Increased timeout for setup-hooks tests, which need to compile | ||
| -- @Cabal-syntax@, @Cabal@ and @Cabal-hooks@ in order to compile @SetupHooks.hs@. | ||
| withHooksTimeout :: IO a -> IO a | ||
| withHooksTimeout = bracket_ (setEnv "LSP_TIMEOUT" "120" True) (unsetEnv "LSP_TIMEOUT") | ||
| -- LSP_TIMEOUT = 120 seconds = 2 minutes | ||
|
|
||
| -- | Open a module that imports module generated by a pre-build rule, | ||
| -- ensuring that it successfully compiles. | ||
| hooksInitialLoad :: IO () | ||
| hooksInitialLoad = withHooksTimeout $ runWithExtraFiles "setup-hooks" $ \dir -> do | ||
| let libPath = dir </> "Lib.hs" | ||
| libSrc <- liftIO $ readFileUtf8 libPath | ||
| libDoc <- createDoc libPath "haskell" libSrc | ||
| waitForProgressDone | ||
| expectCurrentDiagnostics libDoc [] | ||
|
|
||
| -- | Modify a .myPP pre-build rule input, notify HLS, and verify the regenerated | ||
| -- module causes an expected type error. | ||
| -- | ||
| -- This checks that HLS re-runs pre-build rules when necessary. | ||
| hooksRuleInputChange :: IO () | ||
| hooksRuleInputChange = withHooksTimeout $ runWithExtraFiles "setup-hooks" $ \dir -> do | ||
| let libPath = dir </> "Lib.hs" | ||
| genMyPP = dir </> "Gen.myPP" | ||
| libSrc <- liftIO $ readFileUtf8 libPath | ||
| libDoc <- createDoc libPath "haskell" libSrc | ||
|
|
||
| -- Check the package builds. This ensures SetupHooks.hs compiled successfully, | ||
| -- and that the pre-build rules were run (generating Gen.hs). | ||
| waitForAllProgressDone | ||
| expectCurrentDiagnostics libDoc [] | ||
|
|
||
| -- Modify Gen.myPP, changing the type of 'genVal'. | ||
| liftIO $ atomicFileWriteString genMyPP "genVal :: Bool\ngenVal = True\n" | ||
|
|
||
| -- Notify HLS that Gen.myPP has changed. HLS should trigger a re-run of | ||
| -- pre-build rules. | ||
| sendNotification SMethod_WorkspaceDidChangeWatchedFiles $ DidChangeWatchedFilesParams | ||
| [FileEvent (filePathToUri genMyPP) FileChangeType_Changed] | ||
|
|
||
| -- Wait for the session to reload, which should re-run pre-build rules. | ||
| WaitForIdeRuleResult { ideResultSuccess = sessionOk } <- waitForAction "GhcSession" libDoc | ||
| liftIO $ assertBool "GhcSession should succeed after reload" sessionOk | ||
|
|
||
| -- We now expect a type error from the change in type of 'genVal'. | ||
| _ <- waitForTypecheck libDoc | ||
| expectCurrentDiagnostics libDoc | ||
| [(DiagnosticSeverity_Error, (3, 9), "Couldn't match", Just "GHC-83865")] |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to pin the Cabal version for the tests to ensure the pre-build rule manifest file is being written. I'm not sure whether there's a better way to do this? We need to guarantee we use the right
Caballibrary when compilingSetupHooks(ordefaultMainWithSetupHooks setupHooksif we are falling back to the Setup CLI, e.g. because of an oldcabal-installthat is incompatible with this newer version of theCaballibrary).The fact that we need to compile
SetupHooksagainst this version ofCabalalso means the test is a bit slow to run as it needs to buildCabal-syntax,CabalandCabal-hooks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can run this test only if we can ensure the
cabal-installversion is recent enough and then ensure in CI the cabal version is new :)