golang/go · error

ErrUnexpectedSuccess

ErrUnexpectedSuccess

Error message

unexpected success

What it means

ErrUnexpectedSuccess is the sentinel raised by checkStatus when a script command prefixed with '!' (expect failure) instead completed successfully. It is the primary assertion-failure signal for negative tests in the script framework: the author asserted the command must fail, and it did not. The error is wrapped with the command's source location via cmdError.

Source

Thrown at src/cmd/internal/script/errors.go:14

// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package script

import (
	"errors"
	"fmt"
)

// ErrUnexpectedSuccess indicates that a script command that was expected to
// fail (as indicated by a "!" prefix) instead completed successfully.
var ErrUnexpectedSuccess = errors.New("unexpected success")

// A CommandError describes an error resulting from attempting to execute a
// specific command.
type CommandError struct {
	File string
	Line int
	Op   string
	Args []string
	Err  error
}

func cmdError(cmd *command, err error) *CommandError {
	return &CommandError{
		File: cmd.file,
		Line: cmd.line,
		Op:   cmd.name,
		Args: cmd.args,
		Err:  err,

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Re-evaluate whether the command should still fail; if it should now succeed, remove the '!' prefix.
  2. If the command should fail, tighten its input so it actually fails (e.g. pass an invalid flag or missing file).
  3. Use errors.Is(err, script.ErrUnexpectedSuccess) in test harness code to distinguish this from other failures.

Example fix

// before (script.txt)
! go build ./brokenpkg
# the package now builds -> unexpected success

// after: either drop the negation
go build ./brokenpkg
// or keep the package actually broken
Defensive patterns

Strategy: try-catch

Validate before calling

// Negative assertions are static; review them when the underlying behavior changes.

Type guard

func isUnexpectedSuccess(err error) bool {
    return errors.Is(err, script.ErrUnexpectedSuccess)
}

Try / catch

if err := eng.Run(st, file); err != nil {
    if errors.Is(err, script.ErrUnexpectedSuccess) {
        // a '!'-prefixed command unexpectedly succeeded; revisit the assertion
    }
}

Prevention

When it happens

Trigger: A script line `! cmd args` where cmd exits 0 / returns nil. checkStatus sees err == nil with cmd.want == failure and returns cmdError(cmd, ErrUnexpectedSuccess).

Common situations: The negative assertion was wrong (the command now succeeds), the environment changed so a previously-failing case now passes, or the '!' was added by mistake. Very common when updating tests: a fix makes a command succeed but the script still expects failure.

Related errors


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