{"record":{"id":"7d86c9706817d5af","repo":"puppetlabs/puppet","slug":"environment-variable-name-must-not-be-nil-or-empty","errorCode":null,"errorMessage":"environment variable name must not be nil or empty","messagePattern":"environment variable name must not be nil or empty","errorType":"exception","errorClass":"Puppet::Util::Windows::Error","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/windows/process.rb","lineNumber":328,"sourceCode":"                     # reject any string containing the Unicode replacement character\n                     if env_str.include?(\"\\uFFFD\")\n                       Puppet.warning(_(\"Discarding environment variable %{string} which contains invalid bytes\") % { string: env_str })\n                       true\n                     end\n                   end\n                   .map { |env_pair| env_pair.split('=', 2) }\n    pairs.to_h\n  ensure\n    if env_ptr && !env_ptr.null?\n      if FreeEnvironmentStringsW(env_ptr) == FFI::WIN32_FALSE\n        Puppet.debug \"FreeEnvironmentStringsW memory leak\"\n      end\n    end\n  end\n  module_function :get_environment_strings\n\n  def set_environment_variable(name, val)\n    raise Puppet::Util::Windows::Error(_('environment variable name must not be nil or empty')) if !name || name.empty?\n\n    FFI::MemoryPointer.from_string_to_wide_string(name) do |name_ptr|\n      if val.nil?\n        if SetEnvironmentVariableW(name_ptr, FFI::MemoryPointer::NULL) == FFI::WIN32_FALSE\n          raise Puppet::Util::Windows::Error, _(\"Failed to remove environment variable: %{name}\") % { name: name }\n        end\n      else\n        FFI::MemoryPointer.from_string_to_wide_string(val) do |val_ptr|\n          if SetEnvironmentVariableW(name_ptr, val_ptr) == FFI::WIN32_FALSE\n            raise Puppet::Util::Windows::Error, _(\"Failed to set environment variable: %{name}\") % { name: name }\n          end\n        end\n      end\n    end\n  end\n  module_function :set_environment_variable\n\n  def get_system_default_ui_language","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/windows/process.rb#L310-L346","documentation":"Guard clause in Process.set_environment_variable (lib/puppet/util/windows/process.rb:328): it raises before any Win32 call when the variable name is nil or empty. A pure caller-contract error — the environment is untouched, and the fix is always in the calling code, not the system.","triggerScenarios":"Calling set_environment_variable(nil, value) or set_environment_variable('', value); names computed from hash or config lookups that returned nil or blank strings.","commonSituations":"Optional configuration feeding environment blocks where a key is silently missing; string interpolation of an unset variable into the name; data-driven provisioning that assumes a key exists.","solutions":["Fix the caller: guarantee a non-empty String name before the call and fail fast with context.","Validate required config keys at load time rather than at use time.","Log the backtrace when it fires — the failure is deterministic and points straight at the bad call site."],"exampleFix":"# before — env_key is nil when the config omits it\nProcess.set_environment_variable(env_key, value)\n\n# after — fail fast with context\nraise ArgumentError, 'environment variable name required' if env_key.to_s.strip.empty?\nProcess.set_environment_variable(env_key, value)","handlingStrategy":"validation","validationCode":"raise ArgumentError, 'env var name must be a non-empty String' unless name.is_a?(String) && !name.empty?\nProcess.set_environment_variable(name, value)","typeGuard":"valid_env_name = ->(n) { n.is_a?(String) && !n.empty? && !n.include?('=') }\nraise ArgumentError, \"bad env var name #{name.inspect}\" unless valid_env_name.call(name)","tryCatchPattern":null,"preventionTips":["Validate required config keys at load time, not at use time","Never interpolate possibly-nil values into environment names","Reject names containing '=' early — Windows forbids them"],"tags":["windows","validation","environment-variables","argument-check","puppet"],"backgroundTag":"invalid-argument-validation","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}