{"record":{"id":"707a9b575fbb1a36","repo":"puppetlabs/puppet","slug":"a-block-must-be-given-to-in-environment","errorCode":null,"errorMessage":"A block must be given to 'in_environment'","messagePattern":"A block must be given to 'in_environment'","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pal/pal_impl.rb","lineNumber":295,"sourceCode":"  def self.in_environment(env_name,\n                          modulepath: nil,\n                          pre_modulepath: [],\n                          post_modulepath: [],\n                          settings_hash: {},\n                          env_dir:       nil,\n                          envpath:       nil,\n                          facts:         nil,\n                          variables:     {},\n                          &block)\n    # TRANSLATORS terms in the assertions below are names of terms in code\n    assert_non_empty_string(env_name, 'env_name')\n    assert_optionally_empty_array(modulepath, 'modulepath', true)\n    assert_optionally_empty_array(pre_modulepath, 'pre_modulepath', false)\n    assert_optionally_empty_array(post_modulepath, 'post_modulepath', false)\n    assert_mutually_exclusive(env_dir, envpath, 'env_dir', 'envpath')\n\n    unless block_given?\n      raise ArgumentError, _(\"A block must be given to 'in_environment'\") # TRANSLATORS 'in_environment' is a name, do not translate\n    end\n\n    if env_dir\n      unless Puppet::FileSystem.exist?(env_dir)\n        raise ArgumentError, _(\"The environment directory '%{env_dir}' does not exist\") % { env_dir: env_dir }\n      end\n\n      # a nil modulepath for env_dir means it should use its ./modules directory\n      mid_modulepath = modulepath.nil? ? [Puppet::FileSystem.expand_path(File.join(env_dir, 'modules'))] : modulepath\n\n      env = Puppet::Node::Environment.create(env_name, pre_modulepath + mid_modulepath + post_modulepath)\n      environments = Puppet::Environments::StaticDirectory.new(env_name, env_dir, env) # The env being used is the only one...\n    else\n      assert_non_empty_string(envpath, 'envpath')\n\n      # The environment is resolved against the envpath. This is setup without a basemodulepath\n      # The modulepath defaults to the 'modulepath' in the found env when \"Directories\" is used\n      #","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pal/pal_impl.rb#L277-L313","documentation":"Puppet::Pal.in_environment requires a block for the same reason as the other PAL entry points: the environment context (resolved from env_dir or envpath) is established only for the block's duration. Missing block raises ArgumentError.","triggerScenarios":"Puppet::Pal.in_environment('production', envpath: path) without a block; delegating wrappers that lose the block.","commonSituations":"Refactoring PAL calls into helper methods and dropping the &block parameter; scripting examples run partially.","solutions":["Supply the block that receives the pal compiler context","Forward &block through every wrapper layer","Fail fast in your wrapper with a clearer message using block_given?"],"exampleFix":"# before\nPuppet::Pal.in_environment('prod', env_dir: dir)\n\n# after\nPuppet::Pal.in_environment('prod', env_dir: dir) do |pal|\n  # compile / evaluate\nend","handlingStrategy":"validation","validationCode":"raise ArgumentError, 'block required' unless block_given?\nPuppet::Pal.in_environment(name, env_dir: dir, &block)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat all Puppet::Pal entry points as block-scoped by contract","Cover PAL wrappers with a smoke test that always passes a block"],"tags":["puppet","pal","ruby","block"],"backgroundTag":"missing-block-argument","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}