runfiles: Implement Runfiles::CurrentRepository for C++#17887
runfiles: Implement Runfiles::CurrentRepository for C++#17887BoleynSu wants to merge 2 commits intobazelbuild:masterfrom
Conversation
tools/cpp/runfiles/runfiles_src.cc
Outdated
| 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' && |
There was a problem hiding this comment.
Could this use startswith? In any case it would need bounds checking as file may be too short.
There was a problem hiding this comment.
Good catch! Is path always using forward slash, i.e., even on Windows?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Could you split this out into a new test?
| } | ||
| EOF | ||
|
|
||
| bazel run //pkg:binary &>"$TEST_log" || fail "Run should succeed" |
There was a problem hiding this comment.
I think that the test fails because Bazel uses C++11 by default. You probably have to add --copt=c++20.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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?
6fb9077 to
ee0893d
Compare
|
I just found that cc_integration_test is disable for windows. Do we want to test it for windows? |
b5d518e to
237644f
Compare
|
I think we do. You could move the test to |
I have split the test to a new file which has passed the test on all platforms. PTAL |
|
Friendly ping |
| // (also known as the workspace). | ||
| static std::string CurrentRepository(const std::string& file = __builtin_FILE()); | ||
| private: | ||
| static std::string GetPrefixHint() { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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())) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Not really. I do not have MSVC at hand to dig deeper. Seems this only happens we call builtin_FILE inside headers.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
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. |
|
@BoleynSu Could you provide an update on this PR? |
|
@BoleynSu please re-open if you are still interested |
No description provided.