PoshCode / Configuration

A module to help other modules have settings
MIT License
176 stars 27 forks source link

Export-Configuration Overwriting Existing Nested Hashtable Values for User #38

Closed jkubecki closed 2 years ago

jkubecki commented 4 years ago

Issue description

When using a nested Hashtable with more than 1 value, using Export-Configuration to save the values individually overwrites the previous values for the nested Hashtable.

Sorry if this format is not good for reporting this bug - tried to be as detailed as possible and provide test/example scenario files.

Steps to reproduce the issue

What's the expected result?

The end of the output to show BOTH values having been overridden in User Configuration file, and Final Config to reflect BOTH overrides:

*** User Configuration File w Foo.Bar AND Foo.Baz overridden

@{
  Foo = @{
    Bar = 42
    Baz = 8675309
  }
}

*** Final Config
{
  "Foo": {
    "Bar": 42,
    "Baz": 8675309
  }
}

What's the actual result?

The end of the output shows only the MOST RECENT value having been overridden in User Configuration file, and Final Config only reflects the MOST RECENT override:

*** User Configuration File w Foo.Bar AND Foo.Baz overridden

@{
  Foo = @{
    Baz = 8675309
  }
}

*** Final Config
{
  "Foo": {
    "Bar": 1,
    "Baz": 8675309
  }
}
Jaykul commented 3 years ago

I'm not sure that I follow what you expected to happen, but perhaps I need to document this better.

The Export command follows PowerShell's usual semantics for export: it takes an input object and exports that entire object, overwriting any previous content in the destination. It is not an Update and that is why there is an Update-Metadata and Update-Object.

Essentially, Import-Configuration and Export-Configuration always work with the full Configuration object, and the Export command is not designed to allow you to export a subset, nor only the values the user has changed.

If you want to update a single value, you should either:

  1. Import-Metadata and add or set the value, then Export-Metadata
  2. Use Update-Metadata (note: this currently can't add values)

WindowsTerminal_2021-01-18_23-38-19