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
- Append the missing argument after the flag (e.g. `-I ./include`).
- Remove the dangling flag if the path is not needed.
- Re-run `go build` after the fix to confirm the error clears.
- 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
- Never end a #cgo CFLAGS line with a bare -I/-L/-D.
- Lint generated cgo flag lists.
- Re-read edited #cgo directives before commit.
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
- invalid flag in %s: %s %s (see https://go.dev/s/invalidflag)
- invalid flag in %s: %s (see https://go.dev/s/invalidflag)
- SWIG file must not use prefix 'cgo'
- invalid pkg-config package name: %s
- flag %q triggers external linking
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/5900a7cb3a7a4100.
Report an issue: GitHub.