feat: introduce find_resource! macro that works with Cargo or Bazel #8879
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To support Bazelification in #8875, this PR introduces a new
find_resource!macro that we use in place of our existing logic in tests that looks for resources relative to the compile-timeCARGO_MANIFEST_DIRenv var.To make this work, we plan to add the following to all
rust_library()andrust_test()Bazel rules in the project:Our new
find_resource!macro reads this value viaoption_env!("BAZEL_PACKAGE")so that the Bazel package of the code usingfind_resource!is injected into the code expanded from the macro. (Iffind_resource()were a function, thenoption_env!("BAZEL_PACKAGE")would always becodex-rs/utils/cargo-bin, which is not what we want.)Note we only consider the
BAZEL_PACKAGEvalue when theRUNFILES_DIRenvironment variable is set at runtime, indicating that the test is being run by Bazel. In this case, we have to concatenate the runtimeRUNFILES_DIRwith the compile-timeBAZEL_PACKAGEvalue to build the path to the resource.In testing this change, I discovered one funky edge case in
codex-rs/exec-server/tests/common/lib.rswhere we have to normalize (but not canonicalize!) the result fromfind_resource!because the path contains acommon/..component that does not exist on disk when the test is run under Bazel, so it must be semantically normalized using thepath-absolutizecrate before it is passed todotslash fetch.Because this new behavior may be non-obvious, this PR also updates
AGENTS.mdto make humans/Codex aware that this API is preferred.