mumoshu / variant2

Turn your bash scripts into a modern, single-executable CLI app today
MIT License
141 stars 11 forks source link

Bug: referencing invalid `config` keys causes silent exit #7

Closed osterman closed 4 years ago

osterman commented 4 years ago

If I'm referencing my configuration loaded via config { ...} and the key does not exist, then variant just exits with no error message.

mumoshu commented 4 years ago

@osterman Thanks. Would you mind sharing me an example for reproduction? It will help me fix the issue so much faster!

osterman commented 4 years ago

Will do... here's one related, but different. This one causes a segfault.

#!/usr/bin/env variant

config "settings" {
  source file {
    path = "${context.sourcedir}/conf/test.yaml"
    default = ""
  }
}

job "example" {
  description = "Write varfile to terraform moduledir"
  variable "test" {
    value = "123"
  }
  exec {
      command = "echo"
      args = [
        "[${conf.settings.projects[var.test]}]"
      ]
   }
}

with a file in conf/test.yml:

projects:
   something: here

Produces:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x1fa21e4]

goroutine 1 [running]:
github.com/zclconf/go-cty/cty.Type.FriendlyNameForConstraint(...)
    /home/runner/go/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/type.go:55
github.com/mumoshu/variant2/pkg/app.getVarialbles(0xc00047f100, 0xc000203800, 0x1, 0x1, 0xc0002cdc68, 0x1, 0x1, 0x1, 0x0, 0x0)
    /home/runner/work/variant2/variant2/pkg/app/app.go:1744 +0xa64
github.com/mumoshu/variant2/pkg/app.(*App).createJobContext(0xc00045b8b0, 0xc0003a0300, 0xc0004491d0, 0x7, 0x0, 0x283a1c0, 0xc000200240, 0xc00053eee0, 0x0, 0x0, ...)
    /home/runner/work/variant2/variant2/pkg/app/app.go:1527 +0xb68
github.com/mumoshu/variant2/pkg/app.(*App).Job.func1(0x0, 0x0, 0x0)
    /home/runner/work/variant2/variant2/pkg/app/app.go:518 +0x132
github.com/mumoshu/variant2/pkg/app.(*App).Run(0xc00045b8b0, 0xc0004491d0, 0x7, 0xc000203710, 0xc000203740, 0xc00022d9e0, 0x1, 0x1, 0x0, 0x0, ...)
    /home/runner/work/variant2/variant2/pkg/app/app.go:492 +0xc3
github.com/mumoshu/variant2.(*Runner).Cobra.func1(0xc000547b80, 0x3412450, 0x0, 0x0, 0x0, 0x0)
    /home/runner/work/variant2/variant2/variant.go:624 +0x114
github.com/spf13/cobra.(*Command).execute(0xc000547b80, 0x3412450, 0x0, 0x0, 0xc000547b80, 0x3412450)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:826 +0x460
github.com/spf13/cobra.(*Command).ExecuteC(0xc000547900, 0x28114c0, 0xc00000e020, 0x28114c0)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:914 +0x2fb
github.com/spf13/cobra.(*Command).Execute(...)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:864
github.com/mumoshu/variant2.(*Runner).Run(0xc00045b900, 0xc00041d530, 0x1, 0x1, 0xc00022dd78, 0x1, 0x1, 0x0, 0x0)
    /home/runner/work/variant2/variant2/variant.go:754 +0x2cc
github.com/mumoshu/variant2.Main.Run(0x7ffeefbffc02, 0xa, 0x0, 0x0, 0x0, 0x7ffeefbffc00, 0xc, 0x28114c0, 0xc00000e018, 0x28114c0, ...)
    /home/runner/work/variant2/variant2/variant.go:359 +0x12d
main.main()
    /home/runner/work/variant2/variant2/pkg/cmd/main.go:13 +0xb8
osterman commented 4 years ago

I'm not able to reproduce the original error. Not sure why.

I am getting a good error:

Unsupported attribute; This object does not have an attribute named "test"., and 1 other diagnostic(s)

mumoshu commented 4 years ago

@osterman The panic seems to have happened due to missing type in the variable block. That's fixed via 2072059.

I've also added some error printing logics in a few possible places to hopefully fix the original issue fc5484f

Please reopen or submit another issue(s) if you managed to reproduce it.