golang/go · error

%s and %s differ

Error message

%s and %s differ

What it means

Thrown by the cmp command in the Go test script framework when two files' contents differ after optional environment-variable expansion. The error includes the file names; if not quiet, a diff is also logged beforehand via s.Logf. This is an assertion-style command — it 'fails' the script when files don't match.

Source

Thrown at src/cmd/internal/script/cmds.go:268

	}

	data, err := os.ReadFile(s.Path(name2))
	if err != nil {
		return err
	}
	text2 = string(data)

	if env {
		text1 = s.ExpandEnv(text1, false)
		text2 = s.ExpandEnv(text2, false)
	}

	if text1 != text2 {
		if !quiet {
			diffText := diff.Diff(name1, []byte(text1), name2, []byte(text2))
			s.Logf("%s\n", diffText)
		}
		return fmt.Errorf("%s and %s differ", name1, name2)
	}
	return nil
}

// Cp copies one or more files to a new location.
func Cp() Cmd {
	return Command(
		CmdUsage{
			Summary: "copy files to a target file or directory",
			Args:    "src... dst",
			Detail: []string{
				"src can include 'stdout' or 'stderr' to copy from the script's stdout or stderr buffer.",
			},
		},
		func(s *State, args ...string) (WaitFunc, error) {
			if len(args) < 2 {
				return nil, ErrUsage
			}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run the test with -v and examine the logged diff to see exact differences.
  2. If the new output is correct, update the expected/golden file to match.
  3. If env expansion is involved, ensure environment variables are set correctly in the script setup.
  4. Use the -env flag or ensure s.Setenv calls match expected expansion.

Example fix

// Update golden file if output is intentionally different:
// go test -run TestName -update  # or manually cp actual expected

// Or fix the code path producing wrong output.
Defensive patterns

Strategy: try-catch

Try / catch

// cmp is an assertion command; handle by examining the diff:
// In test scripts, this is expected behavior. Use:
// cmp expected.txt actual.txt
// If it fails, the script framework logs the diff automatically.
// To tolerate differences, use a conditional or regenerate golden files.

Prevention

When it happens

Trigger: text1 (content of file 1, optionally env-expanded) != text2 (content of file 2, optionally env-expanded). The cmp command reads both files, optionally expands $VAR references, then compares the resulting strings.

Common situations: Test script expects a specific output file but the program produced different content. Common in golden-file tests where expected output drifts after code changes, or when environment-variable placeholders don't match actual runtime values.

Related errors


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