Skip to content

Comments

Make run_script args[0] the absolute path to the script being called#6971

Closed
logan-bobo wants to merge 3 commits intopython-poetry:masterfrom
logan-bobo:fix/run-script-absolute-path
Closed

Make run_script args[0] the absolute path to the script being called#6971
logan-bobo wants to merge 3 commits intopython-poetry:masterfrom
logan-bobo:fix/run-script-absolute-path

Conversation

@logan-bobo
Copy link
Member

@logan-bobo logan-bobo commented Nov 4, 2022

Pull Request Check List

Resolves: #2435

  • [] Added tests for changed code.
  • Updated documentation for changed code <- This would not produce a change in the documentation as it is not altering the core way the run command functions only deal with edge cases of its use.

When a poetry user wants to run tools with auto-reload such as the kind found in Django's runserver through the poetry run functionality the auto-reload will collect the original command again in our case it works as poetry run script will look up in the config where the script is to be run from however when called again will produce an error as found in #2435. This PR aims to resolve that issue.

@logan-bobo logan-bobo changed the title making args[0] the absolute path to the script Make run_script args[0] the absolute path to the script being called Nov 6, 2022
@logan-bobo logan-bobo self-assigned this Nov 7, 2022
@logan-bobo logan-bobo added area/cli Related to the command line kind/bug Something isn't working as expected labels Nov 7, 2022
def run_script(self, script: str | dict[str, str], args: str) -> int:
def run_script(self, script: str | dict[str, str], args: list[str]) -> int:
env = EnvManager(self.poetry).get()
args[0] = f"{env.path}/bin/{args[0]}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am 90% sure that the path will be different for windows. IIRC Windows uses Scripts directory in venvs (please verify it).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Secrus thanks for the feedback. I have taken a look and you are right Windows uses the Scripts directory in venvs. I have updated my branch so it will build out the absolute path based on platform. Please see the latest update.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like we should add a test that fails if the path is wrong. (In general, we should always try to add a test for a bugfix.)

Further, it might be better to use Env.script_dirs than building the path manually. However, I'm not sure which directory to choose if there is more than one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take a look in to env.script_dirs and also look to add tests to this PR thanks for the feedback 😅

Copy link
Member Author

@logan-bobo logan-bobo Nov 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@radoering it looks as if changing to env.script_dirs[0] works on Linux but not on windows so I am going to leave the paths as {env.path}\Scripts\{args[0]} for Windows and {env.path}/bin/{args[0]} for Linux/MacOS as we know this is where the script will exist defined in tool.poetry.scripts. Will work on extending the MR with test cases now 👐

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the issue with script_dirs on Windows?

What I'm actually wondering is if we have to check which folder is the correct one if there are multiple entries in script_dirs or can we be sure that it's script_dirs[0] (or maybe script_dirs[-1])? It might be relevant if env is a VirtualEnv or a SystemEnv.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to

for scripts_path in self._env.script_dirs:
if is_dir_writable(path=scripts_path, create=True):
break
else:
self._io.write_error_line(
" - Failed to find a suitable script installation directory for"
f" {self._poetry.file.parent}"
)
return []
scripts are written to the first writable folder in script_dirs. Thus, we really should use script_dirs and have to check which is the correct folder.

@logan-bobo logan-bobo requested a review from Secrus November 8, 2022 18:05
@radoering
Copy link
Member

Closing this one in favor of #6737 because #6737 has been created before this one and progressed further.

@radoering radoering closed this Jan 21, 2023
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area/cli Related to the command line kind/bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

poetry run script doesn't run the actual executable script

4 participants