HUGO

Replacements for Hugo modules in env variable

I am using Hugo modules for my Hugo theme. It would be convenient if I could set it to use my local copy of the theme instead of the stable published version in an env variable, rather than in the go.mod file that is committed to my git repo. I have replacements working in my go.mod like so:

module www.maximumethics.dev

go 1.15

require git.sr.ht/~skyfaller/maxethics-hugo v0.0.5 // indirect
// To develop locally, uncomment the line below and edit it to point to your local copy
replace git.sr.ht/~skyfaller/maxethics-hugo => /home/nelson/code/maxethics-hugo

However, when I try to set the replacement as an env variable, it doesn’t seem to work. Here’s what I’m doing in fish shell:

~/c/content (master)>
set -Ux HUGO_MODULE_REPLACEMENTS "git.sr.ht/~skyfaller/maxethics-hugo -> /home/nelson/code/maxethics-hugo"
~/c/content (master)> echo $HUGO_MODULE_REPLACEMENTS
git.sr.ht/~skyfaller/maxethics-hugo -> /home/nelson/code/maxethics-hugo

If I comment out the replacement in go.mod after setting that env variable, the site refuses to build because it’s using the stable version of my theme, not the dev version that my local copy is relying on. (I added a new shortcode.) Re-adding the replacement in go.mod makes it work again.

I don’t know where to begin to figure out what I’m doing wrong. Any ideas?

In case it matters, I’m running:
Hugo Static Site Generator v0.78.1/extended linux/amd64 BuildDate: unknown
go version go1.15.3 linux/amd64

If you’re curious about my system:

      ///////*767////////////////         OS: Pop!_OS 20.10 x86_64 
    //////7676767676*//////////////       Host: Thelio thelio-r1 
   /////76767//7676767//////////////      Kernel: 5.9.6-050906-generic 
  /////767676///*76767///////////////     Uptime: 1 day, 15 hours, 55 mins 
 ///////767676///76767.///7676*///////    Packages: 2277 (dpkg), 52 (brew), 57 (flatpak) 
/////////767676//76767///767676////////   Shell: fish 3.1.2 
//////////76767676767////76767/////////   Resolution: 3840x2160 
///////////76767676//////7676//////////   DE: GNOME 
////////////,7676,///////767///////////   WM: Mutter 
/////////////*7676///////76////////////   WM Theme: Pop 
///////////////7676////////////////////   Theme: Pop-dark [GTK2/3] 
 ///////////////7676///767////////////    Icons: Pop [GTK2/3] 
  //////////////////////'////////////     Terminal: gnome-terminal 
   //////.7676767676767676767,//////      CPU: AMD Ryzen 7 3800X (16) @ 3.900GHz 
    /////767676767676767676767/////       GPU: AMD ATI Radeon RX 5600 OEM/5600 XT / 5700/5700 XT 
      ///////////////////////////         Memory: 5259MiB / 32122MiB

Not really an answer to your question, but I’ve approached a similar problem from a different direction.

I use multiple config files:

...
├── config
│   ├── _default
│   │   └── config.toml
│   └── development
│       └── config.toml
├── content
│   └── ...
└── themes
    ├── bar
    └── foo
# config/default/config.toml

theme = [
"github.com/pointyfar/foo",
"github.com/pointyfar/bar"
]
# config/development/config.toml

theme = [
"foo",
"bar"
]

I had been planning to do some testing with go.mod replacement feature, and I’ll update once I have done so.

I have a “release” script that removes the lines, sends the go.mod to the repo and adds them again :wink: But I thought about the way @pointyfar was suggesting. You would have to take care to upload your local changes to the modules of course. This could be done with a call to git porcellain.

My release script (just typed together, not really clean with fallback):

#!/bin/bash

cp bin/go.mod.repo go.mod

hugo mod get -u ./...
hugo mod tidy
rm -rf public
#hugo mod vendor

cp go.mod bin/go.mod.repo

#git add _vendor
git add go.mod
git add go.sum

{
  echo 'replace github.com/davidsneighbour/dnb-hugo-garuda => /home/patrick/Projects/Hugo/dnb-hugo-garuda'
  echo 'replace github.com/davidsneighbour/dnb-hugo/netlification => /home/patrick/Projects/Hugo/dnb-hugo/netlification'
  echo 'replace github.com/davidsneighbour/dnb-hugo/renderhooks => /home/patrick/Projects/Hugo/dnb-hugo/renderhooks'
  echo 'replace github.com/davidsneighbour/dnb-hugo/robots => /home/patrick/Projects/Hugo/dnb-hugo/robots'
  echo 'replace github.com/davidsneighbour/dnb-hugo/favicons => /home/patrick/Projects/Hugo/dnb-hugo/favicons'
  echo 'replace github.com/davidsneighbour/dnb-hugo/shortcodes => /home/patrick/Projects/Hugo/dnb-hugo/shortcodes'
  echo 'replace github.com/davidsneighbour/dnb-hugo-libs/bootstrap4 => /home/patrick/Projects/Hugo/dnb-hugo-libs/bootstrap4'
} >> go.mod

hugo --gc --minify
git add -f public
npm run release