« Back to Index

Module Error: malformed module path … missing dot in first path element

View original Gist on GitHub

Tags: #go

Golang Module Error.md

When using go modules I’ve stumbled across this error a few times, but it’s infrequent enough for me to spend hours each time trying to recall how to fix it.

malformed module path ... missing dot in first path element

This occurs when I’m using the replace directive in a go.mod file to tell the go compiler that anytime it sees a custom import like foo/bar to actually lookup that code from somewhere locally available to the application.

e.g.

module github.com/buzzfeed/mono/rate_control

go 1.14

require (
	buzzfeed/instrumentation v0.0.0
	buzzfeed/settings v0.0.0
	github.com/go-redis/redis v6.15.7+incompatible
	github.com/go-redis/redis/v7 v7.2.0
	github.com/sirupsen/logrus v1.4.1
)

replace (
	buzzfeed/instrumentation => ./shared/lib/instrumentation
	buzzfeed/settings => ./shared/lib/settings
)

So the problem with the above is that I import buzzfeed/instrumentation and that itself is a go module that imports buzzfeed/logging and buzzfeed/metrics.

In buzzfeed/instrumentation’s go.mod file it also uses replace to change the lookup of those dependencies.

The source of the malformed module path error is actually misleading.

The actual problem is documented in the golang wiki: https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive (specifically the first paragraph under the linked section):

As described in the ‘go.mod’ concepts section above, replace directives provide additional control in the top-level go.mod for what is actually used to satisfy a dependency found in the Go source or go.mod files, while replace directives in modules other than the main module are ignored when building the main module.

This means that my top-level go.mod file, although it doesn’t directly use buzzfeed/logging or buzzfeed/metrics, still needs the ‘replace’ directives to be added.

For example, the following fixes it…

module github.com/buzzfeed/mono/rate_control

go 1.14

require (
	buzzfeed/instrumentation v0.0.0
	buzzfeed/logging v0.0.0
	buzzfeed/metrics v0.0.0
	buzzfeed/settings v0.0.0
	github.com/go-redis/redis v6.15.7+incompatible
	github.com/go-redis/redis/v7 v7.2.0
	github.com/sirupsen/logrus v1.4.1
)

replace (
	buzzfeed/instrumentation => ./shared/lib/instrumentation
+	buzzfeed/logging => ./shared/lib/logging
+	buzzfeed/metrics => ./shared/lib/metrics
	buzzfeed/settings => ./shared/lib/settings
)