Go mod replace package and all subpackages

My project has the following basic structure:

  | - src
    | - go.mod ("project")
    | - api
       | - go.mod ("project/api")
       | - main.go
    | - cloud
      | - go.mod ("project/cloud")
      | - db
        | - go.mod ("project/cloud/db")
      | - fs
        | - go.mod ("project/cloud/fs")
      | - etc. (many more)
         | - go.mod ("project/cloud/...")

The api package imports all of the cloud package’s subpackages (db, fs, etc.) from the local filesystem, not a git url. go mod provides the replace directive to point from an import name like project/cloud/db to the right location.

I tried the following statement in project/src/api/go.mod

replace project => ../

But the following import statement in project/src/api/main.go

import "project/cloud/db"

Produces the error in console when running go mod tidy or any other go mod command:

project/api imports
	project/cloud/db: module project@latest found (v0.0.0-00010101000000-000000000000, replaced by ../), but does not contain package project/cloud/db

Manually specifying every single package to replace does work:

replace project/cloud/db => ../cloud/db

replace project/cloud/dns => ../cloud/dns

replace project/cloud/mail => ../cloud/mail

replace project/cloud/scaling => ../cloud/scaling

replace project/cloud/fs => ../cloud/fs

How can I avoid all of these mostly-redundant cases?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.