Skip to content

Commit

Permalink
Only read --rcfile once, and skip search if unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
koalaman committed Feb 3, 2024
1 parent 1bce426 commit 6a44a19
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions shellcheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ data Options = Options {
sourcePaths :: [FilePath],
formatterOptions :: FormatterOptions,
minSeverity :: Severity,
rcfile :: FilePath
rcfile :: Maybe FilePath
}

defaultOptions = Options {
Expand All @@ -88,7 +88,7 @@ defaultOptions = Options {
foColorOption = ColorAuto
},
minSeverity = StyleC,
rcfile = []
rcfile = Nothing
}

usageHeader = "Usage: shellcheck [OPTIONS...] FILES..."
Expand Down Expand Up @@ -374,7 +374,7 @@ parseOption flag options =

Flag "rcfile" str -> do
return options {
rcfile = str
rcfile = Just str
}

Flag "enable" value ->
Expand Down Expand Up @@ -453,21 +453,31 @@ ioInterface options files = do


-- Returns the name and contents of .shellcheckrc for the given file
getConfig cache filename = do
contents <- readConfig (rcfile options)
if isNothing contents
then 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
else return contents
getConfig cache filename =
case rcfile options of
Just file -> do
-- We have a specified rcfile. Ignore normal rcfile resolution.
(path, result) <- readIORef cache
if path == "/"
then return result
else do
result <- readConfig file
when (isNothing result) $
hPutStrLn stderr $ "Warning: unable to read --rcfile " ++ file
writeIORef cache ("/", result)
return result

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 =
case paths of
Expand Down Expand Up @@ -505,7 +515,7 @@ ioInterface options files = do
where
handler :: FilePath -> IOException -> IO (String, Bool)
handler file err = do
putStrLn $ file ++ ": " ++ show err
hPutStrLn stderr $ file ++ ": " ++ show err
return ("", True)

andM a b arg = do
Expand Down

0 comments on commit 6a44a19

Please sign in to comment.