Skip to content

runfiles: Implement Runfiles::CurrentRepository for C++#17887

Closed
BoleynSu wants to merge 2 commits intobazelbuild:masterfrom
BoleynSu:master
Closed

runfiles: Implement Runfiles::CurrentRepository for C++#17887
BoleynSu wants to merge 2 commits intobazelbuild:masterfrom
BoleynSu:master

Conversation

@BoleynSu
Copy link
Copy Markdown
Contributor

No description provided.

defined(BAZEL_RUNFILES_MSVC_HAS_BUILTIN_FILE)
std::string Runfiles::CurrentRepository(std::string file) {
std::string repo;
if (file[0] == 'e' && file[1] == 'x' && file[2] == 't' && file[3] == 'e' &&
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this use startswith? In any case it would need bounds checking as file may be too short.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Is path always using forward slash, i.e., even on Windows?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, but the tests should tell us.

expect_log "in external/other_repo/pkg/test.cpp: 'other_repo'"
expect_log "in pkg/library.cpp: ''"

# test Runfiles::CurrentRepository()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split this out into a new test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

}
EOF

bazel run //pkg:binary &>"$TEST_log" || fail "Run should succeed"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the test fails because Bazel uses C++11 by default. You probably have to add --copt=c++20.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, using CurrentRepository will fail when the cc toolchain does not support __builtin_FILE. I guess it is failing in such cases. Could we just check if it is supported and skip the rest of not?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I think that the test just wouldn't run if we did: Since Bazel passes --std=0x on Unix by default, it just won't be available on any of the runners.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more related to compiler version. Some older compiler does not support __builtin_FILE. I added a cc_test if test if __builtin_FILE is supported or not and skip the test if it is not supported.

// // Important:
// // If this is a test, use
// // Runfiles::CreateForTest(BAZEL_CURRENT_REPOSITORY, &error).
// // Runfiles::CreateForTest(Runfiles::CurrentRepository(), &error).
Copy link
Copy Markdown
Contributor Author

@BoleynSu BoleynSu Mar 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also make the repo parameter default to CurrentRepo such that user can use Create() or CreatForTest() to use the current repo where these function get called. However, this is a breaking change as currently it defaults to the main repo. Do we want to add a new function for this?

@sgowroji sgowroji added team-Rules-CPP Issues for C++ rules awaiting-review PR is awaiting review from an assigned reviewer labels Mar 27, 2023
@buildbreaker2021 buildbreaker2021 self-assigned this Apr 3, 2023
@BoleynSu BoleynSu force-pushed the master branch 3 times, most recently from 6fb9077 to ee0893d Compare April 16, 2023 11:02
@BoleynSu
Copy link
Copy Markdown
Contributor Author

I just found that cc_integration_test is disable for windows. Do we want to test it for windows?

@BoleynSu BoleynSu force-pushed the master branch 15 times, most recently from b5d518e to 237644f Compare April 16, 2023 13:56
@fmeum
Copy link
Copy Markdown
Collaborator

fmeum commented Apr 25, 2023

I think we do. You could move the test to src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py, which should be running on an all platforms and already contains related tests.

@BoleynSu
Copy link
Copy Markdown
Contributor Author

I think we do. You could move the test to src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py, which should be running on an all platforms and already contains related tests.

I have split the test to a new file which has passed the test on all platforms. PTAL

@BoleynSu
Copy link
Copy Markdown
Contributor Author

BoleynSu commented May 2, 2023

Friendly ping

// (also known as the workspace).
static std::string CurrentRepository(const std::string& file = __builtin_FILE());
private:
static std::string GetPrefixHint() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here explaining the purpose of this function? Doesn't have to be user-facing as it is private, but dev-facing.

#if defined(BAZEL_TOOLS_CPP_RUNFILES_HAS_BUILTIN_FILE)
// Returns the repository name.
//
// Use this from within `cc_test` rules.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next two paragraphs seem outdated.

// using bazel::tools::cpp::runfiles::Runfiles;
//
// 3. Create a Runfiles object and use rlocation to look up runfile paths:
// Note that if Runfiles::CurrentRepository() is not supported for the cc
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could make sense to mention that C++20 is a requirement (if that's actually true) to give users an idea when they should expect this to be available.

return s.rfind(suffix) == s.size() - suffix.size();
}

std::vector<std::string> split(const std::string& s, const std::string& sep) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If sep is always "/\\", we could inline that value, remove the parameter and call the function split_path.

}();
std::vector<std::string> paths = split(file, "/\\");
if (paths.size() > prefixes.size()) {
if (std::equal(prefixes.begin(), prefixes.end(), paths.begin())) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give some examples of prefixes this typically strips away? Assuming Bazel passes in relative paths, I would have expected the prefix to be empty or bazel-out/<something>/bin, in which case we could potentially simplify this logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is used on MSVC where absolute path is returned from builtin_FILE for headers. Let's add some comments on why we need GetPrefixHint.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a potential source for non-hermetic builds. Do you know whether there is a way to get MSVC to return relative paths from builtin_FILE? That would solve the hermeticity issue and simplify the logic here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. I do not have MSVC at hand to dig deeper. Seems this only happens we call builtin_FILE inside headers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will see whether I can find anything and if not, we can still go with the current approach.


bazel build pkg:feature_test &>"$TEST_log" || fail "Build should succeed"
if ! bazel test pkg:feature_test &>"$TEST_log"; then
echo "test skipped because __builtin_FILE is not supported" >&2
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know which CI runners actually end up running this test? I'm still a bit concerned that it's not running on at least one runner per OS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. At least, the runners that passes before adding this guard should still be running it. Seems to me it is better to add tags to exclude this test from some runners than the current approach. However, I do not know if such mechanism exists or how it is like here.

@comius comius added awaiting-user-response Awaiting a response from the author and removed awaiting-review PR is awaiting review from an assigned reviewer labels Dec 28, 2023
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 3, 2025

Thank you for contributing to the Bazel repository! This pull request has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this PR is still relevant and should stay open, please post any comment here and the PR will no longer be marked as stale.

@github-actions github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label Mar 3, 2025
@satyanandak
Copy link
Copy Markdown

@BoleynSu Could you provide an update on this PR?

@github-actions github-actions bot removed the stale Issues or PRs that are stale (no activity for 30 days) label May 13, 2025
@meisterT
Copy link
Copy Markdown
Member

@BoleynSu please re-open if you are still interested

@meisterT meisterT closed this Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-user-response Awaiting a response from the author team-Rules-CPP Issues for C++ rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants