Only read --rcfile once, and skip search if unavailable

This commit is contained in:
Vidar Holen 2024-02-03 13:34:49 -08:00
parent 1bce426fcf
commit 6a44a19f17
1 changed files with 29 additions and 19 deletions

View File

@ -77,7 +77,7 @@ data Options = Options {
sourcePaths :: [FilePath], sourcePaths :: [FilePath],
formatterOptions :: FormatterOptions, formatterOptions :: FormatterOptions,
minSeverity :: Severity, minSeverity :: Severity,
rcfile :: FilePath rcfile :: Maybe FilePath
} }
defaultOptions = Options { defaultOptions = Options {
@ -88,7 +88,7 @@ defaultOptions = Options {
foColorOption = ColorAuto foColorOption = ColorAuto
}, },
minSeverity = StyleC, minSeverity = StyleC,
rcfile = [] rcfile = Nothing
} }
usageHeader = "Usage: shellcheck [OPTIONS...] FILES..." usageHeader = "Usage: shellcheck [OPTIONS...] FILES..."
@ -374,7 +374,7 @@ parseOption flag options =
Flag "rcfile" str -> do Flag "rcfile" str -> do
return options { return options {
rcfile = str rcfile = Just str
} }
Flag "enable" value -> Flag "enable" value ->
@ -453,21 +453,31 @@ ioInterface options files = do
-- Returns the name and contents of .shellcheckrc for the given file -- Returns the name and contents of .shellcheckrc for the given file
getConfig cache filename = do getConfig cache filename =
contents <- readConfig (rcfile options) case rcfile options of
if isNothing contents Just file -> do
then do -- We have a specified rcfile. Ignore normal rcfile resolution.
path <- normalize filename (path, result) <- readIORef cache
let dir = takeDirectory path if path == "/"
(previousPath, result) <- readIORef cache then return result
if dir == previousPath else do
then return result result <- readConfig file
else do when (isNothing result) $
paths <- getConfigPaths dir hPutStrLn stderr $ "Warning: unable to read --rcfile " ++ file
result <- findConfig paths writeIORef cache ("/", result)
writeIORef cache (dir, result) return result
return result
else return contents Nothing -> do
path <- normalize filename
let dir = takeDirectory path
(previousPath, result) <- readIORef cache
if dir == previousPath
then return result
else do
paths <- getConfigPaths dir
result <- findConfig paths
writeIORef cache (dir, result)
return result
findConfig paths = findConfig paths =
case paths of case paths of
@ -505,7 +515,7 @@ ioInterface options files = do
where where
handler :: FilePath -> IOException -> IO (String, Bool) handler :: FilePath -> IOException -> IO (String, Bool)
handler file err = do handler file err = do
putStrLn $ file ++ ": " ++ show err hPutStrLn stderr $ file ++ ": " ++ show err
return ("", True) return ("", True)
andM a b arg = do andM a b arg = do