From 060d124fd3c307f6e4d76e2b9d80bc6bf7a5cd06 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 12 Apr 2020 18:55:03 +0200 Subject: [PATCH 1/2] src: use basename(argv0) for --trace-uncaught suggestion Refs: https://github.com/nodejs/node/pull/32797#discussion_r407222290 --- src/node_errors.cc | 7 +++++-- src/node_file.cc | 6 ++++++ src/node_internals.h | 4 ++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/node_errors.cc b/src/node_errors.cc index 9bae27550cec1b..720493d6348a63 100644 --- a/src/node_errors.cc +++ b/src/node_errors.cc @@ -375,8 +375,11 @@ static void ReportFatalException(Environment* env, } if (!env->options()->trace_uncaught) { - FPrintF(stderr, "(Use `node --trace-uncaught ...` to show " - "where the exception was thrown)\n"); + std::string argv0; + if (!env->argv().empty()) argv0 = env->argv()[0]; + if (argv0.empty()) argv0 = "node"; + FPrintF(stderr, "(Use `%s --trace-uncaught ...` to show " + "where the exception was thrown)\n", fs::Basename(argv0)); } } diff --git a/src/node_file.cc b/src/node_file.cc index f4d26adf290781..59ef66c8319531 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -82,6 +82,12 @@ constexpr char kPathSeparator = '/'; const char* const kPathSeparator = "\\/"; #endif +std::string Basename(const std::string& str) { + std::string::size_type pos = str.find_last_of(kPathSeparator); + if (pos == std::string::npos) return str; + return str.substr(pos + 1); +} + inline int64_t GetOffset(Local value) { return IsSafeJsInt(value) ? value.As()->Value() : -1; } diff --git a/src/node_internals.h b/src/node_internals.h index dfb2c6e0c37c04..88e75d4da5e7db 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -396,6 +396,10 @@ BaseObjectPtr CreateHeapSnapshotStream( Environment* env, HeapSnapshotPointer&& snapshot); } // namespace heap +namespace fs { +std::string Basename(const std::string& str); +} // namespace fs + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS From 36a3658db634e2a5e4ebf51dec8938f2d8d60526 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 12 Apr 2020 22:55:11 +0200 Subject: [PATCH 2/2] fixup! src: use basename(argv0) for --trace-uncaught suggestion --- src/node_errors.cc | 6 ++++-- src/node_file.cc | 18 ++++++++++++++---- src/node_internals.h | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/node_errors.cc b/src/node_errors.cc index 720493d6348a63..4e13c24e15e1d0 100644 --- a/src/node_errors.cc +++ b/src/node_errors.cc @@ -378,8 +378,10 @@ static void ReportFatalException(Environment* env, std::string argv0; if (!env->argv().empty()) argv0 = env->argv()[0]; if (argv0.empty()) argv0 = "node"; - FPrintF(stderr, "(Use `%s --trace-uncaught ...` to show " - "where the exception was thrown)\n", fs::Basename(argv0)); + FPrintF(stderr, + "(Use `%s --trace-uncaught ...` to show where the exception " + "was thrown)\n", + fs::Basename(argv0, ".exe")); } } diff --git a/src/node_file.cc b/src/node_file.cc index 59ef66c8319531..203ec5c8ca57b1 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -82,10 +82,20 @@ constexpr char kPathSeparator = '/'; const char* const kPathSeparator = "\\/"; #endif -std::string Basename(const std::string& str) { - std::string::size_type pos = str.find_last_of(kPathSeparator); - if (pos == std::string::npos) return str; - return str.substr(pos + 1); +std::string Basename(const std::string& str, const std::string& extension) { + std::string ret = str; + + // Remove everything leading up to and including the final path separator. + std::string::size_type pos = ret.find_last_of(kPathSeparator); + if (pos != std::string::npos) ret = ret.substr(pos + 1); + + // Strip away the extension, if any. + if (ret.size() >= extension.size() && + ret.substr(ret.size() - extension.size()) == extension) { + ret = ret.substr(0, ret.size() - extension.size()); + } + + return ret; } inline int64_t GetOffset(Local value) { diff --git a/src/node_internals.h b/src/node_internals.h index 88e75d4da5e7db..fa3ba022fe7e23 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -397,7 +397,7 @@ BaseObjectPtr CreateHeapSnapshotStream( } // namespace heap namespace fs { -std::string Basename(const std::string& str); +std::string Basename(const std::string& str, const std::string& extension); } // namespace fs } // namespace node