Skip to content

Commit

Permalink
[builtin/source] Lookup files to be sourced in $PATH (#393)
Browse files Browse the repository at this point in the history
- Gives precendence to $PATH before sourcing files in current directory

TIL: sourcing files in the current directory is a bash-ism, not part of POSIX

Addresses issue #389.
  • Loading branch information
jyn514 authored and andychu committed Jul 4, 2019
1 parent 4ce7d26 commit 38f1254
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion osh/cmd_exec.py
Expand Up @@ -220,8 +220,11 @@ def _Source(self, arg_vec):
except IndexError:
raise args.UsageError('missing required argument')

(_, resolved) = builtin._ResolveFile(path, self.mem.GetVar('PATH').s.split(':'))
if resolved is None:
resolved = path
try:
f = self.fd_state.Open(path) # Shell can't use descriptors 3-9
f = self.fd_state.Open(resolved) # Shell can't use descriptors 3-9
except OSError as e:
self.errfmt.Print('source %r failed: %s', path, posix.strerror(e.errno),
span_id=arg_vec.spids[1])
Expand Down
20 changes: 20 additions & 0 deletions spec/builtin-eval-source.test.sh
Expand Up @@ -135,3 +135,23 @@ FALSE
FALSE
## END

#### source works for files in current directory
echo "echo current dir" > cmd
. cmd
rm cmd
## STDOUT:
current dir
## N-I dash stdout-json: ""
## N-I dash status: 2
## N-I mksh stdout-json: ""
## N-I mksh status: 1

#### source gives precendence to PATH
mkdir -p dir
echo "echo path" > dir/cmd
echo "echo current dir" > cmd
PATH="dir:$PATH"
. cmd
rm -r dir cmd
## STDOUT:
path
17 changes: 17 additions & 0 deletions spec/posix.test.sh
Expand Up @@ -142,3 +142,20 @@ three
EOF2
## stdout-json: "one\ntwo\nthree\n"

#### source works for files in subdirectory
mkdir -p dir
echo "echo path" > dir/cmd
. dir/cmd
rm dir/cmd
## STDOUT:
path

#### source looks in PATH for files
mkdir -p dir
echo "echo hi" > dir/cmd
PATH="dir:$PATH"
. cmd
rm dir/cmd
## STDOUT:
hi
## END

0 comments on commit 38f1254

Please sign in to comment.