Skip to content

Commit 3983d97

Browse files
authored
Fix ghcide handling project root (#2543)
1 parent ff0ccd6 commit 3983d97

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

ghcide/exe/Main.hs

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Main(main) where
77

88
import Arguments (Arguments (..),
99
getArguments)
10-
import Control.Monad.Extra (unless, whenJust)
10+
import Control.Monad.Extra (unless)
1111
import Data.Default (def)
1212
import Data.Version (showVersion)
1313
import Development.GitRev (gitHash)
@@ -50,13 +50,17 @@ main = withTelemetryLogger $ \telemetryLogger -> do
5050
if argsVersion then ghcideVersion >>= putStrLn >> exitSuccess
5151
else hPutStrLn stderr {- see WARNING above -} =<< ghcideVersion
5252

53-
whenJust argsCwd IO.setCurrentDirectory
53+
-- if user uses --cwd option we need to make this path absolute (and set the current directory to it)
54+
argsCwd <- case argsCwd of
55+
Nothing -> IO.getCurrentDirectory
56+
Just root -> IO.setCurrentDirectory root >> IO.getCurrentDirectory
5457

5558
let logPriority = if argsVerbose then Debug else Info
5659
arguments = if argsTesting then Main.testing else Main.defaultArguments logPriority
5760

5861
Main.defaultMain arguments
59-
{Main.argCommand = argsCommand
62+
{ Main.argsProjectRoot = Just argsCwd
63+
, Main.argCommand = argsCommand
6064
,Main.argsLogger = Main.argsLogger arguments <> pure telemetryLogger
6165

6266
,Main.argsRules = do

ghcide/src/Development/IDE/Main.hs

+16-12
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ import Text.Printf (printf)
124124

125125
data Command
126126
= Check [FilePath] -- ^ Typecheck some paths and print diagnostics. Exit code is the number of failures
127-
| Db {projectRoot :: FilePath, hieOptions :: HieDb.Options, hieCommand :: HieDb.Command}
127+
| Db {hieOptions :: HieDb.Options, hieCommand :: HieDb.Command}
128128
-- ^ Run a command in the hiedb
129129
| LSP -- ^ Run the LSP server
130130
| PrintExtensionSchema
131131
| PrintDefaultConfig
132-
| Custom {projectRoot :: FilePath, ideCommand :: IdeCommand IdeState} -- ^ User defined
132+
| Custom {ideCommand :: IdeCommand IdeState} -- ^ User defined
133133
deriving Show
134134

135135

@@ -144,7 +144,7 @@ isLSP _ = False
144144
commandP :: IdePlugins IdeState -> Parser Command
145145
commandP plugins =
146146
hsubparser(command "typecheck" (info (Check <$> fileCmd) fileInfo)
147-
<> command "hiedb" (info (Db "." <$> HieDb.optParser "" True <*> HieDb.cmdParser <**> helper) hieInfo)
147+
<> command "hiedb" (info (Db <$> HieDb.optParser "" True <*> HieDb.cmdParser <**> helper) hieInfo)
148148
<> command "lsp" (info (pure LSP <**> helper) lspInfo)
149149
<> command "vscode-extension-schema" extensionSchemaCommand
150150
<> command "generate-default-config" generateDefaultConfigCommand
@@ -163,13 +163,14 @@ commandP plugins =
163163
(fullDesc <> progDesc "Print config supported by the server with default values")
164164

165165
pluginCommands = mconcat
166-
[ command (T.unpack pId) (Custom "." <$> p)
166+
[ command (T.unpack pId) (Custom <$> p)
167167
| (PluginId pId, PluginDescriptor{pluginCli = Just p}) <- ipMap plugins
168168
]
169169

170170

171171
data Arguments = Arguments
172-
{ argsOTMemoryProfiling :: Bool
172+
{ argsProjectRoot :: Maybe FilePath
173+
, argsOTMemoryProfiling :: Bool
173174
, argCommand :: Command
174175
, argsLogger :: IO Logger
175176
, argsRules :: Rules ()
@@ -191,7 +192,8 @@ instance Default Arguments where
191192

192193
defaultArguments :: Priority -> Arguments
193194
defaultArguments priority = Arguments
194-
{ argsOTMemoryProfiling = False
195+
{ argsProjectRoot = Nothing
196+
, argsOTMemoryProfiling = False
195197
, argCommand = LSP
196198
, argsLogger = stderrLogger priority
197199
, argsRules = mainRule def >> action kick
@@ -319,7 +321,7 @@ defaultMain Arguments{..} = flip withHeapStats fun =<< argsLogger
319321
hieChan
320322
dumpSTMStats
321323
Check argFiles -> do
322-
dir <- IO.getCurrentDirectory
324+
dir <- maybe IO.getCurrentDirectory return argsProjectRoot
323325
dbLoc <- getHieDbLoc dir
324326
runWithDb logger dbLoc $ \hiedb hieChan -> do
325327
-- GHC produces messages with UTF8 in them, so make sure the terminal doesn't error
@@ -382,17 +384,19 @@ defaultMain Arguments{..} = flip withHeapStats fun =<< argsLogger
382384
measureMemory logger [keys] consoleObserver values
383385

384386
unless (null failed) (exitWith $ ExitFailure (length failed))
385-
Db dir opts cmd -> do
386-
dbLoc <- getHieDbLoc dir
387+
Db opts cmd -> do
388+
root <- maybe IO.getCurrentDirectory return argsProjectRoot
389+
dbLoc <- getHieDbLoc root
387390
hPutStrLn stderr $ "Using hiedb at: " ++ dbLoc
388-
mlibdir <- setInitialDynFlags logger dir def
391+
mlibdir <- setInitialDynFlags logger root def
389392
rng <- newStdGen
390393
case mlibdir of
391394
Nothing -> exitWith $ ExitFailure 1
392395
Just libdir -> retryOnSqliteBusy logger rng (HieDb.runCommand libdir opts{HieDb.database = dbLoc} cmd)
393396

394-
Custom projectRoot (IdeCommand c) -> do
395-
dbLoc <- getHieDbLoc projectRoot
397+
Custom (IdeCommand c) -> do
398+
root <- maybe IO.getCurrentDirectory return argsProjectRoot
399+
dbLoc <- getHieDbLoc root
396400
runWithDb logger dbLoc $ \hiedb hieChan -> do
397401
vfs <- makeVFSHandle
398402
sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions "."

0 commit comments

Comments
 (0)