Skip to content

Commit e2d93ef

Browse files
committed
Fix cwd
1 parent e1e57d0 commit e2d93ef

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

hls-test-utils/src/Test/Hls.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ silenceStderr action = withTempFile $ \temp ->
8585
hSetBuffering stderr buf
8686
hClose old
8787

88+
-- | Restore cwd after running an action
89+
keepCurrentDirectory :: IO a -> IO a
90+
keepCurrentDirectory = bracket getCurrentDirectory setCurrentDirectory . const
91+
8892
-- | Host a server, and run a test session on it
8993
-- Note: cwd will be shifted into @root@ in @Session a@
9094
runSessionWithServer' ::
@@ -98,11 +102,9 @@ runSessionWithServer' ::
98102
FilePath ->
99103
Session a ->
100104
IO a
101-
runSessionWithServer' plugin conf sconf caps root s = silenceStderr $ do
105+
runSessionWithServer' plugin conf sconf caps root s = keepCurrentDirectory $ do
102106
(inR, inW) <- createPipe
103107
(outR, outW) <- createPipe
104-
-- restore cwd after running the session; otherwise the path to test data will be invalid
105-
cwd <- getCurrentDirectory
106108
server <-
107109
async $
108110
Ghcide.defaultMain
@@ -116,11 +118,12 @@ runSessionWithServer' plugin conf sconf caps root s = silenceStderr $ do
116118
in ideOptions {optShakeOptions = (optShakeOptions ideOptions) {shakeThreads = 2}},
117119
argsHlsPlugins = pluginDescToIdePlugins $ plugin ++ Ghcide.descriptors
118120
}
119-
120-
x <-
121-
runSessionWithHandles inW outR sconf caps root s
122-
`finally` setCurrentDirectory cwd
121+
x <- runSessionWithHandles inW outR sconf caps root s
123122
timeout 3 (wait server) >>= \case
124123
Just () -> pure ()
125-
Nothing -> putStrLn "Server does not exit on time, canceling the async task..." >> cancel server
124+
Nothing -> do
125+
putStrLn "Server does not exit in 3s, canceling the async task..."
126+
(t, _) <- duration $ cancel server
127+
putStrLn $ "Finishing canceling (took " <> showDuration t <> "s)"
128+
sleep 0.1
126129
pure x

0 commit comments

Comments
 (0)