Skip to content

Commit

Permalink
Add REPO.bazel and MODULE.bazel as workspace boundary markers (#18787)
Browse files Browse the repository at this point in the history
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 <wyv@google.com>
  • Loading branch information
meteorcloudy and Wyverald committed Jun 27, 2023
1 parent 28312a9 commit 362540c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
18 changes: 8 additions & 10 deletions src/main/cpp/workspace_layout.cc
Expand Up @@ -27,21 +27,19 @@ namespace blaze {
using std::string;
using std::vector;

static const char kWorkspaceDotBazelMarker[] = "WORKSPACE.bazel";
static const char kWorkspaceMarker[] = "WORKSPACE";

string WorkspaceLayout::GetOutputRoot() const {
return blaze::GetOutputRoot();
}

bool WorkspaceLayout::InWorkspace(const string &workspace) const {
auto workspaceDotBazelPath =
blaze_util::JoinPath(workspace, kWorkspaceDotBazelMarker);
auto workspacePath = blaze_util::JoinPath(workspace, kWorkspaceMarker);
return (blaze_util::PathExists(workspaceDotBazelPath) &&
!blaze_util::IsDirectory(workspaceDotBazelPath)) ||
(blaze_util::PathExists(workspacePath) &&
!blaze_util::IsDirectory(workspacePath));
for (auto boundaryFileName :
{"MODULE.bazel", "REPO.bazel", "WORKSPACE.bazel", "WORKSPACE"}) {
auto boundaryFilePath = blaze_util::JoinPath(workspace, boundaryFileName);
if (blaze_util::PathExists(boundaryFilePath) &&
!blaze_util::IsDirectory(boundaryFilePath))
return true;
}
return false;
}

string WorkspaceLayout::GetWorkspace(const string &cwd) const {
Expand Down
5 changes: 3 additions & 2 deletions src/main/cpp/workspace_layout.h
Expand Up @@ -29,7 +29,8 @@ class WorkspaceLayout {
virtual std::string GetOutputRoot() const;

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

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

Expand Down
26 changes: 17 additions & 9 deletions src/test/cpp/workspace_layout_test.cc
Expand Up @@ -22,6 +22,7 @@
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/path.h"
#include "googletest/include/gtest/gtest.h"
#include "src/main/cpp/util/file_platform.h"

namespace blaze {

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

void SetUp() override {
ASSERT_TRUE(blaze_util::MakeDirectories(build_root_, 0755));
ASSERT_TRUE(blaze_util::WriteFile(
"", blaze_util::JoinPath(build_root_, "WORKSPACE"), 0755));
}

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

TEST_F(WorkspaceLayoutTest, GetWorkspace) {
ASSERT_TRUE(blaze_util::WriteFile(
"", blaze_util::JoinPath(build_root_, "WORKSPACE"), 0755));
const auto foobar = blaze_util::JoinPath(build_root_, "foo/bar");
ASSERT_TRUE(blaze_util::MakeDirectories(foobar, 0755));

// "" is returned when there's no workspace path.
std::string cwd = "foo/bar";
ASSERT_EQ("", workspace_layout_->GetWorkspace(cwd));
ASSERT_FALSE(workspace_layout_->InWorkspace(cwd));
ASSERT_EQ("", workspace_layout_->GetWorkspace("foo/bar"));
ASSERT_FALSE(workspace_layout_->InWorkspace("foo/bar"));

cwd = build_root_;
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd));
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(build_root_));
ASSERT_TRUE(workspace_layout_->InWorkspace(build_root_));

cwd = blaze_util::JoinPath(build_root_, cwd);
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd));
ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(foobar));
ASSERT_FALSE(workspace_layout_->InWorkspace(foobar));

// Now write a REPO.bazel file in foo/bar. It becomes the new workspace root.
ASSERT_TRUE(blaze_util::WriteFile(
"", blaze_util::JoinPath(foobar, "REPO.bazel"), 0755));
ASSERT_EQ(foobar, workspace_layout_->GetWorkspace(foobar));
ASSERT_TRUE(workspace_layout_->InWorkspace(foobar));
}

} // namespace blaze

0 comments on commit 362540c

Please sign in to comment.