From efc9f253ee2d3e16cfb95331ea5234d2b237dca1 Mon Sep 17 00:00:00 2001 From: Kris Austin Date: Mon, 14 Sep 2026 23:47:01 -0500 Subject: [PATCH] fix: resolve relative input paths given on the command line (#14803) Opening a model with a relative path, for example `orca-slicer ./some.3mf`, failed with "Loading of a model file failed." and "The file does not contain any geometry data.", while the same file opened by an absolute path or by drag and drop worked. GUI_App::init_app_config() changes the working directory to /log, and it runs from the GUI_App constructor because the app config is needed early for instance checking. The input files are opened much later, in post_init(), so a path still relative at that point resolved against the log directory instead of the directory OrcaSlicer was started from, and the 3MF reader failed to open it. Resolve the input paths in CLI::setup(), which runs before GUI_App is constructed and therefore before the working directory moves. Absolute paths are returned unchanged, so the forms that open today are unaffected, and custom open protocol URLs are passed through since post_init() hands those to the downloader rather than the file loader. The working directory change is left alone. It was added in #3248 so the TUTK logs land in the data directory instead of the working directory (#3209). --- src/OrcaSlicer.cpp | 7 ++++ src/libslic3r/Utils.hpp | 3 ++ src/libslic3r/utils.cpp | 13 +++++++ tests/libslic3r/test_utils.cpp | 64 ++++++++++++++++++++++++++++++++++ tests/test_utils.hpp | 18 ++++++++++ 5 files changed, 105 insertions(+) diff --git a/src/OrcaSlicer.cpp b/src/OrcaSlicer.cpp index b75c653eda..24f218caa5 100644 --- a/src/OrcaSlicer.cpp +++ b/src/OrcaSlicer.cpp @@ -7715,6 +7715,13 @@ bool CLI::setup(int argc, char **argv) this->print_help(); return false; } + + // Orca: resolve here, while the process is still in the directory the user invoked it from. + // GUI_App's constructor moves the working directory to /log, long before the GUI + // opens these files in post_init(), and a relative path would then resolve against that. + for (std::string &input_file : m_input_files) + input_file = resolve_cli_input_path(input_file); + // Parse actions and transform options. for (auto const &opt_key : opt_order) { if (cli_actions_config_def.has(opt_key)) diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index c364860531..b21da72fc8 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -314,6 +314,9 @@ extern unsigned get_current_pid(); std::string per_user_temp_id(); // Per-user temp root under `base`; an empty `user_id` returns `base` unchanged. std::string per_user_temp_dir(const std::string &base, const std::string &user_id); +// Completes a relative command line input path against the current working directory. Absolute +// paths and custom open protocol URLs are returned unchanged. +std::string resolve_cli_input_path(const std::string &path); // BBS: backup & restore std::string get_process_name(int pid); diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index 58323b29ce..9def5dad17 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -1339,6 +1339,19 @@ std::string per_user_temp_dir(const std::string &base, const std::string &user_i return base + "/orcaslicer_" + user_id; } +std::string resolve_cli_input_path(const std::string &path) +{ + const boost::filesystem::path input(path); + if (path.empty() || is_supported_open_protocol(path) || input.is_absolute()) + return path; + + boost::system::error_code ec; + const boost::filesystem::path resolved = boost::filesystem::system_complete(input, ec); + if (ec) + return path; + return resolved.lexically_normal().make_preferred().string(); +} + // BBS: backup & restore std::string get_process_name(int pid) { diff --git a/tests/libslic3r/test_utils.cpp b/tests/libslic3r/test_utils.cpp index 484438127c..7880b783f1 100644 --- a/tests/libslic3r/test_utils.cpp +++ b/tests/libslic3r/test_utils.cpp @@ -4,6 +4,8 @@ #include "test_utils.hpp" +#include + #include #include #include @@ -88,3 +90,65 @@ TEST_CASE("copy_file reports the OS error when the destination cannot be written REQUIRE(std::all_of(code.begin(), code.end(), [](unsigned char c) { return std::isdigit(c) != 0; })); #endif // _WIN32 } + +TEST_CASE("A resolved input path still names the same file after the working directory changes", "[utils]") { + ScopedTemporaryFile model(".3mf"); + { std::ofstream out(model.string()); out << "3mf"; } + const std::string name = model.path().filename().string(); + + // Resolve the bare name from the directory holding the file, then move away from it. The guard + // restores the directory the test started in, wherever this leaves it. + ScopedWorkingDirectory cwd(model.path().parent_path()); + const std::string resolved = resolve_cli_input_path(name); + boost::filesystem::current_path(boost::filesystem::path(TEST_DATA_DIR)); + + REQUIRE(boost::filesystem::exists(resolved)); + REQUIRE(boost::filesystem::equivalent(resolved, model.path())); + // Control: the bare name finds nothing from here, so resolving it this late would have failed. + REQUIRE_FALSE(boost::filesystem::exists(name)); +} + +TEST_CASE("resolve_cli_input_path completes a relative path against the working directory", "[utils]") { + ScopedWorkingDirectory cwd(boost::filesystem::temp_directory_path()); + // Read back rather than reusing temp_directory_path(): changing to it resolves any symlink. + const boost::filesystem::path here = boost::filesystem::current_path(); + + SECTION("a bare name") { + REQUIRE(resolve_cli_input_path("model.3mf") == (here / "model.3mf").make_preferred().string()); + } + SECTION("a ./ prefix is dropped") { + REQUIRE(resolve_cli_input_path("./model.3mf") == (here / "model.3mf").make_preferred().string()); + } + SECTION("a ../ traversal is collapsed") { + REQUIRE(resolve_cli_input_path("../model.3mf") == (here.parent_path() / "model.3mf").make_preferred().string()); + } +} + +TEST_CASE("resolve_cli_input_path leaves inputs that must not be completed unchanged", "[utils]") { + SECTION("an absolute path") { + const boost::filesystem::path absolute = (boost::filesystem::temp_directory_path() / "model.3mf").make_preferred(); + REQUIRE(resolve_cli_input_path(absolute.string()) == absolute.string()); + } +#ifdef _WIN32 + // Every absolute form Windows accepts opens today, so each must come back byte for byte: + // normalizing them would rewrite the forward slashes and rebuild the \\?\ and UNC prefixes. + SECTION("an absolute Windows path of any form") { + for (const std::string absolute : {R"(C:\models\model.3mf)", + R"(C:/models/model.3mf)", + R"(\\server\share\model.3mf)", + R"(\\?\C:\models\model.3mf)"}) + REQUIRE(resolve_cli_input_path(absolute) == absolute); + } +#endif + // These are downloaded rather than opened, and completing one would produce a path, not a URL. + SECTION("a custom open protocol URL") { + for (const std::string url : {"orcaslicer://open/?file=https://example.com/model.3mf", + "prusaslicer://open/?file=https://example.com/model.3mf", + "bambustudio://open/?file=https://example.com/model.3mf", + "cura://open/?file=https://example.com/model.3mf"}) + REQUIRE(resolve_cli_input_path(url) == url); + } + SECTION("an empty argument") { + REQUIRE(resolve_cli_input_path("").empty()); + } +} diff --git a/tests/test_utils.hpp b/tests/test_utils.hpp index e3fbbe8fab..0b04e6ad11 100644 --- a/tests/test_utils.hpp +++ b/tests/test_utils.hpp @@ -176,4 +176,22 @@ inline void write_debug_stream([[maybe_unused]] const std::string &name, [[maybe #endif } +// Changes the working directory and restores the previous one on scope exit, including when an +// assertion throws. It is process wide state shared with every other test. +class ScopedWorkingDirectory +{ +public: + explicit ScopedWorkingDirectory(const boost::filesystem::path &dir) + : m_previous(boost::filesystem::current_path()) + { + boost::filesystem::current_path(dir); + } + ~ScopedWorkingDirectory() { boost::system::error_code ec; boost::filesystem::current_path(m_previous, ec); } + ScopedWorkingDirectory(const ScopedWorkingDirectory &) = delete; + ScopedWorkingDirectory &operator=(const ScopedWorkingDirectory &) = delete; + +private: + boost::filesystem::path m_previous; +}; + #endif // SLIC3R_TEST_UTILS