Skip to content

Commit 958d12d

Browse files
committed
Fix - make sure all errors are handled, and avoid nil error causing a panic
1 parent fa26199 commit 958d12d

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

internal/command/meta_backend.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,45 +1651,50 @@ func (m *Meta) stateStore_C_s(c *configs.StateStore, stateStoreHash int, provide
16511651
// Verify that selected workspace exists in the state store.
16521652
if opts.Init && b != nil {
16531653
err := m.selectWorkspace(b)
1654-
if strings.Contains(err.Error(), "No existing workspaces") {
1655-
// If there are no workspaces, Terraform either needs to create the default workspace here,
1656-
// or instruct the user to run a `terraform workspace new` command.
1657-
ws, err := m.Workspace()
1658-
if err != nil {
1659-
diags = diags.Append(fmt.Errorf("Failed to check current workspace: %w", err))
1660-
return nil, diags
1661-
}
1654+
if err != nil {
1655+
if strings.Contains(err.Error(), "No existing workspaces") {
1656+
// If there are no workspaces, Terraform either needs to create the default workspace here,
1657+
// or instruct the user to run a `terraform workspace new` command.
1658+
ws, err := m.Workspace()
1659+
if err != nil {
1660+
diags = diags.Append(fmt.Errorf("Failed to check current workspace: %w", err))
1661+
return nil, diags
1662+
}
16621663

1663-
switch {
1664-
case ws != backend.DefaultStateName:
1665-
// User needs to run a `terraform workspace new` command.
1666-
diags = append(diags, tfdiags.Sourceless(
1667-
tfdiags.Error,
1668-
fmt.Sprintf("Workspace %q has not been created yet", ws),
1669-
fmt.Sprintf("State store %q in provider %s (%q) reports that no workspaces currently exist. To create the custom workspace %q use the command `terraform workspace new %s`.",
1670-
c.Type,
1671-
c.Provider.Name,
1672-
c.ProviderAddr,
1673-
ws,
1674-
ws,
1675-
),
1676-
))
1677-
return nil, diags
1664+
switch {
1665+
case ws != backend.DefaultStateName:
1666+
// User needs to run a `terraform workspace new` command.
1667+
diags = append(diags, tfdiags.Sourceless(
1668+
tfdiags.Error,
1669+
fmt.Sprintf("Workspace %q has not been created yet", ws),
1670+
fmt.Sprintf("State store %q in provider %s (%q) reports that no workspaces currently exist. To create the custom workspace %q use the command `terraform workspace new %s`.",
1671+
c.Type,
1672+
c.Provider.Name,
1673+
c.ProviderAddr,
1674+
ws,
1675+
ws,
1676+
),
1677+
))
1678+
return nil, diags
16781679

1679-
case ws == backend.DefaultStateName:
1680-
// Users control if the default workspace is created through the -create-default-workspace flag (defaults to true)
1681-
if opts.CreateDefaultWorkspace {
1682-
diags = diags.Append(m.createDefaultWorkspace(c, b))
1683-
} else {
1684-
diags = diags.Append(&hcl.Diagnostic{
1685-
Severity: hcl.DiagWarning,
1686-
Summary: "The default workspace does not exist",
1687-
Detail: "Terraform has been configured to skip creation of the default workspace in the state store. To create it, either run an 'init' command without `-create-default-workspace=true`, or create it using a 'workspace new' command",
1688-
})
1680+
case ws == backend.DefaultStateName:
1681+
// Users control if the default workspace is created through the -create-default-workspace flag (defaults to true)
1682+
if opts.CreateDefaultWorkspace {
1683+
diags = diags.Append(m.createDefaultWorkspace(c, b))
1684+
} else {
1685+
diags = diags.Append(&hcl.Diagnostic{
1686+
Severity: hcl.DiagWarning,
1687+
Summary: "The default workspace does not exist",
1688+
Detail: "Terraform has been configured to skip creation of the default workspace in the state store. To create it, either run an 'init' command without `-create-default-workspace=true`, or create it using a 'workspace new' command",
1689+
})
1690+
}
1691+
default:
1692+
diags = diags.Append(err)
1693+
return nil, diags
16891694
}
1690-
default:
1691-
diags = diags.Append(err)
1692-
return nil, diags
1695+
} else {
1696+
// For all other errors, report via diagnostics
1697+
diags = diags.Append(fmt.Errorf("Failed to select a workspace: %w", err))
16931698
}
16941699
}
16951700
}

0 commit comments

Comments
 (0)