https://pulumi.com logo
#general
Title
# general
g

great-sunset-355

10/19/2021, 8:29 AM
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

prehistoric-activity-61023

10/19/2021, 8:43 AM
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

great-sunset-355

10/19/2021, 8:57 AM
Does
jq -S
deep sorting?
p

prehistoric-activity-61023

10/19/2021, 8:58 AM
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

great-sunset-355

10/19/2021, 9:01 AM
yeah if an array is a set then order does not matter but it does for Diff 😞
p

prehistoric-activity-61023

10/19/2021, 9:02 AM
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

great-sunset-355

10/19/2021, 9:05 AM
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

prehistoric-activity-61023

10/19/2021, 9:06 AM
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

great-sunset-355

10/19/2021, 9:08 AM
that would be too easy, yes
p

prehistoric-activity-61023

10/19/2021, 9:21 AM
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

great-sunset-355

10/19/2021, 9:29 AM
this is what I thought about doing! Thank you for saving me some time!
6 Views