From d561073727186c7b456c9ef113ccb7fc0df4560e Mon Sep 17 00:00:00 2001
From: Claudio Bley <claudio.bley@tweag.io>
Date: Thu, 16 Nov 2023 16:23:28 +0100
Subject: [PATCH] Only exit ghci in -e mode when :add command fails

Previously, when running `ghci -e ':add Sample.hs'` the process would
exit with exit code 1 if the file exists and could be loaded.

Fixes #24115
---
 ghc/GHCi/UI.hs                                | 5 +++--
 testsuite/tests/ghci/should_run/T24115.hs     | 2 ++
 testsuite/tests/ghci/should_run/T24115.script | 1 +
 testsuite/tests/ghci/should_run/all.T         | 3 +++
 4 files changed, 9 insertions(+), 2 deletions(-)
 create mode 100644 testsuite/tests/ghci/should_run/T24115.hs
 create mode 100644 testsuite/tests/ghci/should_run/T24115.script

diff --git a/ghc/GHCi/UI.hs b/ghc/GHCi/UI.hs
index 258e34bc660b..69f3d31abbfa 100644
--- a/ghc/GHCi/UI.hs
+++ b/ghc/GHCi/UI.hs
@@ -2098,8 +2098,9 @@ addModule files = do
     checkTargetFile :: GhciMonad m => String -> m Bool
     checkTargetFile f = do
       exists <- liftIO (doesFileExist f)
-      unless exists $ liftIO $ hPutStrLn stderr $ "File " ++ f ++ " not found"
-      failIfExprEvalMode
+      unless exists $ do
+        liftIO $ hPutStrLn stderr $ "File " ++ f ++ " not found"
+        failIfExprEvalMode
       return exists
 
 -- | @:unadd@ command
diff --git a/testsuite/tests/ghci/should_run/T24115.hs b/testsuite/tests/ghci/should_run/T24115.hs
new file mode 100644
index 000000000000..d328e59028c2
--- /dev/null
+++ b/testsuite/tests/ghci/should_run/T24115.hs
@@ -0,0 +1,2 @@
+
+loaded = True
diff --git a/testsuite/tests/ghci/should_run/T24115.script b/testsuite/tests/ghci/should_run/T24115.script
new file mode 100644
index 000000000000..28aa6232e086
--- /dev/null
+++ b/testsuite/tests/ghci/should_run/T24115.script
@@ -0,0 +1 @@
+loaded
diff --git a/testsuite/tests/ghci/should_run/all.T b/testsuite/tests/ghci/should_run/all.T
index 9a73da55f48a..4f56d1e97170 100644
--- a/testsuite/tests/ghci/should_run/all.T
+++ b/testsuite/tests/ghci/should_run/all.T
@@ -92,3 +92,6 @@ test('T22958b', just_ghci, compile_and_run, [''])
 test('T22958c', just_ghci, compile_and_run, [''])
 test('GhciMainIs', just_ghci, compile_and_run, ['-main-is otherMain'])
 test('LargeBCO', [extra_files(['LargeBCO_A.hs']), req_interp, extra_hc_opts("-O -fbyte-code-and-object-code -fprefer-byte-code")], compile_and_run, [''])
+
+test('T24115', just_ghci + [extra_run_opts("-e ':add T24115.hs'")], ghci_script, ['T24115.script'])
+
-- 
GitLab