rye icon indicating copy to clipboard operation
rye copied to clipboard

Invoke scripts from the project root

Open j178 opened this issue 11 months ago • 6 comments

Fixes #930

j178 avatar Mar 23 '24 17:03 j178

I have some use cases that require rye run to preserve the current working directory, maybe this could be configurable in that case?

bluss avatar Mar 23 '24 18:03 bluss

@bluss I see three approaches:

  1. Add a cwd option proposed in #930, but it should support some special variabes to support your use cases, for example pwd = { cmd = "pwd", cwd: "${CWD}" }
  2. Defaults invoking scripts from current working directory (current behavior, not recommended). If the cwd option is set, invoking from that instead.
  3. We take npm approach, which sets INIT_CWD env to the current working directory when invoking scripts. Then in your script pwd = { cmd = "bash -c 'echo $INIT_CWD'"}

j178 avatar Mar 23 '24 18:03 j178

I'm not sure what's wrong with the current behaviour. Here's an argument in favour of keeping it.

These two should be equivalent: cmd (with virtualenv active - it's a binary in .venv/bin) and rye run cmd (virtualenv doesn't matter)

They will only be the same if they both behave the same w.r.t cwd.

bluss avatar Mar 23 '24 19:03 bluss

Current behavior makes relative file path in the cmd does not work, see #930 Since scripts are defined in pyproject.toml, it is natural to think that relative path are resloved by the directory containing pyproject.toml. This is the default behavior of pipenv/pdm and npm.

j178 avatar Mar 23 '24 19:03 j178

Note that this function also handles rye run python, rye run cmd where cmd is an executable in the venv too (take for example cmd = pytest). Maybe it makes sense for tool.rye.scripts by itself, yes!

bluss avatar Mar 23 '24 19:03 bluss

I feel like making it run from root is a reasonable default as many scripts will be taking relative file arguments. However I do think that if we change that, then an option should be added to override it like PDM does now. Also I do like what npm does with INIT_CWD, maybe we can add support for this.

mitsuhiko avatar Mar 29 '24 09:03 mitsuhiko