leg100 / pug

Drive terraform at terminal velocity.
Mozilla Public License 2.0
439 stars 7 forks source link

Error parsing resources created using `for_each` over a `map` (Edit: `set` too.) #96

Closed kaorihinata closed 2 months ago

kaorihinata commented 2 months ago

Describe the bug

I've been converting some of my projects over to use pug, and it seems to be having issues parsing state where for_each is used to iterate over a map (Edit: set too). At the moment it seems to assume that index_key will always be an int which may be the case with sets, but not with maps. (Edit: Just checked. Not the case for sets either so I have no idea why it's declared as int in internal/state/file.go.)

I can still run things, but pug doesn't understand the resulting state, instead printing the following error in the logs:

reloading state error=parsing state: json: cannot unmarshal string into Go struct field StateFileResourceInstance.Resources.Instances.index_key of type int workspace.name=default module.path=aws_iam_service_accounts workspace.name=default

Setup (please complete the following information):

To Reproduce

Steps to reproduce the behavior:

  1. Run the following module (with or without pug):
    
    terraform {
    backend "local" {}
    }

resource "time_sleep" "wait_three_seconds" { create_duration = each.value for_each = { duration = "3s" } }


2. Open the module in pug. State should be blank, and there should be an error in the log about failing to parse `index_key`.

**Expected behavior**
I expected the state to be parsed and visible inside of pug's state viewer.
kaorihinata commented 2 months ago

I patched file.go and state.go to expect IndexKey to be a string and my state will now happily load, but pressing enter on a state item currently crashes pug. Not sure if that's supposed to do something? I tested that on both the patched build, and the official build.

leg100 commented 2 months ago

You've identified two bugs.

I've raised the one regarding pressing enter on a state here:

https://github.com/leg100/pug/issues/97

Re for_each, I'll implement a fix shortly. It looks like - as you say with your patch - it can also be a string.

leg100 commented 2 months ago

@kaorihinata Thanks for the thorough write-up of the issue. Please try out https://github.com/leg100/pug/releases/tag/v0.4.1.

kaorihinata commented 2 months ago

Perfect! Bumped the package in my local tools and it's working just fine. Thank you so much.