pypa/setuptools-scm

Regression: v9.2 affects setuptools configuration merging from multiple sources

Closed this issue · 3 comments

In a project that uses both a pyproject.toml and setup.cfg for configuration, setuptools-scm v9.2.0 causes a setuptools.errors.PackageDiscoveryError, whereas the same configuration worked with setuptools-scm v9.1.1.

Reproducer

The following code works with setuptools-scm v9.1.1, but fails with v9.2.0:

pyproject.toml

[build-system]
build-backend = "setuptools.build_meta"
requires = [
  "setuptools==80.9",
  "setuptools-scm[toml]==9.2",
]

[project]
name = "my-project"
description = ""
authors = []
requires-python = ">=3.9"
dynamic = [
  "version",
]

[tool.setuptools]
packages = [
  "my_pkg",
]
include-package-data = true

# Enabling this block and removing setup.cfg makes the build succeed with setuptools-scm v9.2
# [tool.setuptools.dynamic]
# version = {attr= "my_pkg.__version__"}

[tool.setuptools_scm]
write_to = "my_pkg/_version.py"
local_scheme = "no-local-version"

setup.cfg

[metadata]
version = attr: my_pkg.__version__

my_pkg/init.py

from ._version import version as __version__  # noqa: F401

another_directory/

Create an empty directory next to my_pkg, so that there is more than one candidate for inclusion into the distribution files.

Then run: python -m build

Behavior

The above configuration leads to setuptools raising a PackageDiscoveryError:

[…]
setuptools.errors.PackageDiscoveryError: Multiple top-level packages discovered in a flat-layout: ['my_pkg', 'another_directory'].

To avoid accidental inclusion of unwanted files or directories,
setuptools will not proceed with this build.

If you are trying to create a single distribution with multiple packages
on purpose, you should not rely on automatic discovery.
Instead, consider the following options:

1. set up custom discovery (`find` directive with `include` or `exclude`)
2. use a `src-layout`
3. explicitly set `py_modules` or `packages` with a list of names

To find more information, look for "package discovery" on setuptools docs.

Background

I encountered this problem in pytest-asyncio which has a relatively stale setuptools-scm setup that needs cleaning up.
Personally, I don't find it a particularly pressing issue, but I didn't find any mention of this in the changelog and I thought a bug report would be helpful.

Its strictly incorrect to use the version files as setuptools version source when you use Setuptools_scm in the first place

If it worked in 9.1 the precisely because of the bug for which it was yanked

More context may be necessary but you absolutely need to drop the extra dynamic mess you added

In that case, let's close the issue.

I switched to deriving the project __version__ using importlib.metadata in favour of a version file, since the former is now available for all recent Python versions without additional dependency. This allows getting rid of all the "extra dynamic mess".

Note that version files are still a good idea in some cases

Its just a bad idea to use version files and then telling setuptools itsel fo use the version file as source of truth