golang/go · error

empty condition

Error message

empty condition

What it means

Thrown by the script parser when a condition bracket token contains nothing after stripping brackets and an optional '!' negation. Conditions take the form [name] or [!name]; an empty interior ([], [!], [ ]) has no condition to evaluate and is rejected at parse time as a syntax error.

Source

Thrown at src/cmd/internal/script/engine.go:385

			switch want := expectedStatus(arg); want {
			case failure, successOrFailure:
				if cmd.want != "" {
					return errors.New("duplicated '!' or '?' token")
				}
				cmd.want = want
				return nil
			}

			// Command prefix [cond] means only run this command if cond is satisfied.
			if strings.HasPrefix(arg, "[") && strings.HasSuffix(arg, "]") {
				want := true
				arg = strings.TrimSpace(arg[1 : len(arg)-1])
				if strings.HasPrefix(arg, "!") {
					want = false
					arg = strings.TrimSpace(arg[1:])
				}
				if arg == "" {
					return errors.New("empty condition")
				}
				cmd.conds = append(cmd.conds, condition{want: want, tag: arg})
				return nil
			}

			if arg == "" {
				return errors.New("empty command")
			}
			cmd.name = arg
			return nil
		}

		cmd.rawArgs = append(cmd.rawArgs, rawArg)
		return nil
	}

	for i := 0; ; i++ {
		if !quoted && (i >= len(line) || strings.ContainsRune(argSepChars, rune(line[i]))) {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Provide a real registered condition name inside the brackets, e.g. `[go1.21]` or `[exec:go]`.
  2. If negating, keep the name: `[!symlink]` not `[!]`.
  3. Remove the empty bracket token entirely if no condition is intended.

Example fix

// before
[ ] go test ./...
# -> empty condition

// after
[go1.22] go test ./...
Defensive patterns

Strategy: validation

Validate before calling

// Reject empty condition brackets in a script line.
func checkCondition(token string) error {
    if strings.HasPrefix(token, "[") && strings.HasSuffix(token, "]") {
        inner := strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(token, "["), "]"))
        inner = strings.TrimPrefix(inner, "!")
        if strings.TrimSpace(inner) == "" { return errors.New("empty condition") }
    }
    return nil
}

Type guard

func isEmptyCondition(err error) bool {
    return err != nil && err.Error() == "empty condition"
}

Try / catch

// Static-lint script lines for [], [ ], [!], [ ! ].

Prevention

When it happens

Trigger: A script line containing `[]`, `[ ]`, `[!]`, or `[ ! ]` as a command-prefix token. After trimming, arg == "" trips the guard in flushArg.

Common situations: Script-test author writes a condition placeholder and forgets to fill in the condition name, or accidentally empties a condition during editing, or leaves a stray `[!]` negation with no condition.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/5debd6a0a1da55b6. Report an issue: GitHub.