astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.52k stars 463 forks source link

✨ Allow passing cwd in `tool.rye.scripts` #930

Open jd-solanki opened 6 months ago

jd-solanki commented 6 months ago

Hi 👋🏻

I'm using tool.rye.scripts for running django server from root of the project and it looks like below:

[tool.rye.scripts]
dev = "python src/django_marketplace/manage.py runserver"

Problem

If I'm already inside src dir and run rye run dev then it gives error due to incorrect dir I'm in.

Solution

Provide cwd option so we can run rye scripts from anywhere in the project

dev = { cmd = "python manage.py runserver", cwd: "src/django_marketplace" }

Another benefit can be cmd option will be much cleaner.

j178 commented 6 months ago

I checked PDM and pipenv, neither support setting cwd. We might just need to set the cwd to the project root.

UPDATE: PDM is going to add a working_dir option in the next version: https://github.com/pdm-project/pdm/pull/2694

j178 commented 6 months ago

And npm run:

Scripts are run from the root of the package folder, regardless of what the current working directory is when npm run is called. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run.