unoconv / unoserver

MIT License
495 stars 69 forks source link

Use sudo -H instead of sudo on Readme to avoid ruin the permissions of the pip cache folder #113

Closed luzfcb closed 3 months ago

luzfcb commented 3 months ago

Motivation and Context

Assuming that I am a regular Linux user named luzfcb, with the HOME folder located in /home/luzfcb and the $HOMEenvironment variable pointing to /home/luzfcb

sudo without -H modifier will run the command with root permissions, but using the $HOME environment variable of the current user ( not the root ) pip will use the $HOME environment variable to create a cache directory on /home/luzfcb/.cache/pip/, but all new files/directories will be created with root permission, and root-only read permission; that is, the luzfcb user has no write or read permission to some of the directories inside /home/luzfcb/.cache/pip/ .

sudo with the -H modifier will run the command with root permissions and use the root user's $HOMEenvironment variable instead of the current user's $HOME.

So, if we use sudo with the -H modifier to run pip, the pip will use the $HOME environment variable of the root user to create a cache directory on /root/.cache/pip/, and all future sufferings will be avoided :-) .

Another option, to not use sudo with -H, is use pip with --no-cache-dir

Description

This pull-request adds the -H modifier the sudo when it is used the pip on the Readme

regebro commented 3 months ago

Hmm. Strange. In 30 years of using Linux I have never encountered this problem.

After digging some in the sudo manuals, I think possibly your /etc/sudoers have a Defaults !env_reset line in it, where the default is Defaults env_reset which should mean that sudo resets the env, including $HOME?

But I'm not sure, it's all a bit unclear, so that's just a guess.

luzfcb commented 3 months ago

I had this problem multiple times when I was new to Python and didn't understand how pip's cache works. At that time pip did not display a useful error message:

https://github.com/pypa/pip/blob/f5e4ee104e7b171a7cfb2843c9c602abf7a4e346/src/pip/_internal/cli/req_command.py#L220-L226 https://github.com/pypa/pip/blob/f5e4ee104e7b171a7cfb2843c9c602abf7a4e346/src/pip/_internal/cli/base_command.py#L163-L172

At that time, if I run sudo pip install foo-package and then pip install bar-package, you may have a permission problem because pip is trying to save some of the dependencies in the pip's cache subdirectory that does not have the same permissions as the current non-root user.

My intention with this pull-request is only to correct the use of sudo or pip to avoid introducing permissions problems in the non-root user's HOME pip cache directory

After digging some in the sudo manuals, I think possibly your /etc/sudoers have a Defaults !env_reset line in it, where the default is Defaults env_reset which should mean that sudo resets the env, including $HOME?

Although it may be a solution, I think it is beyond the scope of unoserver because it is a problem on pip itself and how it interacts with sudo, and that problem will not be resolved by pip at all because is also out of pip scope

regebro commented 3 months ago

That warning doesn't have anything to do with the .cache directory, and in fact pip checks the permissions, and if it encounters the error you have, it warns you, and that has seemingly been the case for 5 years. Pip warns against using sudo for other reasons, and recommends using a virtualenv, but the point of this readme is that a virtualenv will not work with unoserver (although it will work fine for the unoconvert and unocompare commands).

But then again, adding -H doesn't hurt, I guess.