opentofu/opentofu · 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

Raised by modulePath (internal/command/command.go:64) when a command that takes no positional arguments receives at least one. OpenTofu centralized module-path validation here: extra args almost always mean the user tried to pass a working directory the pre-0.14 way, so the message points at the -chdir global flag instead.

Source

Thrown at internal/command/command.go:64

The "backend" in OpenTofu defines how OpenTofu operates. The default
backend performs all operations locally on your machine. Your configuration
is configured to use a non-local backend. This backend doesn't support this
operation.
`

// modulePath returns the path to the root module and validates CLI arguments.
//
// This centralizes the logic for any commands that previously accepted
// a module path via CLI arguments. This will error if any extraneous arguments
// are given and suggest using the -chdir flag instead.
//
// If your command accepts more than one arg, then change the slice bounds
// to pass validation.
func modulePath(args []string) (string, error) {
	// TODO: test

	if len(args) > 0 {
		return "", fmt.Errorf("Too many command line arguments. Did you mean to use -chdir?")
	}

	path, err := os.Getwd()
	if err != nil {
		return "", fmt.Errorf("Error getting pwd: %w", err)
	}

	return path, nil
}

View on GitHub (pinned to 3561785c48)

Solutions

  1. Move the directory to the global flag: tofu -chdir=./envs/prod plan (flag must come before the subcommand)
  2. Or cd into the target directory and run the command with no positional args
  3. Remove stray arguments if they were accidental (copy/paste, wrapper bugs)

Example fix

# before
tofu plan ./envs/prod
# Error: Too many command line arguments. Did you mean to use -chdir?

# after
tofu -chdir=./envs/prod plan
Defensive patterns

Strategy: validation

Validate before calling

// wrapper-side guard mirroring modulePath's contract:
func validateArgs(cmd string, args []string) error {
    zeroArg := map[string]bool{"plan": true, "apply": true, "init": true, "validate": true, "refresh": true}
    if zeroArg[cmd] && len(args) > 0 {
        return fmt.Errorf("%s takes no positional args; use tofu -chdir=%s %s", cmd, args[0], cmd)
    }
    return nil
}

Try / catch

path, err := modulePath(args)
if err != nil {
    if strings.Contains(err.Error(), "Too many command line arguments") {
        // rewrite to: tofu -chdir=<arg> <cmd> and retry, or surface usage help
    }
    return err
}

Prevention

When it happens

Trigger: Invoking a zero-arg command like 'tofu plan ./envs/prod', 'tofu apply prod', or 'tofu init infra' - any positional argument reaches modulePath and trips len(args) > 0.

Common situations: Scripts written for Terraform <= 0.13 that passed a module path; users assuming apply takes a directory; wrappers or aliases that append arguments unexpectedly.

Related errors


AI-assisted analysis of opentofu/opentofu@3561785c48 (2026-08-15). Data as JSON: /api/errors/9c9be21d5a1fdd9d. Report an issue: GitHub.