Skip to content

Commit 7e11ace

Browse files
authored
Fix Binary instance of Q to handle empty file paths (haskell/ghcide#707)
1 parent 8920267 commit 7e11ace

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ test-suite ghcide-tests
296296
build-depends:
297297
aeson,
298298
base,
299+
binary,
299300
bytestring,
300301
containers,
301302
directory,

ghcide/src/Development/IDE/Core/Shake.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ module Development.IDE.Core.Shake(
5757
ProgressEvent(..),
5858
DelayedAction, mkDelayedAction,
5959
IdeAction(..), runIdeAction,
60-
mkUpdater
60+
mkUpdater,
61+
-- Exposed for testing.
62+
Q(..),
6163
) where
6264

6365
import Development.Shake hiding (ShakeValue, doesFileExist, Info)
@@ -792,7 +794,14 @@ isBadDependency x
792794
newtype Q k = Q (k, NormalizedFilePath)
793795
deriving (Eq,Hashable,NFData, Generic)
794796

795-
instance Binary k => Binary (Q k)
797+
instance Binary k => Binary (Q k) where
798+
put (Q (k, fp)) = put (k, fp)
799+
get = do
800+
(k, fp) <- get
801+
-- The `get` implementation of NormalizedFilePath
802+
-- does not handle empty file paths so we
803+
-- need to handle this ourselves here.
804+
pure (Q (k, toNormalizedFilePath' fp))
796805

797806
instance Show k => Show (Q k) where
798807
show (Q (k, file)) = show k ++ "; " ++ fromNormalizedFilePath file

ghcide/test/exe/Main.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import qualified Control.Lens as Lens
1616
import Control.Monad
1717
import Control.Monad.IO.Class (liftIO)
1818
import Data.Aeson (FromJSON, Value)
19+
import qualified Data.Binary as Binary
1920
import Data.Foldable
2021
import Data.List.Extra
2122
import Data.Maybe
2223
import Data.Rope.UTF16 (Rope)
2324
import qualified Data.Rope.UTF16 as Rope
2425
import Development.IDE.Core.PositionMapping (fromCurrent, toCurrent)
26+
import Development.IDE.Core.Shake (Q(..))
2527
import Development.IDE.GHC.Util
2628
import qualified Data.Text as T
2729
import Data.Typeable
@@ -2832,6 +2834,8 @@ unitTests = do
28322834
, testCase "from empty path URI" $ do
28332835
let uri = Uri "file://"
28342836
uriToFilePath' uri @?= Just ""
2837+
, testCase "Key with empty file path roundtrips via Binary" $
2838+
Binary.decode (Binary.encode (Q ((), emptyFilePath))) @?= Q ((), emptyFilePath)
28352839
]
28362840

28372841
positionMappingTests :: TestTree

0 commit comments

Comments
 (0)