{"record":{"id":"7af01bf3b6b91514","repo":"puppetlabs/puppet","slug":"the-code-loaded-from-source-contains-additional-7af01b","errorCode":null,"errorMessage":"The code loaded from %{source} contains additional logic - can only contain the plan %{plan_name}","messagePattern":"The code loaded from %(.+?) contains additional logic - can only contain the plan %(.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/loader/puppet_plan_instantiator.rb","lineNumber":54,"sourceCode":"      raise ArgumentError, _(\"The code loaded from %{source_ref} does not define the plan '%{plan_name}' - it is empty.\") % { source_ref: source_ref, plan_name: typed_name.name }\n    when 1\n      # ok\n    else\n      raise ArgumentError, _(\"The code loaded from %{source_ref} must contain only the plan '%{plan_name}' - it has additional definitions.\") % { source_ref: source_ref, plan_name: typed_name.name }\n    end\n    the_plan_definition = definitions[0]\n\n    unless the_plan_definition.is_a?(Model::PlanDefinition)\n      raise ArgumentError, _(\"The code loaded from %{source_ref} does not define the plan '%{plan_name}' - no plan found.\") % { source_ref: source_ref, plan_name: typed_name.name }\n    end\n\n    unless the_plan_definition.name == typed_name.name\n      expected = typed_name.name\n      actual = the_plan_definition.name\n      raise ArgumentError, _(\"The code loaded from %{source_ref} produced plan with the wrong name, expected %{expected}, actual %{actual}\") % { source_ref: source_ref, expected: expected, actual: actual }\n    end\n    unless result.body == the_plan_definition\n      raise ArgumentError, _(\"The code loaded from %{source} contains additional logic - can only contain the plan %{plan_name}\") % { source: source_ref, plan_name: typed_name.name }\n    end\n\n    # Adapt the function definition with loader - this is used from logic contained in it body to find the\n    # loader to use when making calls to the new function API. Such logic have a hard time finding the closure (where\n    # the loader is known - hence this mechanism\n    private_loader = loader.private_loader\n    Adapters::LoaderAdapter.adapt(the_plan_definition).loader_name = private_loader.loader_name\n\n    # Cannot bind loaded functions to global scope, that must be done without binding that scope as\n    # loaders survive a compilation.\n    closure_scope = nil\n\n    created = create_function_class(the_plan_definition)\n    # create the function instance - it needs closure (scope), and loader (i.e. where it should start searching for things\n    # when calling functions etc.\n    # It should be bound to global scope\n\n    created.new(closure_scope, private_loader)","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/loader/puppet_plan_instantiator.rb#L36-L72","documentation":"Raised by PuppetPlanInstantiator when the plan file's definition checks pass but the program body is not exactly the plan definition -- there is top-level logic outside the plan statement. A plan file may contain only the plan; any expression before or after it triggers this ArgumentError.","triggerScenarios":"`result.body != the_plan_definition`: e.g. plans/deploy.pp starts with `$msg = 'deploying'` or `notice($msg)` at file scope above/below the `plan` statement.","commonSituations":"Adding debug output at the top of a plan file; leaving stray expressions after refactoring; generators emitting boilerplate around the plan.","solutions":["Remove all statements outside the plan body","Move initialization logic inside the plan (first steps or parameter defaults)","Re-check the file named by source_ref for anything other than the single plan statement"],"exampleFix":"# before - modules/mymod/plans/deploy.pp\n$msg = 'deploying'\nnotice($msg)\nplan mymod::deploy($n) { run_command('uptime', $n) }\n\n# after\nplan mymod::deploy($n) {\n  notice('deploying')\n  run_command('uptime', $n)\n}","handlingStrategy":"validation","validationCode":"ok = model.body == defs[0]\nraise ArgumentError, \"#{source_ref} contains logic outside the plan\" unless ok","typeGuard":null,"tryCatchPattern":"begin\n  loader.load_typed(typed_name)\nrescue ArgumentError => e\n  raise unless e.message.include?('can only contain the plan')\n  warn \"stray top-level logic: #{e.message}\"\nend","preventionTips":["Put all executable steps inside the plan body","Treat any expression outside a definition in plans/ as a lint error","Prefer plan parameters over file-scoped variables"],"tags":["puppet","bolt","plan","loader","puppet-language"],"backgroundTag":"puppet-plan-load-validation","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}