'failed to load modules' with recursive module import

I am working on a project that includes three modules, which we can call ‘site’, ‘theme’ and ‘utility’.

‘site’ is the top level, and defines a website. It loads ‘theme’, which contains the layouts, CSS etc. ‘theme’ needs to use a partial that is defined in ‘utility’. All three were duly initialized with go mod init, and I have been careful not to use the old-style way of referencing themes (i.e. putting a theme declaration in my config.toml file).

‘utility’ provides a partial called easy-utility (names have been changed to protect the innocent), which is invoked in layouts/partials/header.html in ‘theme’ with:

{{ partial "easy-utility" . }}

The config/_default/config.toml file in ‘theme’ contains:

[module]
    [[module.imports]]
    path = 'github.com/angusmci/utility'

The latest version of ‘utility’ is available at that location on Github.

If I try to run hugo mod get (or any other hugo command), however, I get an error:

ERROR failed to load modules: module "github.com/angusmci/utility" not found in "/Users/angus/site/themes/github.com/angusmci/utility"; either add it as a Hugo Module or store it in "/Users/angus/site/themes".: module does not exist

There is a fix for this, which is to add:

 [[module.imports]]
    path = 'github.com/angusmci/utility'

to the config.toml file in ‘site’, rather than ‘theme’. If I do that, then everything works.

This seems counterintuitive, however. I would like ‘site’ to be able to use ‘theme’ without needing to know (and list) all the supporting modules that ‘theme’ pulls in.

I assume that there must be something wrong with my approach. Is there a way to let modules import and use sub-modules without needing to explicitly import all the sub-modules in the highest-level module?

This should just work and is tested extensively. I don’t see any obvious wrong with your setup, but the devil is in the detail.

Edit in: One guess (and I’m taking this from memory), but to import Hugo Modules with github paths, the importer needs to be a Hugo Module, as in it needs a go.mod file (created with hugo mod init). The imported repositori can be a plain repo without such file, but that’s worth checking.

All three modules involved have go.mod files.

The basic form of each is:

module github.com/angusmci/utility

go 1.27.3

(obviously, ‘site’ has github.com/angusmci/site as the module name, and so forth).

I think my description of the configuration is faithful. Obviously, I changed the names for simplicity and didn’t include the full content of each configuration file, but I think that everything related to imports is there.

Are your modules semantically versioned (tagged and released), or are you relying on git hashes?