Is there any official documentation on whether the...
# dotnet
c
Is there any official documentation on whether the key insertion order of an InputMap is deterministic? I don't remember having any issues with this before, but I'm suddenly running into scenarios where InputMap keys are being shuffled around when read. snippet:
Copy code
var map = new InputMap<string> {
            { "a", "1" },
            { "b", "2" },
            { "c", "3" },
            { "d", "4" },
    };
    
    map.Apply( m => m.Keys ).Apply( k => { Log.Warn( string.Join( ',', k )) ; return k; });
Running this multiple times will result in something like the following:
Copy code
First run:
d,c,a,b
Second run:
b,d,a,c
I recently upgraded the CLI to 3.208.0. Not sure if that would make a difference
I'm passing some information along as an input map and then later converting it into a list that gets passed to a resource. The non-deterministic key ordering is causing spurious diffs when running
pulumi up
. I'm fine if it's non-deterministic and I need to sort it myself, but this just cropped up when I can't recall having issues with it before. I'm not sure if I've been lucky and dodged this issue until now or if it's some new behavior.
Also worth noting is that the keys do preserve insertion order when passed as a stack output
Copy code
// it has the proper order here
    return new Dictionary<string, object?> {
        { "testmap", map }
    };
which makes me more confident it's a bug of some kind
s
Mmm, InputMap<V> is a Input<ImmutableDictionary<string, V>> according to https://www.pulumi.com/docs/reference/pkg/dotnet/Pulumi/Pulumi.InputMap-1.html Which according to MS : Represents an immutable, unordered collection of keys and values.( https://learn.microsoft.com/en-us/ldotnet/api/system.collections.immutable.immutabledictionary-2?view=net-10.0) So order is not guaranteed
c
Why would the keys passed to the stack outputs be different than the keys passed to an apply?
does stack output sort the keys?
yep, looks like it does
ok, probably not a bug then
weird though, this has never been a problem and I've done all sorts of funky things with inputmaps
I mean, it's fine. I can pipe it into an immutablesorteddictionary and be done with it
apparently Dictionary keeps insertion order but immutabledictionary doesn't weirdly enough it seems like the manner in which immutabledictionary and inputmap shuffle their keys is random across executions but deterministic within an execution
Copy code
var map = new Dictionary<string, string> {
            { "d", "4"},
            { "c", "3"},
            { "a", "1" },
            { "b", "2" },
    };
    
    var immutableMap = map.ToImmutableDictionary();
    
    var inputMap = new InputMap<string> {
        { "d", "4"},
        { "c", "3"},
        { "a", "1" },
        { "b", "2" },
    };
    
    Output.JsonSerialize( Output.Create( map ), new JsonSerializerOptions { WriteIndented = true } ).Apply( a => { Log.Warn( "plain: " + a ); return a; } );
    Output.JsonSerialize( Output.Create( immutableMap ), new JsonSerializerOptions { WriteIndented = true } ).Apply( a => { Log.Warn( "immutable: " + a ); return a; } );
    Output.JsonSerialize( inputMap.ToOutput(), new JsonSerializerOptions { WriteIndented = true } ).Apply( a => { Log.Warn( "inputmap: " + a ); return a; } );
this results in the following output:
Copy code
warning: plain: {
      "d": "4",
      "c": "3",
      "a": "1",
      "b": "2"
    }
    warning: immutable: {
      "a": "1",
      "c": "3",
      "d": "4",
      "b": "2"
    }
    warning: inputmap: {
      "a": "1",
      "c": "3",
      "d": "4",
      "b": "2"
    }
plain keeps order. immutable and inputmap don't
but immutable and inputmap don't in exactly the same way
i dont think that's relevant to anything but it's a fun fact 🙂
anyway imma chalk this up to expected behavior i've never run into before. Thanks for the sanity check @silly-country-86708