{"record":{"id":"512cdb08193a53fa","repo":"puppetlabs/puppet","slug":"section-name-is-already-defined-cannot-redefin","errorCode":null,"errorMessage":"Section %{name} is already defined, cannot redefine","messagePattern":"Section %(.+?) is already defined, cannot redefine","errorType":"exception","errorClass":"Puppet::Util::IniConfig::IniParseError","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/inifile.rb","lineNumber":242,"sourceCode":"\n    def store\n      if @destroy_empty and (sections.empty? or sections.all?(&:destroy?))\n        ::File.unlink(@file)\n      elsif sections.any?(&:dirty?)\n        text = self.format\n        @filetype.write(text)\n      end\n      sections.each(&:mark_clean)\n    end\n\n    # Create a new section and store it in the file contents\n    #\n    # @api private\n    # @param name [String] The name of the section to create\n    # @return [Puppet::Util::IniConfig::Section]\n    def add_section(name)\n      if section_exists?(name)\n        raise IniParseError.new(_(\"Section %{name} is already defined, cannot redefine\") % { name: name.inspect }, @file)\n      end\n\n      section = Section.new(name, @file)\n      @contents << section\n\n      section\n    end\n\n    private\n\n    def section_exists?(name)\n      if get_section(name)\n        true\n      elsif @file_collection and @file_collection.get_section(name)\n        true\n      else\n        false\n      end","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/inifile.rb#L224-L260","documentation":"IniFile#add_section raises IniParseError when a section with that name already exists (section_exists? found it via get_section). Crucially, #parse itself calls add_section for every [name] header, so an INI file containing the same section header twice fails with this error at read time. Puppet deliberately refuses to silently merge duplicate sections.","triggerScenarios":"Reading an INI file with duplicated headers (two [main] blocks); calling ini_file.add_section('agent') when the file's sections already include one named 'agent'; appends (cat a >> b) producing repeated headers.","commonSituations":"Hand-merged puppet.conf-style files keeping both copies of a section; deployment scripts that unconditionally add a section on every run; configuration templates instantiated twice into one file.","solutions":["De-duplicate the section headers in the file: merge the key/value pairs of the repeats into a single block","In code, guard the call: only add_section when ini.sections has no section with that name","Use the existing section instead of adding: fetch it from ini.sections and mutate its entries","Rescue IniParseError when section names come from untrusted input; the error carries @file for reporting"],"exampleFix":"# before\nini.add_section('agent')  # IniParseError if [agent] already present\n\n# after\nsection = ini.sections.find { |s| s.name == 'agent' } || ini.add_section('agent')\nsection['server'] = 'puppet.example.com'","handlingStrategy":"validation","validationCode":"unless ini.sections.any? { |s| s.name == wanted }\n  ini.add_section(wanted)\nend","typeGuard":null,"tryCatchPattern":"begin\n  ini.add_section(name)\nrescue Puppet::Util::IniConfig::IniParseError\n  ini.sections.find { |s| s.name == name } # already exists: reuse it\nend","preventionTips":["Treat add_section as create-only; fetch-or-create via ini.sections lookup instead","Deduplicate section headers when merging INI files (check for repeats before writing)","Run idempotent config scripts that check current state before mutating"],"tags":["ini","duplicate-section","inifile","puppet"],"backgroundTag":"duplicate-config-section","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}