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

@ -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 $