Skip to content
Latchkey

Go "package command-line-arguments is not a main package" - Fix in CI

When you pass file paths to go build or go run, Go groups them into a synthetic command-line-arguments package. If those files are not package main, it cannot produce an executable.

What this error means

A build or run fails with package command-line-arguments is not a main package. It usually means CI passed individual .go files (or a non-main file set) to go build/run instead of a package path.

go
go run util.go
package command-line-arguments is not a main package

Diagnose it: toolchain, tags, or platform?

Go build failures that only appear in CI usually come from a different toolchain version, different build tags, or cross-compilation defaults that differ from your machine.

Terminal
go version
go env GOOS GOARCH CGO_ENABLED GOFLAGS GOTOOLCHAIN

# build exactly what CI builds, verbosely
go build -v ./... 2>&1 | tail -40

# CGO is the usual difference: on by default locally, often off in a slim CI image
CGO_ENABLED=0 go build ./...

Common causes

Built loose files instead of a package

Passing specific .go files makes Go treat them as command-line-arguments, which must be main to build a binary.

The named files are not package main

The files belong to a library package, so there is no func main to build.

How to fix it

Build the package path, not files

  1. Point go build/run at the package directory containing func main.
Terminal
go run ./cmd/app
go build ./cmd/app

Pass the full main file set

  1. If you must name files, include every file of the main package, not a subset.
Terminal
go run main.go util.go

How to prevent it

  • Build package paths (./cmd/app) rather than individual files.
  • Keep func main in the package you intend to run.
  • Avoid passing partial file lists to go build/run.

Frequently asked questions

What causes Go "package command-line-arguments is not a main package"?
There are 2 common causes: built loose files instead of a package and the named files are not package main. Passing specific .go files makes Go treat them as command-line-arguments, which must be main to build a binary.
How do I fix Go "package command-line-arguments is not a main package"?
There are 2 fixes depending on which cause you have: build the package path, not files and pass the full main file set. Work through them in order, since the first is the most common.
What does Go "package command-line-arguments is not a main package" actually mean?
A build or run fails with package command-line-arguments is not a main package.
How do I stop Go "package command-line-arguments is not a main package" happening again?
Build package paths (./cmd/app) rather than individual files. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card