• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 21552830208

31 Jan 2026 11:40PM UTC coverage: 80.277% (-0.05%) from 80.324%
21552830208

Pull #23062

github

web-flow
Merge 808a9786c into 2c4dcf9cf
Pull Request #23062: Remove support for Get

18 of 25 new or added lines in 4 files covered. (72.0%)

17119 existing lines in 541 files now uncovered.

78278 of 97510 relevant lines covered (80.28%)

3.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.49
/src/python/pants/base/build_root.py
1
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
import os
12✔
5
from collections.abc import Iterator
12✔
6
from contextlib import contextmanager
12✔
7
from pathlib import Path
12✔
8

9
from pants.util.meta import SingletonMetaclass
12✔
10

11

12
class BuildRoot(metaclass=SingletonMetaclass):
12✔
13
    """Represents the global workspace build root.
14

15
    By default, a Pants workspace is defined by a root directory where one of multiple sentinel
16
    files resides, such as `pants.toml` or `BUILD_ROOT`. This path can also be manipulated through
17
    this interface for re-location of the build root in tests.
18
    """
19

20
    sentinel_files = ["pants.toml", "pants", "BUILDROOT", "BUILD_ROOT"]
12✔
21

22
    class NotFoundError(Exception):
12✔
23
        """Raised when unable to find the current workspace build root."""
24

25
    def find_buildroot(self) -> str:
12✔
26
        buildroot = Path.cwd().resolve()
12✔
27
        while not any((buildroot / sentinel).is_file() for sentinel in self.sentinel_files):
12✔
UNCOV
28
            if buildroot != buildroot.parent:
1✔
UNCOV
29
                buildroot = buildroot.parent
1✔
30
            else:
UNCOV
31
                raise self.NotFoundError(
1✔
32
                    "No build root detected. Pants detects the build root by looking for at least one file "
33
                    f"from {self.sentinel_files} in the cwd and its ancestors. If you have none of these "
34
                    f"files, you can create an empty file in your build root."
35
                )
36
        return str(buildroot)
12✔
37

38
    def __init__(self) -> None:
12✔
39
        self._root_dir: str | None = None
12✔
40

41
    @property
12✔
42
    def pathlib_path(self) -> Path:
12✔
43
        return Path(self.path)
×
44

45
    @property
12✔
46
    def path(self) -> str:
12✔
47
        """Returns the build root for the current workspace."""
48
        if self._root_dir is None:
12✔
49
            # Do not remove/change this env var without coordinating with `pantsbuild/scie-pants` as
50
            # it is being used when bootstrapping Pants.
51
            override_buildroot = os.environ.get("PANTS_BUILDROOT_OVERRIDE", None)
12✔
52
            if override_buildroot:
12✔
UNCOV
53
                self._root_dir = override_buildroot
2✔
54
            else:
55
                self._root_dir = os.path.realpath(self.find_buildroot())
12✔
56
        return self._root_dir
12✔
57

58
    @path.setter
12✔
59
    def path(self, root_dir: str) -> None:
12✔
60
        """Manually establishes the build root for the current workspace."""
61
        path = os.path.realpath(root_dir)
12✔
62
        if not os.path.exists(path):
12✔
63
            raise ValueError(f"Build root does not exist: {root_dir}")
×
64
        self._root_dir = path
12✔
65

66
    def reset(self) -> None:
12✔
67
        """Clears the last calculated build root for the current workspace."""
UNCOV
68
        self._root_dir = None
1✔
69

70
    def __str__(self) -> str:
12✔
71
        return f"BuildRoot({self._root_dir})"
×
72

73
    @contextmanager
12✔
74
    def temporary(self, path: str) -> Iterator[None]:
12✔
75
        """Establishes a temporary build root, restoring the prior build root on exit."""
UNCOV
76
        if path is None:
1✔
77
            raise ValueError("Can only temporarily establish a build root given a path.")
×
UNCOV
78
        prior = self._root_dir
1✔
UNCOV
79
        self._root_dir = path
1✔
UNCOV
80
        try:
1✔
UNCOV
81
            yield
1✔
82
        finally:
UNCOV
83
            self._root_dir = prior
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc