summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/local/local_action.cpp
blob: 8050c3d76b3bd46664155a8118dbc7e20c16512f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/buildtool/execution_api/local/local_action.hpp"

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <exception>
#include <filesystem>
#include <iterator>
#include <memory>
#include <string>
#include <system_error>
#include <utility>

#include "google/protobuf/repeated_ptr_field.h"
#include "nlohmann/json.hpp"
#include "src/buildtool/common/artifact_digest_factory.hpp"
#include "src/buildtool/common/protocol_traits.hpp"
#include "src/buildtool/execution_api/common/ids.hpp"
#include "src/buildtool/execution_api/common/tree_reader.hpp"
#include "src/buildtool/execution_api/local/config.hpp"
#include "src/buildtool/execution_api/local/local_cas_reader.hpp"
#include "src/buildtool/execution_api/local/local_response.hpp"
#include "src/buildtool/execution_api/utils/outputscheck.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/storage/storage.hpp"
#include "src/buildtool/system/system_command.hpp"
#include "src/utils/cpp/expected.hpp"
#include "src/utils/cpp/path.hpp"

namespace {

/// \brief Removes specified directory if KeepBuildDir() is not set.
class BuildCleanupAnchor {
  public:
    explicit BuildCleanupAnchor(std::filesystem::path build_path) noexcept
        : build_path_{std::move(build_path)} {}
    BuildCleanupAnchor(BuildCleanupAnchor const&) = delete;
    BuildCleanupAnchor(BuildCleanupAnchor&&) = delete;
    auto operator=(BuildCleanupAnchor const&) -> BuildCleanupAnchor& = delete;
    auto operator=(BuildCleanupAnchor&&) -> BuildCleanupAnchor& = delete;
    ~BuildCleanupAnchor() {
        if (not FileSystemManager::RemoveDirectory(build_path_)) {
            Logger::Log(LogLevel::Error,
                        "Could not cleanup build directory {}",
                        build_path_.string());
        }
    }

  private:
    std::filesystem::path const build_path_;
};

[[nodiscard]] auto CreateDigestFromLocalOwnedTree(
    Storage const& storage,
    std::filesystem::path const& dir_path) -> std::optional<ArtifactDigest> {
    auto const& cas = storage.CAS();
    auto store_blob = [&cas](std::filesystem::path const& path,
                             auto is_exec) -> std::optional<ArtifactDigest> {
        return cas.StoreBlob</*kOwner=*/true>(path, is_exec);
    };
    auto store_tree =
        [&cas](std::string const& content) -> std::optional<ArtifactDigest> {
        return cas.StoreTree(content);
    };
    auto store_symlink =
        [&cas](std::string const& content) -> std::optional<ArtifactDigest> {
        return cas.StoreBlob(content);
    };
    return ProtocolTraits::IsNative(storage.GetHashFunction().GetType())
               ? BazelMsgFactory::CreateGitTreeDigestFromLocalTree(
                     dir_path, store_blob, store_tree, store_symlink)
               : BazelMsgFactory::CreateDirectoryDigestFromLocalTree(
                     dir_path, store_blob, store_tree, store_symlink);
}

}  // namespace

auto LocalAction::Execute(Logger const* logger) noexcept
    -> IExecutionResponse::Ptr {

    auto do_cache = CacheEnabled(cache_flag_);
    auto const 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,
                     "start execution\n"
                     " - exec_dir digest: {}\n"
                     " - action digest: {}",
                     root_digest_.hash(),
                     action->hash());
    }

    auto create_response = [](Logger const* logger,
                              std::string const& action_hash,
                              auto&&... args) -> IExecutionResponse::Ptr {
        try {
            return IExecutionResponse::Ptr{new LocalResponse{
                action_hash, std::forward<decltype(args)>(args)...}};
        } catch (...) {
            if (logger != nullptr) {
                logger->Emit(LogLevel::Error,
                             "failed to create a response for {}",
                             action_hash);
            }
        }
        return nullptr;
    };

    if (do_cache) {
        if (auto result =
                local_context_.storage->ActionCache().CachedResult(*action)) {
            if (result->exit_code() == 0 and
                (mode_ == RequestMode::kV2_1
                     ? ActionResultContainsExpectedOutputs(*result,
                                                           output_paths_)
                     : ActionResultContainsExpectedOutputs(
                           *result, output_files_, output_dirs_))) {
                return create_response(
                    logger,
                    action->hash(),
                    LocalAction::Output{*std::move(result), /*is_cached=*/true},
                    local_context_.storage);
            }
        }
    }

    if (ExecutionEnabled(cache_flag_)) {
        if (auto output = Run(*action)) {
            if (cache_flag_ == CacheFlag::PretendCached) {
                // ensure the same id is created as if caching were enabled
                auto const 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 create_response(logger,
                                       action_cached->hash(),
                                       *std::move(output),
                                       local_context_.storage);
            }
            return create_response(logger,
                                   action->hash(),
                                   *std::move(output),
                                   local_context_.storage);
        }
    }

    return nullptr;
}

auto LocalAction::Run(ArtifactDigest const& action_id) const noexcept
    -> std::optional<Output> {
    auto const exec_path = CreateUniquePath(
        local_context_.storage_config->ExecutionRoot() / action_id.hash());

    if (not exec_path) {
        return std::nullopt;
    }

    // anchor for cleaning up build directory at end of function (using RAII)
    auto anchor = BuildCleanupAnchor(*exec_path);

    auto const build_root = *exec_path / "build_root";
    if (not CreateDirectoryStructure(build_root)) {
        return std::nullopt;
    }

    if (cmdline_.empty()) {
        logger_.Emit(LogLevel::Error, "malformed command line");
        return std::nullopt;
    }

    // prepare actual command by including the launcher
    auto cmdline = local_context_.exec_config->launcher;
    std::copy(cmdline_.begin(), cmdline_.end(), std::back_inserter(cmdline));

    SystemCommand system{"LocalExecution"};
    auto start_time = std::chrono::system_clock::now();
    auto const exit_code =
        system.Execute(cmdline, env_vars_, build_root / cwd_, *exec_path);
    auto end_time = std::chrono::system_clock::now();
    if (exit_code.has_value()) {
        Output result{};
        result.action.set_exit_code(*exit_code);
        if (auto const digest = DigestFromOwnedFile(*exec_path / "stdout")) {
            *result.action.mutable_stdout_digest() =
                ArtifactDigestFactory::ToBazel(*digest);
        }
        if (auto const digest = DigestFromOwnedFile(*exec_path / "stderr")) {
            *result.action.mutable_stderr_digest() =
                ArtifactDigestFactory::ToBazel(*digest);
        }

        if (CollectAndStoreOutputs(&result.action, build_root / cwd_)) {
            if (cache_flag_ == CacheFlag::CacheOutput) {
                if (not local_context_.storage->ActionCache().StoreResult(
                        action_id, result.action)) {
                    logger_.Emit(LogLevel::Warning,
                                 "failed to store action results");
                }
            }
        }
        result.duration =
            std::chrono::duration<double>(end_time - start_time).count();
        return result;
    }

    logger_.Emit(LogLevel::Error, "failed to execute commands");

    return std::nullopt;
}

auto LocalAction::StageInput(
    std::filesystem::path const& target_path,
    Artifact::ObjectInfo const& info,
    gsl::not_null<LocalAction::FileCopies*> copies) const noexcept -> bool {
    static std::string const kCopyFileName{"blob"};

    if (IsTreeObject(info.type)) {
        return FileSystemManager::CreateDirectory(target_path);
    }

    std::optional<std::filesystem::path> blob_path{};

    if (auto lookup = copies->find(info); lookup != copies->end()) {
        blob_path = lookup->second->GetPath() / kCopyFileName;
    }
    else {
        blob_path = local_context_.storage->CAS().BlobPath(
            info.digest, IsExecutableObject(info.type));
    }

    if (not blob_path) {
        logger_.Emit(LogLevel::Error,
                     "artifact with id {} is missing in CAS",
                     info.digest.hash());
        return false;
    }

    if (info.type == ObjectType::Symlink) {
        auto to =
            FileSystemManager::ReadContentAtPath(*blob_path, ObjectType::File);
        if (not to) {
            logger_.Emit(LogLevel::Error,
                         "could not read content of symlink {}",
                         (*blob_path).string());
            return false;
        }
        return FileSystemManager::CreateSymlink(*to, target_path);
    }

    if (not FileSystemManager::CreateDirectory(target_path.parent_path())) {
        return false;
    }
    auto res = FileSystemManager::CreateFileHardlink(
        *blob_path, target_path, LogLevel::Debug);
    if (res) {
        return true;
    }
    if (res.error() != std::errc::too_many_links) {
        logger_.Emit(LogLevel::Warning,
                     "Failed to link {} to {}: {}, {}",
                     nlohmann::json(blob_path->string()).dump(),
                     nlohmann::json(target_path.string()).dump(),
                     res.error().value(),
                     res.error().message());
        return false;
    }
    TmpDir::Ptr new_copy_dir =
        local_context_.storage_config->CreateTypedTmpDir("blob-copy");
    if (new_copy_dir == nullptr) {
        logger_.Emit(LogLevel::Warning,
                     "Failed to create a temporary directory for a blob copy");
        return false;
    }
    auto new_copy = new_copy_dir->GetPath() / kCopyFileName;
    if (not FileSystemManager::CopyFile(
            *blob_path, new_copy, IsExecutableObject(info.type))) {
        logger_.Emit(LogLevel::Warning,
                     "Failed to create a copy of {}",
                     info.ToString());
        return false;
    }
    if (not FileSystemManager::CreateFileHardlinkAs<true>(
            new_copy, target_path, info.type)) {
        return false;
    }
    try {
        copies->insert_or_assign(info, new_copy_dir);
    } catch (std::exception const& e) {
        logger_.Emit(LogLevel::Warning,
                     "Failed to update temp-copies map (continuing anyway): {}",
                     e.what());
    }
    return true;
}

auto LocalAction::StageInputs(
    std::filesystem::path const& exec_path,
    gsl::not_null<LocalAction::FileCopies*> copies) const noexcept -> bool {
    if (FileSystemManager::IsRelativePath(exec_path)) {
        return false;
    }
    auto reader = TreeReader<LocalCasReader>{&local_context_.storage->CAS()};
    auto result = reader.RecursivelyReadTreeLeafs(
        root_digest_, exec_path, /*include_trees=*/true);
    if (not result) {
        return false;
    }
    for (std::size_t i{}; i < result->paths.size(); ++i) {
        if (not StageInput(result->paths[i], result->infos[i], copies)) {
            return false;
        }
    }
    return true;
}

auto LocalAction::CreateDirectoryStructure(
    std::filesystem::path const& exec_path) const noexcept -> bool {
    // clean execution directory
    if (not FileSystemManager::RemoveDirectory(exec_path)) {
        logger_.Emit(LogLevel::Error, "failed to clean exec_path");
        return false;
    }

    // create process-exclusive execution directory
    if (not FileSystemManager::CreateDirectoryExclusive(exec_path)) {
        logger_.Emit(LogLevel::Error, "failed to exclusively create exec_path");
        return false;
    }

    // stage inputs (files, leaf trees) to execution directory
    {
        LocalAction::FileCopies copies{};
        if (not StageInputs(exec_path, &copies)) {
            logger_.Emit(LogLevel::Error,
                         "failed to stage input files to exec_path");
            return false;
        }
    }

    // create output paths
    auto const create_dir = [this](auto const& dir) {
        if (not FileSystemManager::CreateDirectory(dir)) {
            logger_.Emit(LogLevel::Error, "failed to create output directory");
            return false;
        }
        return true;
    };

    if (mode_ == RequestMode::kV2_1) {
        return std::all_of(
            output_paths_.begin(),
            output_paths_.end(),
            [this, &exec_path, &create_dir](auto const& local_path) {
                auto dir = (exec_path / cwd_ / local_path).parent_path();
                return create_dir(dir);
            });
    }
    return std::all_of(output_files_.begin(),
                       output_files_.end(),
                       [this, &exec_path, &create_dir](auto const& local_path) {
                           auto dir =
                               (exec_path / cwd_ / local_path).parent_path();
                           return create_dir(dir);
                       }) and
           std::all_of(output_dirs_.begin(),
                       output_dirs_.end(),
                       [this, &exec_path, &create_dir](auto const& local_path) {
                           return create_dir(exec_path / cwd_ / local_path);
                       });
}

/// \brief We expect either a regular file, or a symlink.
auto LocalAction::CollectOutputFileOrSymlink(
    std::filesystem::path const& exec_path,
    std::string const& local_path) const noexcept
    -> std::optional<OutputFileOrSymlink> {
    auto file_path = exec_path / local_path;
    auto type = FileSystemManager::Type(file_path, /*allow_upwards=*/true);
    if (not type) {
        logger_.Emit(LogLevel::Error, "expected known type at {}", local_path);
        return std::nullopt;
    }
    if (IsSymlinkObject(*type)) {
        if (auto content = FileSystemManager::ReadSymlink(file_path)) {
            // in native mode: check validity of symlink
            if (ProtocolTraits::IsNative(
                    local_context_.storage->GetHashFunction().GetType()) and
                not PathIsNonUpwards(*content)) {
                logger_.Emit(
                    LogLevel::Error, "found invalid symlink at {}", local_path);
                return std::nullopt;
            }
            if (local_context_.storage->CAS().StoreBlob(*content)) {
                auto out_symlink = bazel_re::OutputSymlink{};
                out_symlink.set_path(local_path);
                out_symlink.set_target(*content);
                return out_symlink;
            }
        }
    }
    else if (IsFileObject(*type)) {
        bool is_executable = IsExecutableObject(*type);
        auto digest = local_context_.storage->CAS().StoreBlob</*kOwner=*/true>(
            file_path, is_executable);
        if (digest) {
            auto out_file = bazel_re::OutputFile{};
            out_file.set_path(local_path);
            *out_file.mutable_digest() =
                ArtifactDigestFactory::ToBazel(*digest);
            out_file.set_is_executable(is_executable);
            return out_file;
        }
    }
    else {
        logger_.Emit(
            LogLevel::Error, "expected file or symlink at {}", local_path);
    }
    return std::nullopt;
}

auto LocalAction::CollectOutputDirOrSymlink(
    std::filesystem::path const& exec_path,
    std::string const& local_path) const noexcept
    -> std::optional<OutputDirOrSymlink> {
    auto dir_path = exec_path / local_path;
    auto type = FileSystemManager::Type(dir_path, /*allow_upwards=*/true);
    if (not type) {
        logger_.Emit(LogLevel::Error, "expected known type at {}", local_path);
        return std::nullopt;
    }
    if (IsSymlinkObject(*type)) {
        if (auto content = FileSystemManager::ReadSymlink(dir_path)) {
            // in native mode: check validity of symlink
            if (ProtocolTraits::IsNative(
                    local_context_.storage->GetHashFunction().GetType()) and
                not PathIsNonUpwards(*content)) {
                logger_.Emit(
                    LogLevel::Error, "found invalid symlink at {}", local_path);
                return std::nullopt;
            }
            if (local_context_.storage->CAS().StoreBlob(*content)) {
                auto out_symlink = bazel_re::OutputSymlink{};
                out_symlink.set_path(local_path);
                out_symlink.set_target(*content);
                return out_symlink;
            }
        }
    }
    else if (IsTreeObject(*type)) {
        if (auto digest = CreateDigestFromLocalOwnedTree(
                *local_context_.storage, dir_path)) {
            auto out_dir = bazel_re::OutputDirectory{};
            out_dir.set_path(local_path);
            (*out_dir.mutable_tree_digest()) =
                ArtifactDigestFactory::ToBazel(*digest);
            return out_dir;
        }
        logger_.Emit(LogLevel::Error,
                     "found invalid entries in directory at {}",
                     local_path);
    }
    else {
        logger_.Emit(
            LogLevel::Error, "expected directory or symlink at {}", local_path);
    }
    return std::nullopt;
}

auto LocalAction::CollectOutputPath(
    std::filesystem::path const& exec_path,
    std::string const& local_path) const noexcept -> std::optional<OutputPath> {
    auto out_path = exec_path / local_path;
    auto type = FileSystemManager::Type(out_path, /*allow_upwards=*/true);
    if (not type) {
        logger_.Emit(LogLevel::Error, "expected known type at {}", local_path);
        return std::nullopt;
    }
    if (IsSymlinkObject(*type)) {
        if (auto content = FileSystemManager::ReadSymlink(out_path)) {
            // in native mode: check validity of symlink
            if (ProtocolTraits::IsNative(
                    local_context_.storage->GetHashFunction().GetType()) and
                not PathIsNonUpwards(*content)) {
                logger_.Emit(
                    LogLevel::Error, "found invalid symlink at {}", local_path);
                return std::nullopt;
            }
            if (local_context_.storage->CAS().StoreBlob(*content)) {
                auto out_symlink = bazel_re::OutputSymlink{};
                out_symlink.set_path(local_path);
                out_symlink.set_target(*content);
                return out_symlink;
            }
        }
    }
    else if (IsFileObject(*type)) {
        bool is_executable = IsExecutableObject(*type);
        auto digest = local_context_.storage->CAS().StoreBlob</*kOwner=*/true>(
            out_path, is_executable);
        if (digest) {
            auto out_file = bazel_re::OutputFile{};
            out_file.set_path(local_path);
            *out_file.mutable_digest() =
                ArtifactDigestFactory::ToBazel(*digest);
            out_file.set_is_executable(is_executable);
            return out_file;
        }
    }
    else if (IsTreeObject(*type)) {
        if (auto digest = CreateDigestFromLocalOwnedTree(
                *local_context_.storage, out_path)) {
            auto out_dir = bazel_re::OutputDirectory{};
            out_dir.set_path(local_path);
            (*out_dir.mutable_tree_digest()) =
                ArtifactDigestFactory::ToBazel(*digest);
            return out_dir;
        }
        logger_.Emit(LogLevel::Error,
                     "found invalid entries in directory at {}",
                     local_path);
    }
    else {
        logger_.Emit(LogLevel::Error,
                     "expected file, directory, or symlink at {}",
                     local_path);
    }
    return std::nullopt;
}

auto LocalAction::CollectAndStoreOutputs(
    bazel_re::ActionResult* result,
    std::filesystem::path const& exec_path) const noexcept -> bool {
    try {
        logger_.Emit(LogLevel::Trace, "collecting outputs:");
        if (mode_ == RequestMode::kV2_1) {
            for (auto const& path : output_paths_) {
                auto out = CollectOutputPath(exec_path, path);
                if (not out) {
                    logger_.Emit(LogLevel::Error,
                                 "could not collect output path {}",
                                 path);
                    return false;
                }
                if (std::holds_alternative<bazel_re::OutputSymlink>(*out)) {
                    auto out_symlink = std::get<bazel_re::OutputSymlink>(*out);
                    logger_.Emit(LogLevel::Trace,
                                 " - symlink {}: {}",
                                 path,
                                 out_symlink.target());
                    result->mutable_output_symlinks()->Add(
                        std::move(out_symlink));
                }
                else if (std::holds_alternative<bazel_re::OutputFile>(*out)) {
                    auto out_file = std::get<bazel_re::OutputFile>(*out);
                    auto const& digest = out_file.digest().hash();
                    logger_.Emit(
                        LogLevel::Trace, " - file {}: {}", path, digest);
                    result->mutable_output_files()->Add(std::move(out_file));
                }
                else {
                    auto out_dir = std::get<bazel_re::OutputDirectory>(*out);
                    auto const& digest = out_dir.tree_digest().hash();
                    logger_.Emit(
                        LogLevel::Trace, " - dir {}: {}", path, digest);
                    result->mutable_output_directories()->Add(
                        std::move(out_dir));
                }
            }
            return true;
        }
        // if mode_ is RequestMode::kV2_0 or RequestMode::kBestEffort
        for (auto const& path : output_files_) {
            auto out = CollectOutputFileOrSymlink(exec_path, path);
            if (not out) {
                logger_.Emit(LogLevel::Error,
                             "could not collect output file or symlink {}",
                             path);
                return false;
            }
            if (std::holds_alternative<bazel_re::OutputSymlink>(*out)) {
                auto out_symlink = std::get<bazel_re::OutputSymlink>(*out);
                logger_.Emit(LogLevel::Trace,
                             " - symlink {}: {}",
                             path,
                             out_symlink.target());
                if (mode_ == RequestMode::kBestEffort) {
                    *result->mutable_output_symlinks()->Add() = out_symlink;
                }
                result->mutable_output_file_symlinks()->Add(
                    std::move(out_symlink));
            }
            else {
                auto out_file = std::get<bazel_re::OutputFile>(*out);
                auto const& digest = out_file.digest().hash();
                logger_.Emit(LogLevel::Trace, " - file {}: {}", path, digest);
                result->mutable_output_files()->Add(std::move(out_file));
            }
        }
        for (auto const& path : output_dirs_) {
            auto out = CollectOutputDirOrSymlink(exec_path, path);
            if (not out) {
                logger_.Emit(LogLevel::Error,
                             "could not collect output dir or symlink {}",
                             path);
                return false;
            }
            if (std::holds_alternative<bazel_re::OutputSymlink>(*out)) {
                auto out_symlink = std::get<bazel_re::OutputSymlink>(*out);
                logger_.Emit(LogLevel::Trace,
                             " - symlink {}: {}",
                             path,
                             out_symlink.target());
                if (mode_ == RequestMode::kBestEffort) {
                    *result->mutable_output_symlinks()->Add() = out_symlink;
                }
                result->mutable_output_directory_symlinks()->Add(
                    std::move(out_symlink));
            }
            else {
                auto out_dir = std::get<bazel_re::OutputDirectory>(*out);
                auto const& digest = out_dir.tree_digest().hash();
                logger_.Emit(LogLevel::Trace, " - dir {}: {}", path, digest);
                result->mutable_output_directories()->Add(std::move(out_dir));
            }
        }
        return true;
    } catch (std::exception const& ex) {
        // should never happen, but report nonetheless
        logger_.Emit(
            LogLevel::Error, "collecting outputs failed:\n{}", ex.what());
        return false;
    }
}

auto LocalAction::DigestFromOwnedFile(std::filesystem::path const& file_path)
    const noexcept -> std::optional<ArtifactDigest> {
    return local_context_.storage->CAS().StoreBlob</*kOwner=*/true>(
        file_path, /*is_executable=*/false);
}