diff --git a/exe/Main.hs b/exe/Main.hs
--- a/exe/Main.hs
+++ b/exe/Main.hs
@@ -1,4 +1,5 @@
 {-# LANGUAGE CPP #-}
+{-# LANGUAGE LambdaCase #-}
 {-# LANGUAGE ViewPatterns #-}
 
 module Main where
@@ -6,6 +7,7 @@
 import qualified Config as GHC
 import Control.Monad
 import Control.Monad.IO.Class
+import Control.Monad.Trans.Except (runExceptT)
 import qualified Data.Text.IO as T
 import DynFlags (unsafeGlobalDynFlags)
 import HsInspect.Imports
@@ -48,8 +50,11 @@
   when (elem "--ghc-version" args) $
     (putStrLn GHC.cProjectVersion) >> exitWith ExitSuccess
 
+  let ghcflags_flags' w = runExceptT (ghcflags_flags w) >>= \case
+        Left err -> (putStrLn err) >> exitWith (ExitFailure 1)
+        Right flags -> pure flags
   flags <- if (elem "--ghcflags" args)
-           then ghcflags_flags $ case args of
+           then ghcflags_flags' $ case args of
              "imports" : file : _ -> Just file
              "types" : file : _ -> Just file
              _ -> Nothing
@@ -62,7 +67,7 @@
           Right j -> putStrLn $ encodeJson unsafeGlobalDynFlags j
         else T.putStrLn $ S.render a
 
-  runGhcAndJamMasterShe flags $ case args of
+  runGhcAndJamMasterShe flags True $ case args of
     "imports" : file : rest -> do
       quals <- imports file
       respond rest quals
diff --git a/hsinspect.cabal b/hsinspect.cabal
--- a/hsinspect.cabal
+++ b/hsinspect.cabal
@@ -1,6 +1,6 @@
 cabal-version: 2.2
 name:          hsinspect
-version:       0.0.15
+version:       0.0.16
 synopsis:      Inspect Haskell source files.
 license:       GPL-3.0-or-later
 license-file:  LICENSE
diff --git a/library/HsInspect/Runner.hs b/library/HsInspect/Runner.hs
--- a/library/HsInspect/Runner.hs
+++ b/library/HsInspect/Runner.hs
@@ -5,7 +5,7 @@
 
 import Control.Monad
 import Control.Monad.IO.Class
-import Control.Monad.Trans.Except (runExceptT)
+import Control.Monad.Trans.Except (ExceptT(..))
 import Data.List (find, isPrefixOf)
 import qualified Data.List as L
 import Data.Maybe (catMaybes)
@@ -22,8 +22,8 @@
 
 -- expects the PWD to be the same as the .cabal file and the PATH to be what the
 -- build tool sees.
-runGhcAndJamMasterShe :: [String] -> Ghc a -> IO a
-runGhcAndJamMasterShe (filterFlags -> flags) work =
+runGhcAndJamMasterShe :: [String] -> Bool -> Ghc a -> IO a
+runGhcAndJamMasterShe (filterFlags -> flags) setTargets work =
   let libdir = (drop 2) <$> find ("-B" `isPrefixOf`) flags
       flags' = filter (not . ("-B" `isPrefixOf`)) flags
    in GHC.runGhc libdir $ do
@@ -38,25 +38,25 @@
          , GHC.fatalWarningFlags = EnumSet.empty
          }
 
-  -- The caller may have provided a list of home modules, but we do not trust
-  -- them because the ghcflags plugin does not keep the flags up to date for
-  -- incremental compiles.
-  let mkTarget m = GHC.Target (GHC.TargetModule m) True Nothing
-  homeModules <- inferHomeModules
-  GHC.setTargets $ mkTarget <$> homeModules
+  when setTargets $ do
+    -- The caller may have provided a list of home modules, but we do not trust
+    -- them because the ghcflags plugin does not keep the flags up to date for
+    -- incremental compiles.
+    let mkTarget m = GHC.Target (GHC.TargetModule m) True Nothing
+    homeModules <- inferHomeModules
+    GHC.setTargets $ mkTarget <$> homeModules
+
   work
 
 -- gets the flags (and sets the environment) from the output of the ghcflags plugin
-ghcflags_flags :: Maybe FilePath -> IO [String]
+ghcflags_flags :: Maybe FilePath -> ExceptT String IO [String]
 ghcflags_flags mf = do
-  from <- maybe getCurrentDirectory pure mf
-  ctx <- runExceptT $ findContext from
-  case ctx of
-    Left err -> error err
-    Right Context{package_dir, ghcflags, ghcpath} -> do
-      setCurrentDirectory package_dir
-      setEnv "PATH" (T.unpack ghcpath)
-      pure $ T.unpack <$> ghcflags
+  from <- liftIO $ maybe getCurrentDirectory pure mf
+  Context{package_dir, ghcflags, ghcpath} <- findContext from
+  liftIO $ do
+    setCurrentDirectory package_dir
+    setEnv "PATH" (T.unpack ghcpath)
+  pure $ T.unpack <$> ghcflags
 
 inferHomeModules :: GHC.GhcMonad m => m [GHC.ModuleName]
 inferHomeModules = do
