worker: allow copied NODE_OPTIONS in the env setting · nodejs/node@d6d0427
@@ -549,12 +549,40 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
549549// [0] is expected to be the program name, add dummy string.
550550 env_argv.insert(env_argv.begin(), "");
551551 std::vector<std::string> invalid_args{};
552-options_parser::Parse(&env_argv,
553-nullptr,
554- &invalid_args,
555- per_isolate_opts.get(),
556-kAllowedInEnvvar,
557- &errors);
552+553+ std::string parent_node_options;
554+USE(env->env_vars()->Get("NODE_OPTIONS").To(&parent_node_options));
555+556+// If the worker code passes { env: { ...process.env, ... } } or
557+// the NODE_OPTIONS is otherwise character-for-character equal to the
558+// original NODE_OPTIONS, allow per-process options inherited into
559+// the worker since worker spawning code is not usually in charge of
560+// how the NODE_OPTIONS is configured for the parent.
561+// TODO(joyeecheung): a more intelligent filter may be more desirable.
562+// but a string comparison is good enough(TM) for the case where the
563+// worker spawning code just wants to pass the parent configuration down
564+// and does not intend to modify NODE_OPTIONS.
565+if (parent_node_options == node_options) {
566+// Creates a wrapper per-process option over the per_isolate_opts
567+// to allow per-process options copied from the parent.
568+ std::unique_ptr<PerProcessOptions> per_process_opts =
569+ std::make_unique<PerProcessOptions>();
570+ per_process_opts->per_isolate = per_isolate_opts;
571+options_parser::Parse(&env_argv,
572+nullptr,
573+ &invalid_args,
574+ per_process_opts.get(),
575+kAllowedInEnvvar,
576+ &errors);
577+ } else {
578+options_parser::Parse(&env_argv,
579+nullptr,
580+ &invalid_args,
581+ per_isolate_opts.get(),
582+kAllowedInEnvvar,
583+ &errors);
584+ }
585+558586if (!errors.empty() && args[1]->IsObject()) {
559587// Only fail for explicitly provided env, this protects from failures
560588// when NODE_OPTIONS from parent's env is used (which is the default).