Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,16 @@ This eliminates the need for a manually constructed `MANIFEST.in` in most cases.
For Git the listing is based on the index (so freshly `git add`-ed files are
included) and it follows `export-ignore` just like `git archive` does.

When file discovery reaches a checkout but the Git, Mercurial, or Jujutsu executable is missing,
it logs a warning only if no other finder returns tracked files. A colocated Git/Jujutsu
checkout stays quiet when either available tool can list its files.
Install the corresponding tool and make it available on `PATH` to include its tracked files.

If version inference has already failed to find an SCM checkout, file discovery skips
the finder commands. When SCM metadata exists at the project root, this instead logs
the existing "file discovery is suppressed" warning, which indicates that tracked files
may be missing from the distribution.

#### Git submodules

The Git file finder descends into submodules and lists their tracked files
Expand Down
61 changes: 60 additions & 1 deletion setuptools-scm/testing_scm/test_file_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,32 @@ def _build_sdist(cwd: Path) -> str:
print("WARNING:", w.message)
""")
(cwd / "dist").mkdir(exist_ok=True)
return run([sys.executable, "-c", code], cwd).stdout
result = run(
[sys.executable, "-c", code],
cwd=cwd,
merge_stderr=True,
)
assert result.returncode == 0, result.stdout
return result.stdout


def test_failed_sdist_exposes_combined_output(tmp_path: Path) -> None:
(tmp_path / "setup.py").write_text(
dedent("""\
import os
os.write(1, b"before failure\\n")
os.write(2, b"diagnostic on stderr\\n")
os.write(1, b"after diagnostic\\n")
raise SystemExit(7)
"""),
encoding="utf-8",
)

with pytest.raises(AssertionError) as error:
_build_sdist(tmp_path)

lines = [line.strip() for line in str(error.value).splitlines()]
assert lines[:3] == ["before failure", "diagnostic on stderr", "after diagnostic"]


@pytest.mark.issue(1407)
Expand Down Expand Up @@ -110,6 +135,40 @@ def test_configured_build_does_not_warn(
assert _DEPRECATION not in _build_sdist(wd.cwd)


@pytest.mark.issue(1404)
def test_configured_build_missing_git_warns_about_suppressed_search(
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
) -> None:
wd.setup_git(monkeypatch)
wd.cwd.joinpath("pyproject.toml").write_text(
dedent("""\
[build-system]
requires = ["setuptools>=61", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "missing-git"
dynamic = ["version"]

[tool.setuptools_scm]
fallback_version = "0.0"
"""),
encoding="utf-8",
)
wd.cwd.joinpath("mod.py").touch()
wd.cwd.joinpath("README.md").touch()
wd.add_and_commit()
empty_path = wd.cwd / "empty-path"
empty_path.mkdir()
monkeypatch.setenv("PATH", str(empty_path))

output = _build_sdist(wd.cwd)

assert output.count("file discovery is suppressed") == 1
assert "the Git executable was not found" not in output
assert len(list(wd.cwd.joinpath("dist").glob("*.tar.gz"))) == 1


@pytest.mark.issue(1407)
def test_simple_extra_build_does_not_warn(
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
Expand Down
1 change: 1 addition & 0 deletions vcs-versioning/changelog.d/1404.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warn when file discovery finds Git, Mercurial, or Jujutsu metadata but the corresponding executable is missing and no other finder returns files.
59 changes: 52 additions & 7 deletions vcs-versioning/src/vcs_versioning/_file_finders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import sys
from collections.abc import Callable, Generator, Iterable, Mapping
from importlib.metadata import EntryPoint

if sys.version_info >= (3, 10):
from typing import TypeGuard
Expand Down Expand Up @@ -126,6 +127,34 @@ def is_toplevel_acceptable(
return toplevel not in ignore_vcs_roots


def _log_missing_vcs_command(marker: str, root: str, tool: str) -> None:
log.warning(
"cannot list tracked files for %s in %s because the %s"
" executable was not found",
marker,
root,
tool,
)


def _warn_on_missing_vcs_command(path: _t.PathT, marker: str, tool: str) -> None:
from .._discover import match_entrypoint, walk_potential_roots

if not os.path.isdir(path or "."):
return

for root in walk_potential_roots(norm_real(path or ".")):
if match_entrypoint(root, marker):
if is_toplevel_acceptable(str(root)):
command = (marker, str(root), tool)
pending = _missing_vcs_commands.get()
if pending is None:
_log_missing_vcs_command(*command)
elif command not in pending:
pending.append(command)
return


_FILE_FINDER_GROUPS = (
"setuptools_scm.files_command",
"setuptools_scm.files_command_fallback",
Expand All @@ -134,6 +163,9 @@ def is_toplevel_acceptable(
_scm_search_failed: contextvars.ContextVar[bool] = contextvars.ContextVar(
"vcs_versioning_scm_search_failed", default=False
)
_missing_vcs_commands: contextvars.ContextVar[list[tuple[str, str, str]] | None] = (
contextvars.ContextVar("vcs_versioning_missing_commands", default=None)
)


@contextlib.contextmanager
Expand Down Expand Up @@ -203,13 +235,26 @@ def find_files(path: _t.PathT = "") -> list[str]:
_warn_on_suppressed_search_with_marker(root)
return []

for group in _FILE_FINDER_GROUPS:
for ep, wd in iter_marker_entrypoints(root, group):
log.debug("file finder %s selected by marker in %s", ep.name, wd)
command: Callable[[_t.PathT], list[str]] = ep.load()
res: list[str] = command(path)
if res:
return res
pending: list[tuple[str, str, str]] = []
token = _missing_vcs_commands.set(pending)
try:
attempted: set[EntryPoint] = set()
for group in _FILE_FINDER_GROUPS:
for ep, wd in iter_marker_entrypoints(root, group):
# The finder receives the same input path for every enclosing marker.
if ep in attempted:
continue
attempted.add(ep)
log.debug("file finder %s selected by marker in %s", ep.name, wd)
command: Callable[[_t.PathT], list[str]] = ep.load()
res: list[str] = command(path)
if res:
return res
finally:
_missing_vcs_commands.reset(token)

for command_info in pending:
_log_missing_vcs_command(*command_info)

return []

Expand Down
11 changes: 9 additions & 2 deletions vcs-versioning/src/vcs_versioning/_file_finders/_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from .._compat import norm_real, strip_path_suffix
from .._integration import data_from_mime
from .._run_cmd import run as _run
from . import collect_files_and_dirs, is_toplevel_acceptable, scm_find_files
from . import (
_warn_on_missing_vcs_command,
collect_files_and_dirs,
is_toplevel_acceptable,
scm_find_files,
)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,8 +64,10 @@ def _git_toplevel(path: str) -> str | None:
except subprocess.CalledProcessError:
# git returned error, we are not in a git repo
return None
except FileNotFoundError:
_warn_on_missing_vcs_command(path, ".git", "Git")
return None
except OSError:
# git command not found, probably
return None
except subprocess.TimeoutExpired:
log.warning("git rev-parse timed out - pretending this is not a git repo")
Expand Down
11 changes: 9 additions & 2 deletions vcs-versioning/src/vcs_versioning/_file_finders/_hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from .._backends._hg import run_hg
from .._compat import norm_real
from .._integration import data_from_mime
from . import collect_files_and_dirs, is_toplevel_acceptable, scm_find_files
from . import (
_warn_on_missing_vcs_command,
collect_files_and_dirs,
is_toplevel_acceptable,
scm_find_files,
)

log = logging.getLogger(__name__)

Expand All @@ -23,8 +28,10 @@ def _hg_toplevel(path: str) -> str | None:
except subprocess.CalledProcessError:
# hg returned error, we are not in a mercurial repo
return None
except FileNotFoundError:
_warn_on_missing_vcs_command(path, ".hg", "Mercurial")
return None
except OSError:
# hg command not found, probably
return None
except subprocess.TimeoutExpired:
log.warning("hg root timed out - pretending this is not a mercurial repo")
Expand Down
10 changes: 9 additions & 1 deletion vcs-versioning/src/vcs_versioning/_file_finders/_jj.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
from .. import _types as _t
from .._compat import norm_real
from .._run_cmd import run as _run
from . import collect_files_and_dirs, is_toplevel_acceptable, scm_find_files
from . import (
_warn_on_missing_vcs_command,
collect_files_and_dirs,
is_toplevel_acceptable,
scm_find_files,
)

log = logging.getLogger(__name__)

Expand All @@ -30,6 +35,9 @@ def _jj_toplevel(path: str) -> str | None:
return norm_real(toplevel)
except subprocess.CalledProcessError:
return None
except FileNotFoundError:
_warn_on_missing_vcs_command(path, ".jj", "Jujutsu")
return None
except OSError:
return None
except subprocess.TimeoutExpired:
Expand Down
4 changes: 3 additions & 1 deletion vcs-versioning/src/vcs_versioning/_run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def run(
timeout: int | None = None,
check: bool = False,
input: str | None = None,
merge_stderr: bool = False,
) -> CompletedProcess:
if isinstance(cmd, str):
cmd = shlex.split(cmd)
Expand All @@ -159,7 +160,8 @@ def run(
res = subprocess.run(
cmd,
check=False, # handled below via CompletedProcess.check_returncode
capture_output=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT if merge_stderr else subprocess.PIPE,
input=input,
cwd=os.fspath(cwd),
env=dict(
Expand Down
Loading
Loading