Does `pulumi stack export` preserve order? I wante...
# general
g
Does
pulumi stack export
preserve order? I wanted to compare 2 outputs and I get way more differences than I expected - many of them seem to be caused by a different order of the resources.
p
I haven’t checked that - if you’re right, it might be a good idea to sort the keys before printing the output from
pulumi stack export
.
As a workaround, you can do it yourself quite simply by using
jq
tool.
Copy code
pulumi stack export | jq -S .
if you have exported states in JSON files, just
cat
them to
jq
🙂
Copy code
cat some-older-state.json | jq -S . > formatted-some-older-state.json
g
Does
jq -S
deep sorting?
p
Oh… good question 😅. I’ve just assumed it does.
also not sure right now about arrays
the order most probably won’t be changed and I guess in some cases, you’d like to treat the array as set so the order doesn’t matter
g
yeah if an array is a set then order does not matter but it does for Diff 😞
p
anyway, I’ve checked and it looks like
jq
DOES deep sorting
but I’m afraid the issues mentioned above would be a bigger problem
you’d have to match the elements first using their
urn
and then compare their content
g
I'll give it a try and see how it goes. I'm trying to solve a problem where I need to move several resources to another region. However my ComponentResource also contains global resources. It's a bit of can of worms, I may end up just removing stuff from the state and deleting it manually
p
I see
Anyway, I think your case is not that weird. Many people might wanna compare the state files. It would be a nice feature to have
pulumi stack diff <state-file-a> <state-file-b>
.
it could visualise the diff in a similar way
pulumi preview
works
g
that would be too easy, yes
p
so… maybe I’ve got sth to help you
it looks like resources are located at
.deployments.resources
and each one of them has
urn
that should be unique
so if I simply sort the resources in this array by
urn
, they should be easily comparable
Copy code
❯ cat /tmp/a.json | jq '.'
{
  "deployment": {
    "resources": [
      {
        "urn": "b",
        "data": 2
      },
      {
        "urn": "a",
        "data": 1
      }
    ]
  }
}
and with the sorting query:
Copy code
❯ cat /tmp/a.json | jq '.deployment.resources|=sort_by(.urn)'
{
  "deployment": {
    "resources": [
      {
        "urn": "a",
        "data": 1
      },
      {
        "urn": "b",
        "data": 2
      }
    ]
  }
}
try to compare results from running:
Copy code
pulumi stack export | jq '.deployment.resources|=sort_by(.urn)'
and see if that helps
g
this is what I thought about doing! Thank you for saving me some time!