diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2023-06-02 13:23:45 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2023-07-13 16:41:37 +0200 |
commit | a5c8d0fdecf52e94a7b6be44a0356272f133b557 (patch) | |
tree | f68ac2fb49fb3480afd68b4aa45f5ff27e12f59d /src/buildtool/main/main.cpp | |
parent | a7b23d79b236d9b55848f4364bba47e2261c5d6c (diff) | |
download | justbuild-a5c8d0fdecf52e94a7b6be44a0356272f133b557.tar.gz |
just: improve error reporting on missing pwd
... also, gracefully handle the case of a missing working
directory when determining the current module, falling back
to the top-level module.
(cherry-picked from 3a5da87b7185c49567da97cd829733f9bb653b44)
Diffstat (limited to 'src/buildtool/main/main.cpp')
-rw-r--r-- | src/buildtool/main/main.cpp | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp index 79c7c0cb..47337e45 100644 --- a/src/buildtool/main/main.cpp +++ b/src/buildtool/main/main.cpp @@ -520,7 +520,17 @@ void SetupHashFunction() { std::filesystem::path const& workspace_root, FileRoot const& target_root, std::string const& target_file_name) -> std::string { - auto cwd = std::filesystem::current_path(); + std::filesystem::path cwd{}; + try { + cwd = std::filesystem::current_path(); + } catch (std::exception const& e) { + Logger::Log(LogLevel::Warning, + "Cannot determine current working directory ({}), assuming " + "top-level module is requested", + e.what()); + return "."; + } + auto subdir = std::filesystem::proximate(cwd, workspace_root); if (subdir.is_relative() and (*subdir.begin() != "..")) { // cwd is subdir of workspace_root @@ -603,9 +613,18 @@ void SetupHashFunction() { std::move(config)}; } -[[nodiscard]] auto DetermineWorkspaceRootByLookingForMarkers() +[[nodiscard]] auto DetermineWorkspaceRootByLookingForMarkers() noexcept -> std::filesystem::path { - auto cwd = std::filesystem::current_path(); + std::filesystem::path cwd{}; + try { + cwd = std::filesystem::current_path(); + } catch (std::exception const& e) { + Logger::Log(LogLevel::Warning, + "Failed to determine current working directory ({})", + e.what()); + Logger::Log(LogLevel::Error, "Could not determine workspace root."); + std::exit(kExitFailure); + } auto root = cwd.root_path(); cwd = std::filesystem::relative(cwd, root); auto root_dir = FindRoot(cwd, FileRoot{root}, kRootMarkers); |