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

[BUG] 65.4.0 will not run without USERPROFILE set #4202

Open
Tikkoneus opened this issue Feb 1, 2024 · 1 comment
Open

[BUG] 65.4.0 will not run without USERPROFILE set #4202

Tikkoneus opened this issue Feb 1, 2024 · 1 comment
Labels
bug Needs Triage Issues that need to be evaluated for severity and status.

Comments

@Tikkoneus
Copy link

Tikkoneus commented Feb 1, 2024

setuptools version

65.4.0+

Python version

3.9.9

OS

Windows

Additional environment information

No response

Description

We have a build environment which strips out all but an allowed list of environment variables. In particular, it removes HOME / USERPROFILE. This causes setup.py to fail under all conditions with setuptools 65.4.0 and up. Here's why:

In versions prior to 65.4.0 setuptools used os.path.expanduser. The docs state: "If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged." With no home directory user_file = os.path.join(os.path.expanduser('~'), user_filename) was left with a ~ in it, but then find_config_files would silently ignore it: files = [str(path) for path in self._gen_paths() if path.is_file()] (because path.is_file would return False).

commit (included in 65.4.0) switched from os.path to pathlib, but pathlib raises an exception Cannot determine home directory rather than returning the unexpanded path.

This would be fine because we already pass in --no-user-cfg, and doing so avoids the exception-raising codepath altogether. However, the Distribution class which raises the exception is actually instantiated twice: once at build time and once during initialization. _install_setup_requires instantiates a MinimalDistribution and doesn't pass through any script_args. The upshot is that even if we use --no-user-cfg the setuptool initialization will still raise the exception if no home directory is found. Because the MinimalDistribution class is defined in a function block it's not even easy to monkeypatch.

One possible fix is if --no-user-cfg is specified in the script args then MinimalDistribution:__init__ could add
filtered['script_args'] = ['--no-user-cfg']
prior to the call to super().__init__(filtered)

Note this affects Posix systems, too, though slightly differently. pathlib gethomedir for Posix can still raise an exception, but only if no username is specifically passed in and pwd.getpwnam(username).pw_dir raises an exception.

Expected behavior

Being able to use setuptools with any environment, including one with no USERPROFILE set, especially if passing explicit flags such as --no-user-cfg.

How to Reproduce

  1. Install setuptools 65.4.0 or greater
  2. unset USERPROFILE
  3. run setup.py

Edit: adding a better reproduce step:

echo from setuptools import setup; setup() > setup.py
set USERPROFILE=
set HOMEDRIVE=
set HOMEPATH=
python setup.py --no-user-cfg bdist_wheel

Output

RuntimeError: Can't determine home directory
@Tikkoneus Tikkoneus added bug Needs Triage Issues that need to be evaluated for severity and status. labels Feb 1, 2024
@Tikkoneus
Copy link
Author

Tikkoneus commented Feb 1, 2024

I tried

import sys
if '--no-user-cfg' in sys.argv:
    filtered['script_args'] = ['--no-user-cfg']
super().__init__(filtered)

in _install_setup_requires and that did eliminate the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Needs Triage Issues that need to be evaluated for severity and status.
Projects
None yet
Development

No branches or pull requests

1 participant