mumoshu / variant2

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

Bug: Segfault on missing `run` block within `step` block #5

Closed osterman closed 4 years ago

osterman commented 4 years ago

what

variant2 crashes when using need in the following manner.

use-case

I want to recreate the following from a Makefile in variant using need

# Init projects can be a dynamically computed list of projects, e.g. from yaml
INIT_PROJECTS ?= terraform/init/eks

terraform/init/eks:
  terraform init ....

# Initialize all projects
terraform/init/all: $(INIT_PROJECTS)

crash log

eriks-mbp:variant-demo erik$ ./mycli terraform init all
? env test
Error: nothing to run
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x1f995f8]

goroutine 35 [running]:
github.com/mumoshu/variant2/pkg/app.(*App).execRunInternal(0xc0000a7ef0, 0xc00023c300, 0xc00023bfc0, 0x0, 0x0, 0x0, 0x0)
    /home/runner/work/variant2/variant2/pkg/app/app.go:932 +0x48
github.com/mumoshu/variant2/pkg/app.(*App).execRun(0xc0000a7ef0, 0xc00023c300, 0xc00023bfc0, 0x0, 0x2822300, 0xc0003768c8, 0x0, 0x0, 0x0)
    /home/runner/work/variant2/variant2/pkg/app/app.go:956 +0x81
github.com/mumoshu/variant2/pkg/app.(*App).execJobSteps.func1(0xc000012738, 0x0, 0x0)
    /home/runner/work/variant2/variant2/pkg/app/app.go:1056 +0x87
github.com/mumoshu/variant2/pkg/app.(*App).execJobSteps.func4()
    /home/runner/work/variant2/variant2/pkg/app/app.go:1137 +0x8f
github.com/mumoshu/variant2/pkg/app.(*App).execJobSteps.func3(0xc00044a2a0)
    /home/runner/work/variant2/variant2/pkg/app/app.go:1122 +0x3b
created by github.com/mumoshu/variant2/pkg/app.(*App).execJobSteps
    /home/runner/work/variant2/variant2/pkg/app/app.go:1120 +0xb3f

code

#!/usr/bin/env variant
# vim: filetype=hcl

...

job "terraform init" {
  description = "Run terraform init on a project"
  parameter "project" {
    type = string
    description = "Project to provision"
  }

  variable "moduledir" {
    type = string
    value = try("${opt.cachedir}/${param.project}")
  }

  concurrency = 1

  step "mkdir" {
    run "shell" {
      env = opt.env
      project = param.project
      commands = list(
    "rm -rf '${var.moduledir}'",
    "mkdir -p '${var.moduledir}'",
    "ls -ld ${var.moduledir}"
      )
    }
  }

  step "init" {
    run "terraform subcommand" {
      subcommand = "init"
      args = list("-backend=false", "--from-module", conf.terraform.module)
      env = opt.env
      project = param.project
      moduledir = var.moduledir
    }
  }
}

job "terraform init all" {
  description = "Init all projects"
  step "init" {
    need = ["terraform init eks"]
  }   
}
...
osterman commented 4 years ago

Tested this on 0.19.1 and earlier

osterman commented 4 years ago

This segfault is caused by user error. I thought I could use need with jobs (like a list of dependencies), but it only works with steps. Anyways, looks like a validation error?

mumoshu commented 4 years ago

@osterman Hey! Thanks - This specific issue was caused by the insufficient validation. I fixed that.

Let's implement need for jobs based on our discussion on the cloudposse slack later, in a separate issue.