diff options
6 files changed, 66 insertions, 23 deletions
diff --git a/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp b/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp index ba564ad8..3c25fa72 100644 --- a/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp +++ b/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp @@ -642,9 +642,16 @@ auto BazelMsgFactory::CreateGitTreeDigestFromLocalTree( } auto BazelMsgFactory::CreateActionDigestFromCommandLine( - ActionDigestRequest const& request) -> bazel_re::Digest { + ActionDigestRequest const& request) -> std::optional<bazel_re::Digest> { auto cmd = CreateCommandBundle(request); + if (cmd == nullptr) { + return std::nullopt; + } + auto action = CreateActionBundle(cmd->Digest(), request); + if (action == nullptr) { + return std::nullopt; + } if (request.store_blob) { (*request.store_blob)(cmd->MakeBlob(/*is_exec=*/false)); diff --git a/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp b/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp index ff61e9f3..720d00d5 100644 --- a/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp +++ b/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp @@ -97,7 +97,7 @@ class BazelMsgFactory { /// CommandBundle that can be captured via BlobStoreFunc. /// \returns Digest representing the action. [[nodiscard]] static auto CreateActionDigestFromCommandLine( - ActionDigestRequest const& request) -> bazel_re::Digest; + ActionDigestRequest const& request) -> std::optional<bazel_re::Digest>; /// \brief Create message vector from std::map. /// \param[in] input map diff --git a/src/buildtool/execution_api/local/local_action.cpp b/src/buildtool/execution_api/local/local_action.cpp index 4b8bc0dc..94d98d6b 100644 --- a/src/buildtool/execution_api/local/local_action.cpp +++ b/src/buildtool/execution_api/local/local_action.cpp @@ -17,7 +17,6 @@ #include <algorithm> #include <cstddef> #include <filesystem> -#include <optional> #include <string> #include <system_error> #include <utility> @@ -86,6 +85,14 @@ auto LocalAction::Execute(Logger const* logger) noexcept -> IExecutionResponse::Ptr { auto do_cache = CacheEnabled(cache_flag_); auto action = CreateActionDigest(root_digest_, not do_cache); + if (not action) { + if (logger != nullptr) { + logger->Emit(LogLevel::Error, + "failed to create an action digest for {}", + root_digest_.hash()); + } + return nullptr; + } if (logger != nullptr) { logger->Emit(LogLevel::Trace, @@ -93,16 +100,16 @@ auto LocalAction::Execute(Logger const* logger) noexcept " - exec_dir digest: {}\n" " - action digest: {}", root_digest_.hash(), - NativeSupport::Unprefix(action.hash())); + NativeSupport::Unprefix(action->hash())); } if (do_cache) { - if (auto result = storage_.ActionCache().CachedResult(action)) { + if (auto result = storage_.ActionCache().CachedResult(*action)) { if (result->exit_code() == 0 and ActionResultContainsExpectedOutputs( *result, output_files_, output_dirs_)) { return IExecutionResponse::Ptr{ - new LocalResponse{action.hash(), + new LocalResponse{action->hash(), {std::move(*result), /*is_cached=*/true}, &storage_}}; } @@ -110,16 +117,26 @@ auto LocalAction::Execute(Logger const* logger) noexcept } if (ExecutionEnabled(cache_flag_)) { - if (auto output = Run(action)) { + if (auto output = Run(*action)) { if (cache_flag_ == CacheFlag::PretendCached) { // ensure the same id is created as if caching were enabled - auto action_id = CreateActionDigest(root_digest_, false).hash(); + auto action_cached = CreateActionDigest(root_digest_, false); + if (not action_cached) { + if (logger != nullptr) { + logger->Emit( + LogLevel::Error, + "failed to create a cached action digest for {}", + root_digest_.hash()); + } + return nullptr; + } + output->is_cached = true; return IExecutionResponse::Ptr{new LocalResponse{ - std::move(action_id), std::move(*output), &storage_}}; + action_cached->hash(), std::move(*output), &storage_}}; } return IExecutionResponse::Ptr{new LocalResponse{ - action.hash(), std::move(*output), &storage_}}; + action->hash(), std::move(*output), &storage_}}; } } diff --git a/src/buildtool/execution_api/local/local_action.hpp b/src/buildtool/execution_api/local/local_action.hpp index 7999da33..582f9b8e 100644 --- a/src/buildtool/execution_api/local/local_action.hpp +++ b/src/buildtool/execution_api/local/local_action.hpp @@ -18,6 +18,7 @@ #include <chrono> #include <map> #include <memory> +#include <optional> #include <string> #include <unordered_map> #include <utility> // std::move @@ -97,7 +98,7 @@ class LocalAction final : public IExecutionAction { [[nodiscard]] auto CreateActionDigest(bazel_re::Digest const& exec_dir, bool do_not_cache) - -> bazel_re::Digest { + -> std::optional<bazel_re::Digest> { auto const env_vars = BazelMsgFactory::CreateMessageVectorFromMap< bazel_re::Command_EnvironmentVariable>(env_vars_); diff --git a/src/buildtool/execution_api/remote/bazel/bazel_action.cpp b/src/buildtool/execution_api/remote/bazel/bazel_action.cpp index 74634838..c6cadf20 100644 --- a/src/buildtool/execution_api/remote/bazel/bazel_action.cpp +++ b/src/buildtool/execution_api/remote/bazel/bazel_action.cpp @@ -14,7 +14,6 @@ #include "src/buildtool/execution_api/remote/bazel/bazel_action.hpp" -#include <optional> #include <utility> // std::move #include "src/buildtool/execution_api/bazel_msg/bazel_blob_container.hpp" @@ -49,6 +48,14 @@ auto BazelAction::Execute(Logger const* logger) noexcept BazelBlobContainer blobs{}; auto do_cache = CacheEnabled(cache_flag_); auto action = CreateBundlesForAction(&blobs, root_digest_, not do_cache); + if (not action) { + if (logger != nullptr) { + logger->Emit(LogLevel::Error, + "failed to create an action digest for {}", + root_digest_.hash()); + } + return nullptr; + } if (logger != nullptr) { logger->Emit(LogLevel::Trace, @@ -56,36 +63,46 @@ auto BazelAction::Execute(Logger const* logger) noexcept " - exec_dir digest: {}\n" " - action digest: {}", root_digest_.hash(), - action.hash()); + action->hash()); } if (do_cache) { if (auto result = - network_->GetCachedActionResult(action, output_files_)) { + network_->GetCachedActionResult(*action, output_files_)) { if (result->exit_code() == 0 and ActionResultContainsExpectedOutputs( *result, output_files_, output_dirs_) ) { return IExecutionResponse::Ptr{new BazelResponse{ - action.hash(), network_, {*result, true}}}; + action->hash(), network_, {*result, true}}}; } } } if (ExecutionEnabled(cache_flag_) and network_->UploadBlobs(std::move(blobs))) { - if (auto output = network_->ExecuteBazelActionSync(action)) { + if (auto output = network_->ExecuteBazelActionSync(*action)) { if (cache_flag_ == CacheFlag::PretendCached) { // ensure the same id is created as if caching were enabled - auto action_id = - CreateBundlesForAction(nullptr, root_digest_, false).hash(); + auto action_cached = + CreateBundlesForAction(nullptr, root_digest_, false); + if (not action_cached) { + if (logger != nullptr) { + logger->Emit( + LogLevel::Error, + "failed to create a cached action digest for {}", + root_digest_.hash()); + } + return nullptr; + } + output->cached_result = true; return IExecutionResponse::Ptr{new BazelResponse{ - std::move(action_id), network_, std::move(*output)}}; + action_cached->hash(), network_, std::move(*output)}}; } - return IExecutionResponse::Ptr{ - new BazelResponse{action.hash(), network_, std::move(*output)}}; + return IExecutionResponse::Ptr{new BazelResponse{ + action->hash(), network_, std::move(*output)}}; } } @@ -95,7 +112,7 @@ auto BazelAction::Execute(Logger const* logger) noexcept auto BazelAction::CreateBundlesForAction(BazelBlobContainer* blobs, bazel_re::Digest const& exec_dir, bool do_not_cache) const noexcept - -> bazel_re::Digest { + -> std::optional<bazel_re::Digest> { using StoreFunc = BazelMsgFactory::ActionDigestRequest::BlobStoreFunc; std::optional<StoreFunc> store_blob = std::nullopt; if (blobs != nullptr) { diff --git a/src/buildtool/execution_api/remote/bazel/bazel_action.hpp b/src/buildtool/execution_api/remote/bazel/bazel_action.hpp index 58e5965f..8392b1ba 100644 --- a/src/buildtool/execution_api/remote/bazel/bazel_action.hpp +++ b/src/buildtool/execution_api/remote/bazel/bazel_action.hpp @@ -17,6 +17,7 @@ #include <map> #include <memory> +#include <optional> #include <string> #include <vector> @@ -62,7 +63,7 @@ class BazelAction final : public IExecutionAction { [[nodiscard]] auto CreateBundlesForAction(BazelBlobContainer* blobs, bazel_re::Digest const& exec_dir, bool do_not_cache) const noexcept - -> bazel_re::Digest; + -> std::optional<bazel_re::Digest>; }; #endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_REMOTE_BAZEL_BAZEL_ACTION_HPP |