Skip to content

Commit dcd7cb4

Browse files
authored
Use a consistent include dir for cwd (#114)
This only matters for the DAML codebase (so I’ll add a test on that side) where we use relative paths: Previously, we would produce the include dir "." for moduleImportPath "./A.daml" and "" for moduleImportPath "./A/B.daml". This resulted in us ending up with ./A.daml and A.daml in the Shake graph which resulted in issues like digital-asset/daml#2929. We should move this logic completely over to the DAML repo at some point but I’ll leave that for a separate PR.
1 parent 79301b4 commit dcd7cb4

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/Development/IDE/Core/Compile.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ upgradeWarningToError (nfp, fd) = (nfp, fd{_severity = Just DsError})
158158

159159
addRelativeImport :: ParsedModule -> DynFlags -> DynFlags
160160
addRelativeImport modu dflags = dflags
161-
{importPaths = nubOrd $ maybeToList (moduleImportPaths modu) ++ importPaths dflags}
161+
{importPaths = nubOrd $ maybeToList (moduleImportPath modu) ++ importPaths dflags}
162162

163163
mkTcModuleResult
164164
:: GhcMonad m

src/Development/IDE/GHC/Util.hs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Development.IDE.GHC.Util(
1717
prettyPrint,
1818
runGhcEnv,
1919
textToStringBuffer,
20-
moduleImportPaths,
20+
moduleImportPath,
2121
HscEnvEq, hscEnv, newHscEnvEq
2222
) where
2323

@@ -103,16 +103,25 @@ fakeDynFlags = defaultDynFlags settings mempty
103103
, pc_WORD_SIZE=8
104104
}
105105

106-
moduleImportPaths :: GHC.ParsedModule -> Maybe FilePath
107-
moduleImportPaths pm
108-
| rootModDir == "." = Just rootPathDir
109-
| otherwise =
110-
dropTrailingPathSeparator <$> stripSuffix (normalise rootModDir) (normalise rootPathDir)
106+
moduleImportPath :: GHC.ParsedModule -> Maybe FilePath
107+
moduleImportPath pm
108+
| rootModDir == "." = Just rootPathDir
109+
| otherwise = do
110+
dir <- dropTrailingPathSeparator <$> stripSuffix (normalise rootModDir) (normalise rootPathDir)
111+
-- For modules with more than one component, this can be empty, e.g.,
112+
-- stripSuffix (normalise ./A) (normalise ./A) for A/B.daml.
113+
-- We make a best effort attemp at not duplicating file paths
114+
-- by mapping the current directory to '.' if 'rootPathDir' starts with '.' and
115+
-- to an empty string otherwise.
116+
pure $! if null dir then dotDir else dir
111117
where
118+
dotDir = if "." `isPrefixOf` rootPathDir then "." else ""
112119
ms = GHC.pm_mod_summary pm
113120
file = GHC.ms_hspp_file ms
114121
mod' = GHC.ms_mod ms
122+
-- ./src/A for file ./src/A/B.daml
115123
rootPathDir = takeDirectory file
124+
-- A for module A.B
116125
rootModDir = takeDirectory . moduleNameSlashes . GHC.moduleName $ mod'
117126

118127
-- | An HscEnv with equality.

0 commit comments

Comments
 (0)