poetry icon indicating copy to clipboard operation
poetry copied to clipboard

Actually switch directory with --directory/-C

Open brandonchinn178 opened this issue 2 years ago • 12 comments

  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [x] I have searched the FAQ and general documentation and believe that my question is not already covered.

Feature Request

Previous issues have been raised asking about the behavior of --directory: (https://github.com/python-poetry/poetry/issues/7363, https://github.com/python-poetry/poetry/issues/7507). They have both been closed as "working as expected", so this issue is an official request to change the behavior OR change the flag ~OR update the docs~.

Most GNU tools like make or git will make git -C dir ... equivalent to (cd dir && git ...). Using the same flag implies the same behavior, when it's not currently the case:

this is expected. Poetry just loads the pyproject.toml from the given directory and does not switch to the directory. https://github.com/python-poetry/poetry/issues/7363#issuecomment-1386833452

Ideally, the behavior would be changed to actually change the directory; I don't see why a simple addition of os.chdir in the beginning of the CLI main function would break things.

brandonchinn178 avatar May 09 '23 19:05 brandonchinn178

You don't need to make an "official request" to fix the docs, just submit a merge request - make it happen yourself!

(Reporters in both of the issues that you linked were also invited to fix the docs)

dimbleby avatar May 09 '23 19:05 dimbleby

Sure, but ideally the behavior would be changed, so this is a request mostly for that

brandonchinn178 avatar May 09 '23 20:05 brandonchinn178

I think a docs clarification is more likely to succeed - two issues declaring this working-as-designed tells its own story - but: same deal. If you care about this and want it to happen, your best shot is to do it yourself.

dimbleby avatar May 09 '23 20:05 dimbleby

I know the issues were closed as "working-as-designed", but I don't actually think that's the case. The original PR (https://github.com/python-poetry/poetry/pull/6810) says it closes https://github.com/python-poetry/poetry/issues/2179, but that issue is asking for the same behavior as this issue, which isn't solved by the current behavior of -C

brandonchinn178 avatar May 09 '23 20:05 brandonchinn178

Imo it should not change the directory, because this would defy the whole option at all. It is especially useful to run a script entrypoint via poetry run your-script from a different folder and still be able to pass files relative to your current directory as arguments that are not relative to the pyproject.toml's directory.

So yes, the feature is working as designed I would assume.

Makman2 avatar Jun 07 '23 17:06 Makman2

@Makman2 I assume you meant poetry -C ../other/dir/ run entry-point?

I don't think that should be a supported usecase. If you're using another poetry project as a tool for another project, you should add it as a dependency, which will register the entrypoint in the second project so you can just do poetry run entry-point project2-file in the second project

brandonchinn178 avatar Jun 07 '23 17:06 brandonchinn178

The second project might not be a Python project, and general-purpose tools/helpers not required for building or similar should not be listed in a requirements section normally.

Without this usecase you don't have a chance to sanely invoke a Python entrypoint via poetry and reference any files on command line.

Makman2 avatar Jun 07 '23 17:06 Makman2

If it's a poetry project, you should certainly add a dev group (or similar) for tools. If it's a non-poetry python project, you should install it to the environment somehow (e.g. requirements-dev.txt or just manually install into the venv venv/bin/pip install ../other/dir)

If it's not a python project, IMO you should install it the same as any other third-party dev tool you use. But as a workaround, you could always do (or write a script to do)

$(poetry -C ../other/dir/ run which entry-point) file1 file2

brandonchinn178 avatar Jun 07 '23 19:06 brandonchinn178

I also don't understand why is poetry run exception in how it actually handles -C / --directory flag.

When I am outside of MY_PROJECT_DIR and use

  • poetry --directory=MY_PROJECT_DIR version

it works as expected. But when I run

  • poetry --directory=MY_PROJECT_DIR run MY_COMMAND

it fails with

Poetry could not find a pyproject.toml file in CURRENT_DIR or its parents

Same error as if I wouldn't use flag -C / --directory at all - it's ignored.

MichalVasut avatar Jul 14 '23 05:07 MichalVasut

Is there something blocking solution to this issue or is it just too low priority on everyone's rosters?

rafalkrupinski avatar Jun 10 '24 12:06 rafalkrupinski

Still does not work as of version 1.8.3

aa956@desktop02:~/tmp$ ~/projects/toolkit/generate-mask.sh 

Poetry could not find a pyproject.toml file in /home/aa956/tmp or its parents
aa956@desktop02:~/tmp$ cat ~/projects/toolkit/generate-mask.sh 
#!/bin/bash
PROJECTPATH=~/projects/toolkit

poetry run --directory=$PROJECTPATH python $PROJECTPATH/mask/florence2_sam2_image_mask.py "$@"

aa956@desktop02:~/tmp$ ls ~/projects/toolkit/*.toml
/home/aa956/projects/toolkit/poetry.toml
/home/aa956/projects/toolkit/pyproject.toml

aa956@desktop02:~/tmp$ poetry --version
Poetry (version 1.8.3)

aa956@desktop02:~/tmp$ 

According to the documentation:

  • --directory=DIRECTORY (-C): The working directory for the Poetry command (defaults to the current working directory).

Edit: Works with space between --directory and /actual/path and does not work as documented with = between parameter and it's value.

aa956 avatar Sep 01 '24 08:09 aa956

This looks like two bugs then - both that poetry run ignores --directory/-C and that the real usage requires not including = as presented in the docs

And then the overarching issue that --directory/-C is just a sort of --config-file that fetches a pyproject.toml in a different directory, and doesn't actually change the working directory (as documented).

marcospgp avatar Sep 17 '24 18:09 marcospgp

Hey everyone,

because the current behavior seems to confuse several users we are thinking about the following change:

  1. --directory / -C will actually change the current working directory. So any relative path will be relative to the given directory.
  2. In addition we provide a --project flag, which will work exactly as the --directory flag now. Meaning the pyproject.toml file will be read from the given directory, but any relative path will be interpreted relative to the place where one start the command.

What do you think? Will this meet your needs?

fin swimmer

finswimmer avatar Nov 06 '24 10:11 finswimmer

Thank you @finswimmer!! Love seeing a resolution to this long-lived issue

brandonchinn178 avatar Nov 26 '24 06:11 brandonchinn178

This issue 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[bot] avatar Dec 27 '24 00:12 github-actions[bot]