Skip to content

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

Open
@Tikkoneus

Description

@Tikkoneus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssues that need to be evaluated for severity and status.bug

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions