diff --git a/news/fix_locate_file.rst b/news/fix_locate_file.rst new file mode 100644 index 000000000..5acdc6037 --- /dev/null +++ b/news/fix_locate_file.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* Fixed wrong order of directories in PATH (#5606 #5605). Please do not use 0.18.0 and 0.18.1 versions. + +**Security:** + +* diff --git a/tests/aliases/__init__.py b/tests/aliases/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/procs/test_executables.py b/tests/procs/test_executables.py index aa27c1426..161a2fb25 100644 --- a/tests/procs/test_executables.py +++ b/tests/procs/test_executables.py @@ -24,18 +24,26 @@ def test_get_paths(tmpdir): def test_locate_executable(tmpdir, xession): - bindir = tmpdir.mkdir("bindir") - bindir.mkdir("subdir") + bindir1 = tmpdir.mkdir("bindir1") + bindir2 = tmpdir.mkdir("bindir2") + bindir3 = tmpdir.mkdir("bindir3") + bindir2.mkdir("subdir") executables = ["file1.EXE", "file2.COM", "file2.EXE", "file3"] not_executables = ["file4.EXE", "file5"] for exefile in executables + not_executables: - f = bindir / exefile + f = bindir2 / exefile f.write_text("binary", encoding="utf8") if exefile in executables: os.chmod(f, 0o777) + # Test overlapping file names in different bin directories. + (f := bindir3 / "file3").write_text("binary", encoding="utf8") + os.chmod(f, 0o777) + pathext = [".EXE", ".COM"] if ON_WINDOWS else [] - with xession.env.swap(PATH=str(bindir), PATHEXT=pathext): + with xession.env.swap( + PATH=[str(bindir1), str(bindir2), str(bindir3)], PATHEXT=pathext + ): assert locate_executable("file1.EXE") assert locate_executable("nofile") is None assert locate_executable("file5") is None @@ -45,7 +53,7 @@ def test_locate_executable(tmpdir, xession): assert locate_executable("file4") assert locate_executable("file2").endswith("file2.exe") else: - assert locate_executable("file3") + assert locate_executable("file3").find("bindir2") > 0 assert locate_executable("file1") is None assert locate_executable("file4") is None assert locate_executable("file2") is None diff --git a/xonsh/procs/executables.py b/xonsh/procs/executables.py index b26131111..cfe2af50d 100644 --- a/xonsh/procs/executables.py +++ b/xonsh/procs/executables.py @@ -89,7 +89,7 @@ def locate_file(name, env=None, check_executable=False, use_pathext=False): """ env = env if env is not None else XSH.env env_path = env.get("PATH", []) - paths = tuple(reversed(tuple(clear_paths(env_path)))) + paths = tuple(tuple(clear_paths(env_path))) possible_names = get_possible_names(name, env) if use_pathext else [name] for path, possible_name in itertools.product(paths, possible_names):