pypa / pypa/setuptools

[BUG] 65.4.0 will not run without USERPROFILE set

Open
#4,202 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Needs Triage
Dominant language
Python
Stars
2.9k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with USERPROFILE, HOMEDRIVE, and HOMEPATH unset, using the setup.py command shown. Read setuptools/init.py around MinimalDistribution and setuptools/_distutils/dist.py around Distribution's home-directory handling, including how script_args are passed. Done means setup.py runs with --no-user-cfg in an environment without a home directory.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.