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

pantsbuild / pants / 18517631058

15 Oct 2025 04:18AM UTC coverage: 69.207% (-11.1%) from 80.267%
18517631058

Pull #22745

github

web-flow
Merge 642a76ca1 into 99919310e
Pull Request #22745: [windows] Add windows support in the stdio crate.

53815 of 77759 relevant lines covered (69.21%)

2.42 hits per line

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

70.21
/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
7✔
5
from collections.abc import Iterator
7✔
6
from contextlib import contextmanager
7✔
7
from pathlib import Path
7✔
8

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

11

12
class BuildRoot(metaclass=SingletonMetaclass):
7✔
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"]
7✔
21

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

25
    def find_buildroot(self) -> str:
7✔
26
        buildroot = Path.cwd().resolve()
7✔
27
        while not any((buildroot / sentinel).is_file() for sentinel in self.sentinel_files):
7✔
28
            if buildroot != buildroot.parent:
×
29
                buildroot = buildroot.parent
×
30
            else:
31
                raise self.NotFoundError(
×
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)
7✔
37

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

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

45
    @property
7✔
46
    def path(self) -> str:
7✔
47
        """Returns the build root for the current workspace."""
48
        if self._root_dir is None:
7✔
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)
7✔
52
            if override_buildroot:
7✔
53
                self._root_dir = override_buildroot
1✔
54
            else:
55
                self._root_dir = os.path.realpath(self.find_buildroot())
7✔
56
        return self._root_dir
7✔
57

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

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

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

73
    @contextmanager
7✔
74
    def temporary(self, path: str) -> Iterator[None]:
7✔
75
        """Establishes a temporary build root, restoring the prior build root on exit."""
76
        if path is None:
×
77
            raise ValueError("Can only temporarily establish a build root given a path.")
×
78
        prior = self._root_dir
×
79
        self._root_dir = path
×
80
        try:
×
81
            yield
×
82
        finally:
83
            self._root_dir = prior
×
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

© 2025 Coveralls, Inc