From 38f1254f4365f591cbea4e3a8ddbab4c085320db Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 4 Jul 2019 11:54:04 -0500 Subject: [PATCH] [builtin/source] Lookup files to be sourced in $PATH (#393) - 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. --- osh/cmd_exec.py | 5 ++++- spec/builtin-eval-source.test.sh | 20 ++++++++++++++++++++ spec/posix.test.sh | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/osh/cmd_exec.py b/osh/cmd_exec.py index d59fbe89af..1240236256 100755 --- a/osh/cmd_exec.py +++ b/osh/cmd_exec.py @@ -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]) diff --git a/spec/builtin-eval-source.test.sh b/spec/builtin-eval-source.test.sh index f5d5d9f55f..5f4a5b9387 100755 --- a/spec/builtin-eval-source.test.sh +++ b/spec/builtin-eval-source.test.sh @@ -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 diff --git a/spec/posix.test.sh b/spec/posix.test.sh index 3ac757f50c..83439683aa 100644 --- a/spec/posix.test.sh +++ b/spec/posix.test.sh @@ -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