gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.82k stars 7.54k forks source link

Weird issues with merge modules config #11403

Closed canstand closed 1 year ago

canstand commented 1 year ago

What version of Hugo are you using (hugo version)?

v0.117.0

Does this issue reproduce with the latest release?

yes

Reproduce with the following steps:

  1. git clone https://github.com/bep/hugo-starter-tailwind-basic
  2. use following script create new site and serve
    
    #!/usr/bin/env bash

rm -rf ./hugotest hugo new site hugotest cd hugotest hugo mod init hugotest

cat >> hugo.toml<< EOF [build] _merge = 'deep' [module] _merge = 'shallow' replacements = 'github.com/bep/hugo-starter-tailwind-basic/v3 -> ../../hugo-starter-tailwind-basic' [[module.imports]] path = 'github.com/bep/hugo-starter-tailwind-basic/v3' EOF

hugo mod npm pack npm install hugo server -logLevel debug

4. open http://localhost:1313, it shows `Page Not Found`. **This is the first problem and can be solved with the next step.**
5. edit `hugo-starter-tailwind-basic/hugo.toml`, add missing default mounts. 
```toml
  [[module.mounts]]
    source = "content"
    target = "content"
  [[module.mounts]]
    source = "static"
    target = "static"
  [[module.mounts]]
    source = "layouts"
    target = "layouts"
  [[module.mounts]]
    source = "data"
    target = "data"
  [[module.mounts]]
    source = "i18n"
    target = "i18n"
  [[module.mounts]]
    source = "archetypes"
    target = "archetypes"
  1. Now the page content is present, but the css styles are missing.
  2. Had to re-run hugo server, so that the page style can be displayed correctly.
  3. Run the script again to recreate a new site will still have this problem. Always appears on the first run of hugo server for a new site (with merge module), don't know why
bep commented 1 year ago

This works (I have removed the replacement part, which I don't think is important here):

baseURL      = 'https://example.org/'
languageCode = 'en-us'
title        = 'My New Hugo Site'
[build]
  _merge = 'deep'
[module]
  _merge       = 'shallow'
  [[module.imports]]
    path = 'github.com/bep/hugo-starter-tailwind-basic/v3'
    [[module.imports.mounts]]
      source = "assets"
      target = "assets"
    [[module.imports.mounts]]
      source = "content"
      target = "content"
    [[module.imports.mounts]]
      source = "static"
      target = "static"
    [[module.imports.mounts]]
      source = "layouts"
      target = "layouts"
    [[module.imports.mounts]]
      source = "data"
      target = "data"
    [[module.imports.mounts]]
      source = "i18n"
      target = "i18n"
    [[module.imports.mounts]]
      source = "archetypes"
      target = "archetypes"

Note the placement of the mounts below the import, also the important assets mount where the CSS lives.

But note:

  1. github.com/bep/hugo-starter-tailwind-basic is not meant to be used as a theme, but as a starter/example project. It works as a theme, but you need the above elaborate setup.
  2. The replacements config was added before Go's workspace support; now I would recommend that you set up your development with work files, see https://gohugo.io/hugo-modules/use-modules/#module-workspaces
canstand commented 1 year ago

This only solves the first issue. Let's use my theme for testing, which is more intuitive.. (Note, run in WSL2, ubuntu 22.04.3)

  1. Run the script, and the page will display as follows

    #!/usr/bin/env bash
    # remove dir for new test
    rm -rf ./hugotest
    hugo new site hugotest
    cd hugotest
    hugo mod init hugotest
    
    cat >> hugo.toml<< EOF
    [build]
      _merge = 'deep'
    [module]
      _merge = 'shallow'
      [[module.imports]]
        path = 'github.com/canstand/compost'
    EOF
    
    hugo mod npm pack
    npm install
    hugo server  -logLevel debug
  2. No need to make any changes, just press Ctrl+C to exit, then cd hugotest; hugo server, and refresh the page, it's back to normal.

  3. But, If you replace _merge = 'shallow' in the script with the specific config, there will be no such issue.

      [[module.mounts]]
        source = "assets"
        target = "assets"
      [[module.mounts]]
        source = "hugo_stats.json"
        target = "assets/watching/hugo_stats.json"

screenshot

first run

error

after

correct

bep commented 1 year ago

Yea, OK, now I get it. What you see is the

[[module.mounts]]
  source = 'hugo_stats.json'
  target = 'assets/watching/hugo_stats.json'

Not considered to be a "watch candidate" when coming from a module. So, the configuration merge works, but I suspect it's a ordering "thing" where we evaluate the watch directories based on the project config only (not sure what, someone needs to look at the code).

bep commented 1 year ago

OK, now I think I understand.

Running this locally, the mount assets/watching/hugo_stats.json mounts to /Users/bep/Library/Caches/hugo_cache/modules/filecache/modules/pkg/mod/github.com/canstand/compost@v0.6.1/hugo_stats.json

To cut a long story short:

Anyhow, you need to move the hugo_stats mount to the project config.

canstand commented 1 year ago

Thanks, now I understand, I need to guide the theme users to explicitly configure mounts, not merge.

Thanks.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.