puppetlabs/puppet · error · ArgumentError

The code loaded from %{source_ref} does not define the type

Error message

The code loaded from %{source_ref} does not define the type '%{name}' - no type alias or type definition found.

What it means

Raised by TypeDefinitionInstantiator.create when the file has exactly one definition, but that definition is neither a Model::TypeAlias nor a Model::TypeDefinition. The types/ directory may only contain type aliases (`type X = ...`) or object type definitions (`type X << ... >>`); a lone class, define, function, or site statement in that slot fails this check.

Source

Thrown at lib/puppet/pops/loader/type_definition_instantiator.rb:27

    # parse and validate
    parser = Parser::EvaluatingParser.new()
    model = parser.parse_string(pp_code_string, source_ref)
    # Only one type is allowed (and no other definitions)

    name = typed_name.name
    case model.definitions.size
    when 0
      raise ArgumentError, _("The code loaded from %{source_ref} does not define the type '%{name}' - it is empty.") % { source_ref: source_ref, name: name }
    when 1
      # ok
    else
      raise ArgumentError,
            _("The code loaded from %{source_ref} must contain only the type '%{name}' - it has additional definitions.") % { source_ref: source_ref, name: name }
    end
    type_definition = model.definitions[0]

    unless type_definition.is_a?(Model::TypeAlias) || type_definition.is_a?(Model::TypeDefinition)
      raise ArgumentError,
            _("The code loaded from %{source_ref} does not define the type '%{name}' - no type alias or type definition found.") % { source_ref: source_ref, name: name }
    end

    actual_name = type_definition.name
    unless name == actual_name.downcase
      raise ArgumentError,
            _("The code loaded from %{source_ref} produced type with the wrong name, expected '%{name}', actual '%{actual_name}'") % { source_ref: source_ref, name: name, actual_name: actual_name }
    end

    unless model.body == type_definition
      raise ArgumentError,
            _("The code loaded from %{source_ref} contains additional logic - can only contain the type '%{name}'") % { source_ref: source_ref, name: name }
    end

    # Adapt the type definition with loader - this is used from logic contained in its body to find the
    # loader to use when resolving contained aliases API. Such logic have a hard time finding the closure (where
    # the loader is known - hence this mechanism
    private_loader = loader.private_loader

View on GitHub (pinned to e227c27540)

Solutions

  1. Move class/define manifests out of types/ into manifests/.
  2. Replace the content with a real type alias or type definition matching the file name.
  3. If the type is not needed, delete the file so lookups of that name fail cleanly instead of loading junk.

Example fix

// before: modules/mymodule/types/mytype.pp
class mymodule::mytype ($x) { notify { $x: } }

// after: modules/mymodule/types/mytype.pp
type mymodule::mytype = String[1, 50]
Defensive patterns

Strategy: validation

Validate before calling

# CI check: the single definition in types/*.pp must be a type alias or type definition
require 'puppet'
require 'puppet/pops'

Dir['modules/*/types/**/*.pp'].each do |f|
  model = Puppet::Pops::Parser::EvaluatingParser.new.parse_string(File.read(f), f)
  d = model.definitions[0]
  next if d && (d.is_a?(Puppet::Pops::Model::TypeAlias) || d.is_a?(Puppet::Pops::Model::TypeDefinition))
  abort "#{f}: contains a #{d.class} — types/ only allows type aliases / type definitions"
end

Prevention

When it happens

Trigger: types/mytype.pp whose only content is `class mymodule::mytype { ... }` or `define mymodule::mytype (...) { ... }`, then any Puppet code or resource declaration that causes the loader to autoload Mod::Mytype as a type.

Common situations: Misreading the types/ directory purpose and putting a class manifest there; converting a class to a type and leaving the class in place; file committed into the wrong directory.

Related errors


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