diff --git a/command/output.go b/command/output.go index 05fd31f34..2e3c1bee0 100644 --- a/command/output.go +++ b/command/output.go @@ -39,7 +39,7 @@ func (c *OutputCommand) Run(args []string) int { } state := stateStore.State() - if len(state.RootModule().Outputs) == 0 { + if state.Empty() || len(state.RootModule().Outputs) == 0 { c.Ui.Error(fmt.Sprintf( "The state file has no outputs defined. Define an output\n" + "in your configuration with the `output` directive and re-run\n" + diff --git a/command/output_test.go b/command/output_test.go index fbcda9962..d3444c389 100644 --- a/command/output_test.go +++ b/command/output_test.go @@ -142,6 +142,27 @@ func TestOutput_noArgs(t *testing.T) { } } +func TestOutput_noState(t *testing.T) { + originalState := &terraform.State{} + statePath := testStateFile(t, originalState) + + ui := new(cli.MockUi) + c := &OutputCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + "foo", + } + if code := c.Run(args); code != 1 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } +} + func TestOutput_noVars(t *testing.T) { originalState := &terraform.State{ Modules: []*terraform.ModuleState{