Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.3.0] Add REPO.bazel and MODULE.bazel as workspace boundary markers #18787

Merged
merged 2 commits into from
Jun 27, 2023
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
18 changes: 8 additions & 10 deletions src/main/cpp/workspace_layout.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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