Skip to content

Commit 362540c

Browse files
Add REPO.bazel and MODULE.bazel as workspace boundary markers (#18787)
Work towards #18077 RELNOTES: The REPO.bazel and MODULE.bazel files are now also considered workspace boundary markers. PiperOrigin-RevId: 523979135 Change-Id: I0cbc9964742a0e5efa8afac8b8dfb84c6235a2f0 Co-authored-by: Googler <[email protected]>
1 parent 28312a9 commit 362540c

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/main/cpp/workspace_layout.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,19 @@ namespace blaze {
2727
using std::string;
2828
using std::vector;
2929

30-
static const char kWorkspaceDotBazelMarker[] = "WORKSPACE.bazel";
31-
static const char kWorkspaceMarker[] = "WORKSPACE";
32-
3330
string WorkspaceLayout::GetOutputRoot() const {
3431
return blaze::GetOutputRoot();
3532
}
3633

3734
bool WorkspaceLayout::InWorkspace(const string &workspace) const {
38-
auto workspaceDotBazelPath =
39-
blaze_util::JoinPath(workspace, kWorkspaceDotBazelMarker);
40-
auto workspacePath = blaze_util::JoinPath(workspace, kWorkspaceMarker);
41-
return (blaze_util::PathExists(workspaceDotBazelPath) &&
42-
!blaze_util::IsDirectory(workspaceDotBazelPath)) ||
43-
(blaze_util::PathExists(workspacePath) &&
44-
!blaze_util::IsDirectory(workspacePath));
35+
for (auto boundaryFileName :
36+
{"MODULE.bazel", "REPO.bazel", "WORKSPACE.bazel", "WORKSPACE"}) {
37+
auto boundaryFilePath = blaze_util::JoinPath(workspace, boundaryFileName);
38+
if (blaze_util::PathExists(boundaryFilePath) &&
39+
!blaze_util::IsDirectory(boundaryFilePath))
40+
return true;
41+
}
42+
return false;
4543
}
4644

4745
string WorkspaceLayout::GetWorkspace(const string &cwd) const {

src/main/cpp/workspace_layout.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class WorkspaceLayout {
2929
virtual std::string GetOutputRoot() const;
3030

3131
// Given the working directory, returns the nearest enclosing directory with a
32-
// WORKSPACE file in it. If there is no such enclosing directory, returns "".
32+
// workspace boundary file in it. If there is no such enclosing directory,
33+
// returns "".
3334
//
3435
// E.g., if there was a WORKSPACE file in foo/bar/build_root:
3536
// GetWorkspace('foo/bar') --> ''
@@ -41,7 +42,7 @@ class WorkspaceLayout {
4142
virtual std::string GetWorkspace(const std::string& cwd) const;
4243

4344
// Given a result returned from GetWorkspace, returns a pretty workspace name
44-
// than can e.g. be used in the process title of the Bazel server.
45+
// that can e.g. be used in the process title of the Bazel server.
4546
virtual std::string GetPrettyWorkspaceName(
4647
const std::string& workspace) const;
4748

src/test/cpp/workspace_layout_test.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "src/main/cpp/util/file.h"
2323
#include "src/main/cpp/util/path.h"
2424
#include "googletest/include/gtest/gtest.h"
25+
#include "src/main/cpp/util/file_platform.h"
2526

2627
namespace blaze {
2728

@@ -34,8 +35,6 @@ class WorkspaceLayoutTest : public ::testing::Test {
3435

3536
void SetUp() override {
3637
ASSERT_TRUE(blaze_util::MakeDirectories(build_root_, 0755));
37-
ASSERT_TRUE(blaze_util::WriteFile(
38-
"", blaze_util::JoinPath(build_root_, "WORKSPACE"), 0755));
3938
}
4039

4140
void TearDown() override {
@@ -55,17 +54,26 @@ class WorkspaceLayoutTest : public ::testing::Test {
5554
};
5655

5756
TEST_F(WorkspaceLayoutTest, GetWorkspace) {
57+
ASSERT_TRUE(blaze_util::WriteFile(
58+
"", blaze_util::JoinPath(build_root_, "WORKSPACE"), 0755));
59+
const auto foobar = blaze_util::JoinPath(build_root_, "foo/bar");
60+
ASSERT_TRUE(blaze_util::MakeDirectories(foobar, 0755));
61+
5862
// "" is returned when there's no workspace path.
59-
std::string cwd = "foo/bar";
60-
ASSERT_EQ("", workspace_layout_->GetWorkspace(cwd));
61-
ASSERT_FALSE(workspace_layout_->InWorkspace(cwd));
63+
ASSERT_EQ("", workspace_layout_->GetWorkspace("foo/bar"));
64+
ASSERT_FALSE(workspace_layout_->InWorkspace("foo/bar"));
6265

63-
cwd = build_root_;
64-
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd));
66+
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(build_root_));
6567
ASSERT_TRUE(workspace_layout_->InWorkspace(build_root_));
6668

67-
cwd = blaze_util::JoinPath(build_root_, cwd);
68-
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd));
69+
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(foobar));
70+
ASSERT_FALSE(workspace_layout_->InWorkspace(foobar));
71+
72+
// Now write a REPO.bazel file in foo/bar. It becomes the new workspace root.
73+
ASSERT_TRUE(blaze_util::WriteFile(
74+
"", blaze_util::JoinPath(foobar, "REPO.bazel"), 0755));
75+
ASSERT_EQ(foobar, workspace_layout_->GetWorkspace(foobar));
76+
ASSERT_TRUE(workspace_layout_->InWorkspace(foobar));
6977
}
7078

7179
} // namespace blaze

0 commit comments

Comments
 (0)