Skip to content
Latchkey

Go "code in directory expects import path" - Fix Import Comments in CI

A package can carry an import comment (package foo // import "github.com/org/foo") that pins its canonical import path. When the build imports it under a different path, Go enforces the comment and fails with an expects import path error.

What this error means

A build fails with code in directory <dir> expects import path "<canonical>" while the code imported it as something else. It typically appears after a repository move or a vanity-import setup where the comment and the actual import path diverged.

go output
package github.com/old/foo: code in directory
/root/go/pkg/mod/github.com/old/foo@v1.2.0
expects import path "github.com/new/foo"

Common causes

The package pins a canonical path that moved

An // import "github.com/new/foo" comment fixes the canonical path. After a repo rename, code still importing the old path conflicts with the comment.

A vanity import path mismatch

A custom/vanity import host in the comment differs from the module path your go.mod actually requires, so the two disagree.

How to fix it

Import the package by its canonical path

Update imports (and the require) to the path the comment declares.

Terminal
# the error names the expected path; switch to it
go get github.com/new/foo
grep -rl 'github.com/old/foo' --include='*.go' . | xargs sed -i 's#github.com/old/foo#github.com/new/foo#g'
go mod tidy

Or remove/align the import comment in your own package

If it is your package and the canonical path changed, update the import comment to match the new module path.

Go
// package store // import "github.com/org/app/internal/store"
package store // import "github.com/org/app/v2/internal/store"

How to prevent it

  • Keep import comments aligned with the module path in go.mod.
  • Update all imports after a repository or module rename.
  • Use vanity import comments deliberately and keep their host in sync.

Frequently asked questions

What causes ""expects import path""?
An // import "github.com/new/foo" comment fixes the canonical path. After a repo rename, code still importing the old path conflicts with the comment.
How do I fix "expects import path"?
Update imports (and the require) to the path the comment declares.

Related guides

References

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