puppetlabs/puppet · error · TypeError

Type mismatch (expect %{rtype} but %{type} present)

Error message

Type mismatch (expect %{rtype} but %{type} present)

What it means

Raised by Registry.read (lib/puppet/util/windows/registry.rb:235) when the caller restricted acceptable types via the rtype splat but the value's actual registry type is not in that list — e.g. read(key, name, Win32::Registry::REG_DWORD) over a REG_SZ value. The check runs before decoding (the method returns [type, data]), so nothing is read in the failing case; it is a TypeError, not a Win32 error.

Source

Thrown at lib/puppet/util/windows/registry.rb:235

    # type is value type. (see Win32::Registry::Constants module)
    # data is value data, its class is:
    # :REG_SZ, REG_EXPAND_SZ
    #    String
    # :REG_MULTI_SZ
    #    Array of String
    # :REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_QWORD
    #    Integer
    # :REG_BINARY
    #    String (contains binary data)
    #
    # When rtype is specified, the value type must be included by
    # rtype array, or TypeError is raised.
    def read(key, name_ptr, *rtype)
      result = nil

      query_value_ex(key, name_ptr) do |type, data_ptr, byte_length|
        unless rtype.empty? or rtype.include?(type)
          raise TypeError, _("Type mismatch (expect %{rtype} but %{type} present)") % { rtype: rtype.inspect, type: type }
        end

        string_length = 0
        # buffer is raw bytes, *not* chars - less a NULL terminator
        string_length = (byte_length / WCHAR_SIZE) - 1 if byte_length > 0

        begin
          result = case type
                   when Win32::Registry::REG_SZ, Win32::Registry::REG_EXPAND_SZ
                     [type, data_ptr.read_wide_string(string_length, Encoding::UTF_8, true)]
                   when Win32::Registry::REG_MULTI_SZ
                     [type, data_ptr.read_wide_string(string_length).split(/\0/)]
                   when Win32::Registry::REG_BINARY
                     [type, data_ptr.read_bytes(byte_length)]
                   when Win32::Registry::REG_DWORD
                     [type, data_ptr.read_dword]
                   when Win32::Registry::REG_DWORD_BIG_ENDIAN
                     [type, data_ptr.order(:big).read_dword]

View on GitHub (pinned to e227c27540)

Solutions

  1. Call read once without rtype to learn the stored type (first element of the returned pair), then decide how to handle it.
  2. Accept the legitimate alternatives in one call: read(key, name, Win32::Registry::REG_SZ, Win32::Registry::REG_EXPAND_SZ).
  3. Re-write the value in the expected type (reg.exe add ... /t REG_DWORD /f) if it must be managed as that type.
  4. In manifests, make the declared registry_value type match what the provider writes, and check the registry view so you read the intended bitness.

Example fix

# before — hard expectation of DWORD
read(key, name_ptr, Win32::Registry::REG_DWORD)

# after — accept string-stored numbers and coerce
type, data = read(key, name_ptr, Win32::Registry::REG_DWORD, Win32::Registry::REG_SZ)
data = data.to_i if type == Win32::Registry::REG_SZ
Defensive patterns

Strategy: type-guard

Type guard

# narrow before the typed read — read without rtype returns [type, data]
type, _data = Registry.read(key, name)
expected = Win32::Registry::REG_DWORD
if type != expected
  Puppet.debug "#{name} stored as #{type}, expected #{expected}"
  # caller branches before attempting a typed read
end

Try / catch

begin
  type, data = read(key, name, *expected_types)
rescue TypeError => e
  Puppet.err "#{key.keyname}\\#{name}: #{e.message}"
  type, data = read(key, name) # fall back to unrestricted read
end

Prevention

When it happens

Trigger: Passing rtype constants that exclude the stored type; installers writing REG_EXPAND_SZ where REG_SZ was declared; string-stored numbers ('1') read as REG_DWORD; values changed outside Puppet between runs; 32/64-bit registry views (KEY_WOW64_32KEY/64KEY) holding different types under the same path.

Common situations: registry_value manifests drifting from reality after manual edits or installer runs; installers switching REG_SZ to REG_EXPAND_SZ; WOW6432NODE redirection surprises.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/470ff779578e75c2. Report an issue: GitHub.