Skip to content
Latchkey

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.

go
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

  1. Pick the version you intend to import and update all imports to its path.
  2. Drop the other require so only one module provides the package.
Terminal
go mod tidy
go mod why github.com/foo/bar

Use a replace to unify

  1. Replace the unwanted module so both paths resolve to a single source.
go.mod
// go.mod
replace github.com/foo/bar => github.com/foo/bar/v2 v2.0.1

How to prevent it

  • Import a single major version of each module.
  • Run go mod why to trace duplicate providers.
  • Keep replace directives complete and consistent.

Frequently asked questions

What causes ""ambiguous import""?
Both github.com/foo/bar and github.com/foo/bar/v2 are in the graph and both expose the same package subtree.
How do I fix "ambiguous import"?
Consolidate on one module

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card