Go "ambiguous import: found package in multiple modules" - Fix in CI
An import path resolves to a package present in two different required modules. Go cannot decide which one you meant, so it refuses to build.
What this error means
A build fails with ambiguous import: found package X in multiple modules. It usually happens after a module split its path, or when a fork and the original are both required.
app/main.go:5:2: ambiguous import: found package github.com/foo/bar/v2/baz in multiple modules:
github.com/foo/bar v1.5.0 (/root/go/pkg/mod/...)
github.com/foo/bar/v2 v2.0.1 (/root/go/pkg/mod/...)Common causes
Original and major-version module both required
Both github.com/foo/bar and github.com/foo/bar/v2 are in the graph and both expose the same package subtree.
A fork and the upstream both required
A replace was added incompletely, leaving two modules that claim the same package path.
How to fix it
Consolidate on one module
- Pick the version you intend to import and update all imports to its path.
- Drop the other require so only one module provides the package.
go mod tidy
go mod why github.com/foo/barUse a replace to unify
- Replace the unwanted module so both paths resolve to a single source.
// go.mod
replace github.com/foo/bar => github.com/foo/bar/v2 v2.0.1How to prevent it
- Import a single major version of each module.
- Run
go mod whyto trace duplicate providers. - Keep replace directives complete and consistent.