Skip to content

[BUG] bracketedArray: false produces arrays for duplicate keys across sections #298

@hudikm

Description

@hudikm

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When parsing INI content with multiple sections that reuse the same key name:

const content = `
[section_1]
var = 1

[section_2]
var = 2
`

calling:

 ini.parse(content, { bracketedArray: false });

returns:

{
  Section_1: { var: 1 },
  Section_2: { var: [2] }
}

As you can see, Section_2.var is incorrectly returned as an array, even though there is only a single value defined in that section.

I need bracketedArray for legacy compatibility—specifically, to support cases where multiple identical keys within the same section should be parsed as an array.

I believe the issue comes from using a global duplicate counter that is not scoped per section. In ini.js:

duplicates[keyRaw] = (duplicates?.[keyRaw] || 0) + 1;
isArray = duplicates[keyRaw] > 1;

Because duplicates is tracked globally, keys with the same name across different sections are treated as duplicates, which causes later sections to be converted into arrays even when they contain only a single entry.

Expected Behavior

Keys should only be converted to arrays when the same key appears multiple times within the same section. Identical keys in different sections must be treated independently and remain single values unless duplicated locally.

Steps To Reproduce

Steps to Reproduce

  1. Prepare the following INI content:
[section_1]
var = 1

[section_2]
var = 2
  1. Parse it using:
ini.parse(content, { bracketedArray: false });
  1. Observe the result:
{
  section_1: { var: 1 },
  section_2: { var: [2] }
}
  1. Notice that section_2.var is returned as an array even though the key appears only once in that section.

Environment

  • npm: 11.6.0
  • Node: v22.19.0
  • OS: windows 11
  • platform: HP

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions