Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands are run from the current path when applying the commit #629

Open
maxdinech opened this issue Feb 14, 2024 · 4 comments
Open

Commands are run from the current path when applying the commit #629

maxdinech opened this issue Feb 14, 2024 · 4 comments
Labels
bug Something isn't working waiting for response We need more details or confirmation

Comments

@maxdinech
Copy link

maxdinech commented Feb 14, 2024

🔧 Summary

If I run a commit in ./ or ./backend/, the results differ. In this case, with Pyright (but other commands are affected too)
The python venv is attached to the backend folder.

Lefthook version

1.6.1

Steps to reproduce

pre-commit:
  commands:
    backend-pyright:
      root: "backend/"
      run: pyright .
cd ./
git commit -m "test from root"
cd backend/
git commit -m "test from backend"

Expected results

Pyright (including config) loaded from the backend in both cases

Actual results

A lot of pyright errors such as Unable to resolve import X in the first case but not in the second

Possible Solution

Make the command run from the path specified in root.

@maxdinech maxdinech added the bug Something isn't working label Feb 14, 2024
@mrexox
Copy link
Member

mrexox commented Feb 15, 2024

Hey! Actually with 1.6.0 release I've mad an effort to make lefthook commands run inside the root. Like in this example

test:
  commands:
    ls:
      run: echo $(ls)

lefthook run test prints files in project root, and if you add some root: subfolder to the ls command it will list files in this subfolder.

Could you please provide some logs and config example, so I could reproduce your issue. An example repo would be even better.

@maxdinech
Copy link
Author

maxdinech commented Feb 15, 2024

The issue seems to stem from where commands are executed in relation to a virtual environment. Commands run from the root directory (./) do not have access to the virtual environment, whereas commands executed within the ./backend directory are run within an activated virtual environment. This difference in execution context leads to command failures.

This setup:

# lefthook.yml
test:
  commands:
    which:
      run: echo $(which python)

with the folder structure

.
├── backend
│   └── .venv
└── lefthook.yml

shows that this is the case depending on the folder I run the git commit (or lefthook run pre-commit) from.

A suggested approach to resolve this issue could be to consider executing the commands as though they were initiated from the root: directory, aiming for a consistent environment across executions.

Or I could just simply setup the python venv at the project root, but this is not really desirable in my case.

@mrexox
Copy link
Member

mrexox commented Feb 16, 2024

Oh, I see, so, looks like python venv runs some hook when you are inside the backend directory, right? When lefthook runs the command it just changes the cwd to backend, but no hooks get applied. All ENVs are taken from the caller (from your shell), so if you run the hook from the root folder, the executed commands will have the same ENVs.

If assumption about some cd hooks I can suggest you to use rc option to apply the changes that force using pyenv.

I can be wrong since I haven't worked with the pyenv closely. But I'll be glad to figure it out.

@mrexox
Copy link
Member

mrexox commented Apr 9, 2024

Hey! Have you tried the rc option? Do you still fight with this issue?

@mrexox mrexox added the waiting for response We need more details or confirmation label Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting for response We need more details or confirmation
Projects
None yet
Development

No branches or pull requests

2 participants