Index: utils/lit/lit/ShCommands.py =================================================================== --- utils/lit/lit/ShCommands.py +++ utils/lit/lit/ShCommands.py @@ -52,10 +52,18 @@ import glob import os if os.path.isabs(self.pattern): - abspath = self.pattern + results = glob.glob(self.pattern) else: - abspath = os.path.join(cwd, self.pattern) - results = glob.glob(abspath) + # For a non-absolute pattern, we want to match "relative to + # cwd"; unfortunately glob.glob() will only match relative to + # the process working directory, or else an absolute path. So + # rather than change working directory to and from cwd in order + # to glob, we extend the relative pattern to an absolute + # pattern, then glob, then map the results back to relative + # paths using relpath. + abs_pat = os.path.join(cwd, self.pattern) + abs_glob = glob.glob(abs_pat) + results = [os.path.relpath(f, cwd) for f in abs_glob] return [self.pattern] if len(results) == 0 else results class Pipeline: Index: utils/lit/tests/shtest-glob-relative.py =================================================================== --- /dev/null +++ utils/lit/tests/shtest-glob-relative.py @@ -0,0 +1,8 @@ +# Here we're checking that glob-expanding a relative glob within a "cd ..." does not +# wind up prepending the working directory absolute path to the glob expansion. +# +# RUN: mkdir -p %t.dir/test/deep/path +# RUN: touch %t.dir/test/deep/path/file.txt +# RUN: cd %t.dir/test/deep/path && ls file.* > %t.out +# RUN: FileCheck --input-file %t.out %s +# CHECK-NOT: deep/path