diff --git a/plugins/hls-hlint-plugin/hls-hlint-plugin.cabal b/plugins/hls-hlint-plugin/hls-hlint-plugin.cabal index 212aeda12b..879c0de666 100644 --- a/plugins/hls-hlint-plugin/hls-hlint-plugin.cabal +++ b/plugins/hls-hlint-plugin/hls-hlint-plugin.cabal @@ -53,13 +53,13 @@ library , transformers , unordered-containers - if ((!flag(ghc-lib) && impl(ghc >=8.10.1)) && impl(ghc <8.11.0)) + if (!flag(ghc-lib) && impl(ghc >=8.10.1) && impl(ghc <9.0.0)) build-depends: ghc ^>= 8.10 else build-depends: , ghc - , ghc-lib ^>= 8.10.2.20200916 + , ghc-lib ^>= 8.10.4.20210206 , ghc-lib-parser-ex ^>= 8.10 cpp-options: -DHLINT_ON_GHC_LIB diff --git a/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs b/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs index d93c22b234..8f83618617 100644 --- a/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs +++ b/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs @@ -51,8 +51,6 @@ import "ghc-lib-parser" GHC.LanguageExtensions (Extension) import "ghc" HscTypes as RealGHC.HscTypes (hsc_dflags, ms_hspp_opts) import Language.Haskell.GhclibParserEx.GHC.Driver.Session as GhclibParserEx (readExtension) -import System.Environment (setEnv, - unsetEnv) import System.FilePath (takeFileName) import System.IO (IOMode (WriteMode), hClose, @@ -86,6 +84,8 @@ import qualified Language.LSP.Types.Lens as LSP import GHC.Generics (Generic) import Text.Regex.TDFA.Text () +import System.Environment (setEnv, + unsetEnv) -- --------------------------------------------------------------------- descriptor :: PluginId -> PluginDescriptor IdeState @@ -380,36 +380,27 @@ applyHint ide nfp mhint = oldContent <- maybe (liftIO $ T.readFile fp) return mbOldContent (modsum, _) <- liftIO $ runAction' $ use_ GetModSummary nfp let dflags = ms_hspp_opts modsum + -- Setting a environment variable with the libdir used by ghc-exactprint. + -- It is a workaround for an error caused by the use of a hadcoded at compile time libdir + -- in ghc-exactprint that makes dependent executables non portables. + -- See https://github.com/alanz/ghc-exactprint/issues/96. + -- WARNING: this code is not thread safe, so if you try to apply several async refactorings + -- it could fail. That case is not very likely so we assume the risk. + let withRuntimeLibdir :: IO a -> IO a + withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key) + where key = "GHC_EXACTPRINT_GHC_LIBDIR" -- set Nothing as "position" for "applyRefactorings" because -- applyRefactorings expects the provided position to be _within_ the scope -- of each refactoring it will apply. -- But "Idea"s returned by HLint point to starting position of the expressions -- that contain refactorings, so they are often outside the refactorings' boundaries. - -- Example: - -- Given an expression "hlintTest = reid $ (myid ())" - -- Hlint returns an idea at the position (1,13) - -- That contains "Redundant brackets" refactoring at position (1,20): - -- - -- [("src/App/Test.hs:5:13: Warning: Redundant bracket\nFound:\n reid $ (myid ())\nWhy not:\n reid $ myid ()\n",[Replace {rtype = Expr, pos = SrcSpan {startLine = 5, startCol = 20, endLine = 5, endCol = 29}, subts = [("x",SrcSpan {startLine = 5, startCol = 21, endLine = 5, endCol = 28})], orig = "x"}])] - -- - -- If we provide "applyRefactorings" with "Just (1,13)" then - -- the "Redundant bracket" hint will never be executed - -- because SrcSpan (1,20,??,??) doesn't contain position (1,13). + let position = Nothing #ifdef HLINT_ON_GHC_LIB let writeFileUTF8NoNewLineTranslation file txt = withFile file WriteMode $ \h -> do hSetEncoding h utf8 hSetNewlineMode h noNewlineTranslation hPutStr h (T.unpack txt) - -- Setting a environment variable with the libdir used by ghc-exactprint. - -- It is a workaround for an error caused by the use of a hadcoded at compile time libdir - -- in ghc-exactprint that makes dependent executables non portables. - -- See https://github.com/alanz/ghc-exactprint/issues/96. - -- WARNING: this code is not thread safe, so if you try to apply several async refactorings - -- it could fail. That case is not very likely so we assume the risk. - let withRuntimeLibdir :: IO a -> IO a - withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key) - where key = "GHC_EXACTPRINT_GHC_LIBDIR" res <- liftIO $ withSystemTempFile (takeFileName fp) $ \temp h -> do hClose h @@ -419,7 +410,7 @@ applyHint ide nfp mhint = -- We have to reparse extensions to remove the invalid ones let (enabled, disabled, _invalid) = parseExtensions $ map show exts let refactExts = map show $ enabled ++ disabled - (Right <$> withRuntimeLibdir (applyRefactorings Nothing commands temp refactExts)) + (Right <$> withRuntimeLibdir (applyRefactorings position commands temp refactExts)) `catches` errorHandlers #else mbParsedModule <- liftIO $ runAction' $ getParsedModuleWithComments nfp @@ -433,7 +424,7 @@ applyHint ide nfp mhint = let rigidLayout = deltaOptions RigidLayout (anns', modu') <- ExceptT $ return $ postParseTransform (Right (anns, [], dflags, modu)) rigidLayout - liftIO $ (Right <$> applyRefactorings' Nothing commands anns' modu') + liftIO $ (Right <$> withRuntimeLibdir (applyRefactorings' position commands anns' modu')) `catches` errorHandlers #endif case res of diff --git a/stack-8.10.2.yaml b/stack-8.10.2.yaml index d6df60ed16..da17e3636d 100644 --- a/stack-8.10.2.yaml +++ b/stack-8.10.2.yaml @@ -27,8 +27,8 @@ extra-deps: - floskell-0.10.4 - fourmolu-0.3.0.0 - ghc-exactprint-0.6.3.4 - - ghc-lib-8.10.3.20201220 - - ghc-lib-parser-8.10.3.20201220 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - lsp-1.1.1.0 - lsp-types-1.1.0.0 - lsp-test-0.13.0.0 diff --git a/stack-8.10.3.yaml b/stack-8.10.3.yaml index acfdf37837..bc2c9bf1a4 100644 --- a/stack-8.10.3.yaml +++ b/stack-8.10.3.yaml @@ -25,6 +25,8 @@ extra-deps: - data-tree-print-0.1.0.2@rev:2 - floskell-0.10.4 - fourmolu-0.3.0.0 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - heapsize-0.3.0 - hie-bios-0.7.4 - implicit-hie-cradle-0.3.0.2 diff --git a/stack-8.6.4.yaml b/stack-8.6.4.yaml index 9086ddf502..9b932bd668 100644 --- a/stack-8.6.4.yaml +++ b/stack-8.6.4.yaml @@ -36,8 +36,8 @@ extra-deps: - ghc-check-0.5.0.1 - ghc-events-0.13.0 - ghc-exactprint-0.6.3.4 - - ghc-lib-8.10.3.20201220 - - ghc-lib-parser-8.10.3.20201220 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - ghc-lib-parser-ex-8.10.0.17 - ghc-source-gen-0.4.0.0 - ghc-trace-events-0.1.2.1 diff --git a/stack-8.6.5.yaml b/stack-8.6.5.yaml index 6c3d61fda8..a96689d6fa 100644 --- a/stack-8.6.5.yaml +++ b/stack-8.6.5.yaml @@ -35,8 +35,8 @@ extra-deps: - ghc-check-0.5.0.1 - ghc-events-0.13.0 - ghc-exactprint-0.6.3.4 - - ghc-lib-8.10.3.20201220 - - ghc-lib-parser-8.10.3.20201220 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - ghc-lib-parser-ex-8.10.0.17 - ghc-source-gen-0.4.0.0 - ghc-trace-events-0.1.2.1 diff --git a/stack-8.8.2.yaml b/stack-8.8.2.yaml index 8ca84f620c..7a74861048 100644 --- a/stack-8.8.2.yaml +++ b/stack-8.8.2.yaml @@ -31,8 +31,8 @@ extra-deps: - ghc-check-0.5.0.1 - ghc-events-0.13.0 - ghc-exactprint-0.6.3.4 - - ghc-lib-8.10.3.20201220 - - ghc-lib-parser-8.10.3.20201220 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - ghc-lib-parser-ex-8.10.0.17 - ghc-trace-events-0.1.2.1 - haddock-library-1.8.0 diff --git a/stack-8.8.3.yaml b/stack-8.8.3.yaml index 6ccea7c245..0934e0241f 100644 --- a/stack-8.8.3.yaml +++ b/stack-8.8.3.yaml @@ -29,8 +29,8 @@ extra-deps: - floskell-0.10.4 - fourmolu-0.3.0.0 - ghc-exactprint-0.6.3.4 - - ghc-lib-8.10.3.20201220 - - ghc-lib-parser-8.10.3.20201220 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - ghc-trace-events-0.1.2.1 - haskell-src-exts-1.21.1 - heapsize-0.3.0 diff --git a/stack-8.8.4.yaml b/stack-8.8.4.yaml index 56c018cb9a..4f69a8c7c3 100644 --- a/stack-8.8.4.yaml +++ b/stack-8.8.4.yaml @@ -29,6 +29,8 @@ extra-deps: - floskell-0.10.4 - fourmolu-0.3.0.0 - ghc-exactprint-0.6.3.4 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - ghc-trace-events-0.1.2.1 - haskell-src-exts-1.21.1 - heapsize-0.3.0 diff --git a/stack.yaml b/stack.yaml index 82c28566fe..5c212f13a1 100644 --- a/stack.yaml +++ b/stack.yaml @@ -37,8 +37,8 @@ extra-deps: - ghc-check-0.5.0.1 - ghc-events-0.13.0 - ghc-exactprint-0.6.3.4 - - ghc-lib-8.10.3.20201220 - - ghc-lib-parser-8.10.3.20201220 + - ghc-lib-8.10.4.20210206 + - ghc-lib-parser-8.10.4.20210206 - ghc-lib-parser-ex-8.10.0.17 - ghc-source-gen-0.4.0.0 - ghc-trace-events-0.1.2.1