golang/go · error

invalid flag in %s: %s without argument (see https://go.dev/

Error message

invalid flag in %s: %s without argument (see https://go.dev/s/invalidflag)

What it means

Thrown by checkFlags when a flag that expects an argument (e.g. `-I`) appears as the last token in the list with no value following it. The error reports the flag and notes it is missing its argument.

Source

Thrown at src/cmd/go/internal/work/security.go:462

					load.SafeArg(list[i+1][4:]) &&
					!strings.Contains(list[i+1][4:], ",") {
					i++
					continue Args
				}

				// Permit -I= /path, -I $SYSROOT.
				if i+1 < len(list) && arg == "-I" {
					if (strings.HasPrefix(list[i+1], "=") || strings.HasPrefix(list[i+1], "$SYSROOT")) &&
						load.SafeArg(list[i+1][1:]) {
						i++
						continue Args
					}
				}

				if i+1 < len(list) {
					return fmt.Errorf("invalid flag in %s: %s %s (see https://go.dev/s/invalidflag)", source, arg, list[i+1])
				}
				return fmt.Errorf("invalid flag in %s: %s without argument (see https://go.dev/s/invalidflag)", source, arg)
			}
		}
	Bad:
		return fmt.Errorf("invalid flag in %s: %s (see https://go.dev/s/invalidflag)", source, arg)
	}
	return nil
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Append the missing argument after the flag (e.g. `-I ./include`).
  2. Remove the dangling flag if the path is not needed.
  3. Re-run `go build` after the fix to confirm the error clears.
  4. Lint #cgo directives in vendored C packages before upgrading.

Example fix

// before
// #cgo CFLAGS: -O2 -I

// after
// #cgo CFLAGS: -O2 -I ./include
Defensive patterns

Strategy: validation

Validate before calling

// Ensure argument-taking flags are not dangling
flags := strings.Fields(os.Getenv("CGO_CFLAGS"))
argTaking := map[string]bool{"-I": true, "-L": true, "-D": true, "-U": true, "-include": true}
if argTaking[flags[len(flags)-1]] {
    log.Fatalf("%s has no argument", flags[len(flags)-1])
}

Prevention

When it happens

Trigger: A #cgo CFLAGS line or CGO_CFLAGS list ends with a bare `-I` (or similar argument-taking flag): i+1 is out of range, so checkFlags returns the `without argument` form.

Common situations: Truncation while editing a #cgo line; a generated/templated flag list that dropped the trailing path; mis-ordered flags where the intended value landed on the next line.

Related errors


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