Skip to content

Use runtime ghc libdir for ghc-exactprint and ghc-8.10 #1451

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
merged 5 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions plugins/hls-hlint-plugin/hls-hlint-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Is it OK to not update cabal.project index?

Copy link
Member Author

@jneira jneira Feb 27, 2021

Choose a reason for hiding this comment

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

Yeah, that ghc-lib version was released on 2021-02-06T16:51:55Z

, ghc-lib-parser-ex ^>= 8.10

cpp-options: -DHLINT_ON_GHC_LIB
Expand Down
37 changes: 14 additions & 23 deletions plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stack-8.10.2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions stack-8.10.3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stack-8.6.4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stack-8.6.5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stack-8.8.2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stack-8.8.3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions stack-8.8.4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down