{"record":{"id":"c00cb03aed678cc7","repo":"puppetlabs/puppet","slug":"failed-to-set-environment-variable-name","errorCode":null,"errorMessage":"Failed to set environment variable: %{name}","messagePattern":"Failed to set environment variable: %(.+?)","errorType":"exception","errorClass":"Puppet::Util::Windows::Error","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/windows/process.rb","lineNumber":338,"sourceCode":"      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\n    GetSystemDefaultUILanguage()\n  end\n  module_function :get_system_default_ui_language\n\n  # Returns whether or not the OS has the ability to set elevated\n  # token information.\n  #\n  # Returns true on Windows Vista or later, otherwise false\n  #\n  def supports_elevated_security?","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/windows/process.rb#L320-L356","documentation":"Raised by set_environment_variable (lib/puppet/util/windows/process.rb:338) when SetEnvironmentVariableW(name, value) returns FALSE while setting (not removing) a variable. Typical causes: name or value exceeding the 32,767-character environment limit, an '=' embedded in the name, or the process environment block being exhausted by bulk updates.","triggerScenarios":"Writing very long values (serialized JSON, giant PATH-style strings, tokens) into a variable; names assembled from unvalidated input containing '='; hundreds of variables set in one run hitting environment block limits.","commonSituations":"CI/build runners pushing credentials or payloads through env vars; Puppet providers exporting large environment blocks to child processes; secrets managers overflowing into env values.","solutions":["Validate sizes before the call: keep name and value under 32K characters each.","Move oversized payloads to a temp file and pass the path via the env var.","Reject or sanitize '=' in names and strip stray whitespace.","Log name/value lengths when it fires to confirm the limit is the cause."],"exampleFix":"# before\nProcess.set_environment_variable(name, huge_json)\n\n# after — spill to a file when the value is large\nif value.length > 32_000\n  file = File.join(Dir.tmpdir, \"#{name}.payload\")\n  File.write(file, value)\n  Process.set_environment_variable(name, file)\nelse\n  Process.set_environment_variable(name, value)\nend","handlingStrategy":"validation","validationCode":"raise ArgumentError, 'env name invalid' if name.empty? || name.include?('=')\nraise ArgumentError, \"env value too long (#{value.length})\" if value.length > 32_767\nProcess.set_environment_variable(name, value)","typeGuard":"settable_env_pair = ->(n, v) { n.is_a?(String) && !n.empty? && !n.include?('=') && v.is_a?(String) && v.length <= 32_767 }","tryCatchPattern":null,"preventionTips":["Cap environment payloads and spill oversized values to files","Reject '=' in names at input parsing","Set the biggest values first so failures happen during setup, not mid-run"],"tags":["windows","win32-api","environment-variables","limits","validation","puppet"],"backgroundTag":"win32-setenvironmentvariable-failed","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}