Skip to content

Commit

Permalink
fix: pdm run conflicts between local file and PATH (#2223)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Sep 1, 2023
1 parent 7a740a6 commit 1adb852
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/2221.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`pdm run` should only find local file if the command starts with `./`.
7 changes: 5 additions & 2 deletions src/pdm/cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ def get_task(self, script_name: str) -> Task | None:

def expand_command(self, command: str) -> str:
expanded_command = os.path.expanduser(os.path.expandvars(command))
if os.path.exists(expanded_command):
return os.path.abspath(expanded_command)
if expanded_command.replace(os.sep, "/").startswith(("./", "../")):
abspath = os.path.abspath(expanded_command)
if not os.path.isfile(abspath):
raise PdmUsageError(f"Command [success]'{command}'[/] is not a valid executable.")
return abspath
result = self.project.environment.which(command)
if not result:
raise PdmUsageError(f"Command [success]'{command}'[/] is not found in your PATH.")
Expand Down
7 changes: 7 additions & 0 deletions tests/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ def test_run_script_with_relative_path(project, pdm, capfd):
assert out.strip() == "Hello"


def test_run_non_existing_local_script(project, pdm):
with cd(project.root):
result = pdm(["run", "./test_script.sh"], obj=project)
assert result.exit_code != 0
assert "not a valid executable" in result.stderr


@pytest.mark.parametrize(
"args,expected",
(
Expand Down

0 comments on commit 1adb852

Please sign in to comment.