Treat $x/ or $(x)/ as ./ when finding sourced files (fixes #1998)

This commit is contained in:
Vidar Holen 2020-06-28 17:24:07 -07:00
parent 1b884a17ea
commit 210cdcd01a
4 changed files with 25 additions and 1 deletions

View File

@ -8,6 +8,9 @@
- SC1072/SC1073 now respond to disable annotations, though ignoring parse errors
is still purely cosmetic and does not allow ShellCheck to continue.
### Changed
- SC1090: A leading `$x/` or `$(x)/` is now treated as `./` when locating files
## v0.7.1 - 2020-04-04
### Fixed

View File

@ -503,6 +503,11 @@ isCommandSubstitution t = case t of
T_Backticked {} -> True
_ -> False
-- Is this an expansion that results in a simple string?
isStringExpansion t = isCommandSubstitution t || case t of
T_DollarArithmetic {} -> True
T_DollarBraced {} -> not (isArrayExpansion t)
_ -> False
-- Is this a T_Annotation that ignores a specific code?
isAnnotationIgnoringCode code t =

View File

@ -229,6 +229,12 @@ prop_cantSourceDynamic =
prop_cantSourceDynamic2 =
[1090] == checkWithIncludes [("lib", "")] "source ~/foo"
prop_canStripPrefixAndSource =
null $ checkWithIncludes [("./lib", "")] "source \"$MYDIR/lib\""
prop_canStripPrefixAndSource2 =
null $ checkWithIncludes [("./utils.sh", "")] "source \"$(dirname \"${BASH_SOURCE[0]}\")/utils.sh\""
prop_canSourceDynamicWhenRedirected =
null $ checkWithIncludes [("lib", "")] "#shellcheck source=lib\n. \"$1\""

View File

@ -2122,7 +2122,7 @@ readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file':rest'))) = d
let file = getFile file' rest'
override <- getSourceOverride
let literalFile = do
name <- override `mplus` getLiteralString file
name <- override `mplus` getLiteralString file `mplus` stripDynamicPrefix file
-- Hack to avoid 'source ~/foo' trying to read from literal tilde
guard . not $ "~/" `isPrefixOf` name
return name
@ -2182,6 +2182,16 @@ readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file':rest'))) = d
SourcePath x -> Just x
_ -> Nothing
-- If the word has a single expansion as the directory, try stripping it
-- This affects `$foo/bar` but not `${foo}-dir/bar` or `/foo/$file`
stripDynamicPrefix word =
case getWordParts word of
exp : rest | isStringExpansion exp -> do
str <- getLiteralString (T_NormalWord (Id 0) rest)
guard $ "/" `isPrefixOf` str
return $ "." ++ str
_ -> Nothing
subRead name script =
withContext (ContextSource name) $
inSeparateContext $