Skip to content

Commit 0350c7f

Browse files
authored
Fix .hie file location for .hs-boot files (#690)
* Find source for boot files * fix modlocs for boot files * Add test * Fix build on 8.6
1 parent 271c6e0 commit 0350c7f

File tree

9 files changed

+64
-11
lines changed

9 files changed

+64
-11
lines changed

src/Development/IDE/Core/Compile.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,7 @@ writeHiFile hscEnv tc =
303303
writeIfaceFile dflags fp modIface
304304
where
305305
modIface = hm_iface $ tmrModInfo tc
306-
modSummary = tmrModSummary tc
307-
targetPath = withBootSuffix $ ml_hi_file $ ms_location $ tmrModSummary tc
308-
withBootSuffix = case ms_hsc_src modSummary of
309-
HsBootFile -> addBootSuffix
310-
_ -> id
306+
targetPath = ml_hi_file $ ms_location $ tmrModSummary tc
311307
dflags = hsc_dflags hscEnv
312308

313309
handleGenerationErrors :: DynFlags -> T.Text -> IO () -> IO [FileDiagnostic]
@@ -409,6 +405,10 @@ getImportsParsed dflags (L loc parsed) = do
409405
, GHC.moduleNameString (GHC.unLoc $ ideclName i) /= "GHC.Prim"
410406
])
411407

408+
withBootSuffix :: HscSource -> ModLocation -> ModLocation
409+
withBootSuffix HsBootFile = addBootSuffixLocnOut
410+
withBootSuffix _ = id
411+
412412
-- | Produce a module summary from a StringBuffer.
413413
getModSummaryFromBuffer
414414
:: GhcMonad m
@@ -425,7 +425,7 @@ getModSummaryFromBuffer fp modTime dflags parsed contents = do
425425
let InstalledUnitId unitId = thisInstalledUnitId dflags
426426
return $ ModSummary
427427
{ ms_mod = mkModule (fsToUnitId unitId) modName
428-
, ms_location = modLoc
428+
, ms_location = withBootSuffix sourceType modLoc
429429
, ms_hs_date = modTime
430430
, ms_textual_imps = [imp | (False, imp) <- imports]
431431
, ms_hspp_file = fp
@@ -485,7 +485,7 @@ getModSummaryFromImports fp modTime contents = do
485485
, ms_hspp_file = fp
486486
, ms_hspp_opts = dflags
487487
, ms_iface_date = Nothing
488-
, ms_location = modLoc
488+
, ms_location = withBootSuffix sourceType modLoc
489489
, ms_obj_date = Nothing
490490
, ms_parsed_mod = Nothing
491491
, ms_srcimps = srcImports

src/Development/IDE/Core/Rules.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,7 @@ isHiFileStableRule :: Rules ()
685685
isHiFileStableRule = define $ \IsHiFileStable f -> do
686686
ms <- use_ GetModSummaryWithoutTimestamps f
687687
let hiFile = toNormalizedFilePath'
688-
$ case ms_hsc_src ms of
689-
HsBootFile -> addBootSuffix (ml_hi_file $ ms_location ms)
690-
_ -> ml_hi_file $ ms_location ms
688+
$ ml_hi_file $ ms_location ms
691689
mbHiVersion <- use GetModificationTime_{missingFileDiagnostics=False} hiFile
692690
modVersion <- use_ GetModificationTime f
693691
sourceModified <- case mbHiVersion of

src/Development/IDE/GHC/Compat.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Development.IDE.GHC.Compat(
2323
dontWriteHieFiles,
2424
#if !MIN_GHC_API_VERSION(8,8,0)
2525
ml_hie_file,
26+
addBootSuffixLocnOut,
2627
#endif
2728
hPutStringBuffer,
2829
includePathsGlobal,
@@ -122,6 +123,7 @@ import System.FilePath ((-<.>))
122123

123124
#if MIN_GHC_API_VERSION(8,6,0)
124125
import GhcPlugins (srcErrorMessages)
126+
import Data.List (isSuffixOf)
125127
#else
126128
import System.IO.Error
127129
import IfaceEnv
@@ -153,7 +155,9 @@ hieExportNames = nameListFromAvails . hie_exports
153155

154156
#if !MIN_GHC_API_VERSION(8,8,0)
155157
ml_hie_file :: GHC.ModLocation -> FilePath
156-
ml_hie_file ml = ml_hi_file ml -<.> ".hie"
158+
ml_hie_file ml
159+
| "boot" `isSuffixOf ` ml_hi_file ml = ml_hi_file ml -<.> ".hie-boot"
160+
| otherwise = ml_hi_file ml -<.> ".hie"
157161
#endif
158162

159163
#endif
@@ -380,6 +384,14 @@ instance HasSrcSpan (GenLocated SrcSpan a) where
380384
getHeaderImports a b c d =
381385
catch (Right <$> Hdr.getImports a b c d)
382386
(return . Left . srcErrorMessages)
387+
388+
-- | Add the @-boot@ suffix to all output file paths associated with the
389+
-- module, not including the input file itself
390+
addBootSuffixLocnOut :: GHC.ModLocation -> GHC.ModLocation
391+
addBootSuffixLocnOut locn
392+
= locn { ml_hi_file = Module.addBootSuffix (ml_hi_file locn)
393+
, ml_obj_file = Module.addBootSuffix (ml_obj_file locn)
394+
}
383395
#endif
384396

385397
getModuleHash :: ModIface -> Fingerprint

test/data/boot/A.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module A where
2+
3+
import B( TB(..) )
4+
5+
newtype TA = MkTA Int
6+
7+
f :: TB -> TA
8+
f (MkTB x) = MkTA x

test/data/boot/A.hs-boot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module A where
2+
newtype TA = MkTA Int

test/data/boot/B.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module B(TA(..), TB(..)) where
2+
import {-# SOURCE #-} A( TA(..) )
3+
4+
data TB = MkTB !Int
5+
6+
g :: TA -> TB
7+
g (MkTA x) = MkTB x

test/data/boot/C.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module C where
2+
3+
import B
4+
import A hiding (MkTA(..))
5+
6+
x = MkTA
7+
y = MkTB
8+
z = f

test/data/boot/hie.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cradle: {direct: {arguments: ["A", "B", "C"]}}

test/exe/Main.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ main = do
8787
, nonLspCommandLine
8888
, benchmarkTests
8989
, ifaceTests
90+
, bootTests
9091
]
9192

9293
initializeResponseTests :: TestTree
@@ -2796,6 +2797,22 @@ ifaceTests = testGroup "Interface loading tests"
27962797
, ifaceTHTest
27972798
]
27982799

2800+
bootTests :: TestTree
2801+
bootTests = testCase "boot-def-test" $ withoutStackEnv $ runWithExtraFiles "boot" $ \dir -> do
2802+
let cPath = dir </> "C.hs"
2803+
cSource <- liftIO $ readFileUtf8 cPath
2804+
2805+
-- Dirty the cache
2806+
liftIO $ runInDir dir $ do
2807+
cDoc <- createDoc cPath "haskell" cSource
2808+
_ <- getHover cDoc $ Position 4 3
2809+
closeDoc cDoc
2810+
2811+
cdoc <- createDoc cPath "haskell" cSource
2812+
locs <- getDefinitions cdoc (Position 7 4)
2813+
let floc = mkR 7 0 7 1
2814+
checkDefs locs (pure [floc])
2815+
27992816
-- | test that TH reevaluates across interfaces
28002817
ifaceTHTest :: TestTree
28012818
ifaceTHTest = testCase "iface-th-test" $ withoutStackEnv $ runWithExtraFiles "TH" $ \dir -> do

0 commit comments

Comments
 (0)