hashicorp/terraform · error

Too many command line arguments. Did you mean to use -chdir?

Error message

Too many command line arguments. Did you mean to use -chdir?

What it means

Returned by ParseWorkspaceList (workspace_list.go:39) when 'terraform workspace list' is given any positional arguments. Historically 'workspace list' accepted a DIR argument; that was replaced by the global -chdir flag, so the error suggests -chdir. The command now takes no positionals.

Source

Thrown at internal/command/arguments/workspace_list.go:39

	var diags tfdiags.Diagnostics

	var jsonOutput bool
	cmdFlags := defaultFlagSet("workspace list")
	cmdFlags.BoolVar(&jsonOutput, "json", false, "produce JSON output")

	if err := cmdFlags.Parse(args); err != nil {
		diags = diags.Append(tfdiags.Sourceless(
			tfdiags.Error,
			"Failed to parse command-line flags",
			err.Error(),
		))
	}

	// `workspace list` takes no positional arguments. Historically there was a DIR argument that was replaced with the -chdir flag.
	// Here we replicate the old behaviour of suggesting the user to use -chdir if they provide any positional arguments.
	args = cmdFlags.Args()
	if len(args) != 0 {
		diags = diags.Append(errors.New("Too many command line arguments. Did you mean to use -chdir?"))
	}

	switch {
	case jsonOutput:
		return &WorkspaceList{Workspace: Workspace{ViewType: ViewJSON}}, diags
	default:
		return &WorkspaceList{Workspace: Workspace{ViewType: ViewHuman}}, diags
	}
}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Use the global -chdir flag to switch directories: 'terraform -chdir=./my-dir workspace list'.
  2. cd into the target directory first, then run 'terraform workspace list' with no arguments.
  3. Remove any positional path argument from the command line.

Example fix

# before
$ terraform workspace list ./my-dir
# after
$ terraform -chdir=./my-dir workspace list
Defensive patterns

Strategy: validation

Validate before calling

// 'workspace list' takes no positionals; redirect legacy DIR usage to -chdir.
func normalizeListArgs(args []string) ([]string, []string) {
    var positionals []string
    for _, a := range args {
        if strings.HasPrefix(a, "-") { continue }
        positionals = append(positionals, a)
    }
    if len(positionals) > 0 {
        return []string{"-chdir=" + positionals[0], "workspace", "list"}, nil
    }
    return append([]string{"workspace", "list"}, args...), nil
}

Prevention

When it happens

Trigger: Line 37-39: cmdFlags.Args() is non-empty after flag parsing -> the error is appended. Triggered by e.g. 'terraform workspace list ./my-dir'.

Common situations: User following an old tutorial that passes a path to 'workspace list'; muscle memory from 'terraform init DIR'; trying to list workspaces for a different directory by position rather than -chdir.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/619a7918c7a498df. Report an issue: GitHub.