Repository: bitcoin/bitcoin
File: src/bitcoin.cpp (C++)

Code before:
                fs::path candidate = fs::path(paths.substr(start, end - start)) / argv0_path;
                if (fs::exists(candidate, ec) && fs::is_regular_file(candidate, ec)) {
                    exe_path = candidate;
                    break;
                }
            }
        }
    }
#else
    wchar_t module_path[MAX_PATH];
    if (GetModuleFileNameW(nullptr, module_path, MAX_PATH) > 0) {
        exe_path = fs::path{module_path};
    } else {
        tfm::format(std::cerr, "Warning: Failed to get executable path. Error: %s\n", GetLastError());
    }
#endif

    // Try to resolve any symlinks and figure out actual directory containing this executable.
    fs::path exe_dir{fs::weakly_canonical(exe_path, ec)};
    if (exe_dir.empty()) exe_dir = exe_path; // Restore previous path if weakly_canonical failed.
    exe_dir = exe_dir.parent_path();
    // Search for executables on system PATH only if this executable was invoked
    // from the PATH, to avoid unintentionally launching system executables in a
    // local build
    // (https://github.com/bitcoin/bitcoin/pull/31375#discussion_r1861814807)
    const bool use_system_path{!argv0_path.has_parent_path()};
    const fs::path arg0{fs::PathFromString(args[0])};

    // If exe is in a CMake build tree, first look for target executable
    // relative to it.
    (exe_dir.filename() == "src" && try_exec(exe_dir / arg0)) ||
    // Otherwise if exe is installed in a bin/ directory, first look for target
    // executable in libexec/
    (exe_dir.filename() == "bin" && try_exec(fs::path{exe_dir.parent_path()} / "libexec" / arg0.filename())) ||
    // Otherwise look for target executable next to current exe
    try_exec(exe_dir / arg0.filename(), use_system_path) ||
    // Otherwise just look on the system path.
    (use_system_path && try_exec(arg0.filename(), false));
};


Review comment (suggestion):
I think this is the same check as above: `argv0.find('/') == std::string_view::npos`, right? Would be good to use the same expression in both locations.

Code after:
    const fs::path wrapper_path{util::GetExePath(wrapper_argv0)};

    // Try to resolve any symlinks and figure out the directory containing the wrapper executable.
    std::error_code ec;
    fs::path wrapper_dir{fs::weakly_canonical(wrapper_path, ec)};
    if (wrapper_dir.empty()) wrapper_dir = wrapper_path; // Restore previous path if weakly_canonical failed.
    wrapper_dir = wrapper_dir.parent_path();

    // Get path of the executable to be invoked.
    const fs::path arg0{fs::PathFromString(args[0])};

    // Decide whether to fall back to the operating system to search for the
    // specified executable. Avoid doing this if it looks like the wrapper
    // executable was invoked by path, rather than by search, to avoid
    // unintentionally launching system executables in a local build.
    // (https://github.com/bitcoin/bitcoin/pull/31375#discussion_r1861814807)
    const bool fallback_os_search{!fs::PathFromString(std::string{wrapper_argv0}).has_parent_path()};

    // If wrapper is installed in a bin/ directory, look for target executable
    // in libexec/
    (wrapper_dir.filename() == "bin" && try_exec(fs::path{wrapper_dir.parent_path()} / "libexec" / arg0.filename())) ||
#ifdef WIN32
    // Otherwise check the "daemon" subdirectory in a windows install.
    (!wrapper_dir.empty() && try_exec(wrapper_dir / "daemon" / arg0.filename())) ||
#endif
    // Otherwise look for target executable next to current wrapper
    (!wrapper_dir.empty() && try_exec(wrapper_dir / arg0.filename(), fallback_os_search)) ||
    // Otherwise just look on the system path.
    (fallback_os_search && try_exec(arg0.filename(), false));
}