Skip to content

fix: scope duplicate key tracking per section with bracketedArray=false#299

Open
abhu85 wants to merge 1 commit intonpm:mainfrom
abhu85:fix/scope-duplicates-per-section
Open

fix: scope duplicate key tracking per section with bracketedArray=false#299
abhu85 wants to merge 1 commit intonpm:mainfrom
abhu85:fix/scope-duplicates-per-section

Conversation

@abhu85
Copy link
Copy Markdown

@abhu85 abhu85 commented Feb 23, 2026

Summary

Fixes #298

When using bracketedArray: false, the duplicate key counter was global across all sections, causing keys with the same name in different sections to be incorrectly converted to arrays.

The Problem

[section_1]
var = 1

[section_2]
var = 2

Was incorrectly parsed as:

{ section_1: { var: '1' }, section_2: { var: ['2'] } }  // ❌ var is wrongly an array

Because the global duplicates counter saw var twice across both sections.

The Fix

Reset the duplicates counter when entering a new section (line 128), so keys are only treated as duplicates when they appear multiple times within the same section.

Now correctly parses as:

{ section_1: { var: '1' }, section_2: { var: '2' } }  // ✅ both are scalars

Changes

  • lib/ini.js: Change duplicates from const to let, reset to {} when entering new section
  • test/duplicate-properties.js: Add regression tests for cross-section and within-section duplicates

Test Results

All tests pass with 100% coverage.


Fixes npm#298

When using `bracketedArray: false`, the duplicate key counter was global across
all sections, causing keys with the same name in different sections to be
incorrectly converted to arrays.

For example:
```ini
[section_1]
var = 1

[section_2]
var = 2
```

Was incorrectly parsed as:
```js
{ section_1: { var: '1' }, section_2: { var: ['2'] } }
```

The fix resets the duplicates counter when entering a new section, so keys are
only treated as duplicates when they appear multiple times within the same section.

Now correctly parses as:
```js
{ section_1: { var: '1' }, section_2: { var: '2' } }
```
@rquadling
Copy link
Copy Markdown
Contributor

Purely as an "is it allowed"...

[section_1]
var = 1

[section_2]
var = 2

[section_1]
var = 3

I can't remember the rules on a section being reused in the file, If so, section_1.var either is an array or overwritten, depending upon POV.

@abhu85
Copy link
Copy Markdown
Author

abhu85 commented Apr 22, 2026

@rquadling - Great question! I tested this exact scenario. With a repeated section:

[section_1]
var = 1

[section_2]
var = 2

[section_1]
var = 3

With this fix (bracketedArray: false): section_1.var = "3" (last value wins), section_2.var = "2" — consistent with how the default bracketedArray: true mode handles repeated sections.

On main today (bracketedArray: false): section_1.var = ["1", "3"] and section_2.var = ["2"] — the section_2 single-element array is the bug this PR fixes (#298), and the section_1 array is a side effect of the same global counter bug rather than intentional behavior.

So this fix makes bracketedArray: false consistent with the default mode for repeated sections: last value wins. If someone needs array merging across repeated section headers, that would be a separate feature since neither mode supports it today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants