From a8412f567725395f2039c815eeee85975d6b7aba Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 20 Oct 2022 21:39:25 +0300 Subject: [PATCH] src: forbid running watch mode in REPL PR-URL: https://github.com/nodejs/node/pull/45058 Fixes: https://github.com/nodejs/node/issues/45006 Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum --- src/node.cc | 2 +- src/node_options-inl.h | 2 +- src/node_options.cc | 28 +++++++++++++++------------- src/node_options.h | 15 ++++++++++----- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/node.cc b/src/node.cc index a6a4cb79250db6..1e64784f2218a5 100644 --- a/src/node.cc +++ b/src/node.cc @@ -331,7 +331,7 @@ MaybeLocal StartExecution(Environment* env, StartExecutionCallback cb) { return StartExecution(env, "internal/main/test_runner"); } - if (env->options()->watch_mode && !first_argv.empty()) { + if (env->options()->watch_mode) { return StartExecution(env, "internal/main/watch_mode"); } diff --git a/src/node_options-inl.h b/src/node_options-inl.h index 7facb22afc3c9b..988814c876ee4e 100644 --- a/src/node_options-inl.h +++ b/src/node_options-inl.h @@ -466,7 +466,7 @@ void OptionsParser::Parse( UNREACHABLE(); } } - options->CheckOptions(errors); + options->CheckOptions(errors, orig_args); } } // namespace options_parser diff --git a/src/node_options.cc b/src/node_options.cc index 7ac22c789003e9..43f7e6e6edef0c 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -34,7 +34,8 @@ Mutex cli_options_mutex; std::shared_ptr cli_options{new PerProcessOptions()}; } // namespace per_process -void DebugOptions::CheckOptions(std::vector* errors) { +void DebugOptions::CheckOptions(std::vector* errors, + std::vector* argv) { #if !NODE_USE_V8_PLATFORM && !HAVE_INSPECTOR if (inspector_enabled) { errors->push_back("Inspector is not available when Node is compiled " @@ -64,7 +65,8 @@ void DebugOptions::CheckOptions(std::vector* errors) { } } -void PerProcessOptions::CheckOptions(std::vector* errors) { +void PerProcessOptions::CheckOptions(std::vector* errors, + std::vector* argv) { #if HAVE_OPENSSL if (use_openssl_ca && use_bundled_ca) { errors->push_back("either --use-openssl-ca or --use-bundled-ca can be " @@ -91,14 +93,16 @@ void PerProcessOptions::CheckOptions(std::vector* errors) { use_largepages != "silent") { errors->push_back("invalid value for --use-largepages"); } - per_isolate->CheckOptions(errors); + per_isolate->CheckOptions(errors, argv); } -void PerIsolateOptions::CheckOptions(std::vector* errors) { - per_env->CheckOptions(errors); +void PerIsolateOptions::CheckOptions(std::vector* errors, + std::vector* argv) { + per_env->CheckOptions(errors, argv); } -void EnvironmentOptions::CheckOptions(std::vector* errors) { +void EnvironmentOptions::CheckOptions(std::vector* errors, + std::vector* argv) { if (has_policy_integrity_string && experimental_policy.empty()) { errors->push_back("--policy-integrity requires " "--experimental-policy be enabled"); @@ -161,15 +165,13 @@ void EnvironmentOptions::CheckOptions(std::vector* errors) { if (watch_mode) { if (syntax_check_only) { errors->push_back("either --watch or --check can be used, not both"); - } - - if (has_eval_string) { + } else if (has_eval_string) { errors->push_back("either --watch or --eval can be used, not both"); - } - - if (force_repl) { + } else if (force_repl) { errors->push_back("either --watch or --interactive " "can be used, not both"); + } else if (argv->size() < 1 || (*argv)[1].empty()) { + errors->push_back("--watch requires specifying a file"); } #ifndef ALLOW_ATTACHING_DEBUGGER_IN_WATCH_MODE @@ -214,7 +216,7 @@ void EnvironmentOptions::CheckOptions(std::vector* errors) { heap_prof_dir = diagnostic_dir; } - debug_options_.CheckOptions(errors); + debug_options_.CheckOptions(errors, argv); #endif // HAVE_INSPECTOR } diff --git a/src/node_options.h b/src/node_options.h index 89d69e2b67a9d9..c9b42f6c944e19 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -50,7 +50,8 @@ class HostPort { class Options { public: - virtual void CheckOptions(std::vector* errors) {} + virtual void CheckOptions(std::vector* errors, + std::vector* argv) {} virtual ~Options() = default; }; @@ -99,7 +100,8 @@ class DebugOptions : public Options { return break_first_line || break_node_first_line; } - void CheckOptions(std::vector* errors) override; + void CheckOptions(std::vector* errors, + std::vector* argv) override; }; class EnvironmentOptions : public Options { @@ -203,7 +205,8 @@ class EnvironmentOptions : public Options { inline DebugOptions* get_debug_options() { return &debug_options_; } inline const DebugOptions& debug_options() const { return debug_options_; } - void CheckOptions(std::vector* errors) override; + void CheckOptions(std::vector* errors, + std::vector* argv) override; private: DebugOptions debug_options_; @@ -218,7 +221,8 @@ class PerIsolateOptions : public Options { bool experimental_shadow_realm = false; std::string report_signal = "SIGUSR2"; inline EnvironmentOptions* get_per_env_options(); - void CheckOptions(std::vector* errors) override; + void CheckOptions(std::vector* errors, + std::vector* argv) override; }; class PerProcessOptions : public Options { @@ -291,7 +295,8 @@ class PerProcessOptions : public Options { std::vector cmdline; inline PerIsolateOptions* get_per_isolate_options(); - void CheckOptions(std::vector* errors) override; + void CheckOptions(std::vector* errors, + std::vector* argv) override; }; // The actual options parser, as opposed to the structs containing them: