Randomly ignoring baseURL and using local path after hugo server when building site

When I make changes to a site I use hugo server to test the changes and then build the site using hugo. Randomly the site will be built using the local paths for links and images rather than the path specified with baseURL. Building the site again after discovering this will usually, but not always fix it. This applies to hugo 153 and I tried reverting to 151. The sites use themes theme-hugo-foundation6, which I’ve fixed from time to time and kraiklyn. All the sites were originally created some time ago and it’s possible I’ve missed something that should be in the site config file.

URLs of the two sites affected recently:

Update: I’ve just tried hugo 154 and it worked correctly. Whether it will next time is indeterminate.

When building your site locally for a production deployment, make sure you delete the contents of the public directory before running the hugo command.

When building the site using a CI/CD workflow (e.g., GitHub Pages, GitLab Pages, Netlify, Cloudflare, etc.) you don’t have to delete the public directory before pushing your changes, unless you have placed the public directory under source control, which you should never do.

1 Like

Thank you for this. However there’s nothing in the public directory of the blog site, the other site doesn’t have one. Most of the sites I have created don’t have one. I assume the hugo new site command stopped creating them at some stage.

This is something unique to your process. Such “random” behavior would have been found and fixed immediately.

This is true. However I have been building sites using this method off and on since 2017 and this has only started happening recently. Because it’s random it’s difficult to pin down a hugo version where it started. This is why I said…

All the sites were originally created some time ago and it’s possible I’ve missed something that should be in the site config file.

All the sites have a baseURL and a publishDir entry in the config file. The publishDir entry points to a folder which is published via Github pages, either in the billthefarmer.github.io project or in a project docs folder.

Hi mister bill. I’m not certain I’m following, but have you tried using the –cleanDestinationDir option when you build?

It just did it again using the latest Hugo 158. Here’s a bit of diff to prove it…

Bill@Cecelia MINGW64 /d/billthefarmer.github.io (master)
$ git diff
diff --git a/blog/404.html b/blog/404.html
index 3a916fc4..a3322ae6 100644
--- a/blog/404.html
+++ b/blog/404.html
@@ -1,12 +1,12 @@
 <!DOCTYPE html>
 <html lang="en">
-  <head><script src="/blog/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=blog/livere
load" data-no-instant defer></script>
+  <head>

     <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <!-- The above 3 meta tags *must* come first in the head; any other head content must come *aft
er* these tags -->

-<meta property="og:url" content="http://localhost:1313/blog/404.html">
+<meta property="og:url" content="https://billthefarmer.github.io/blog/404.html">
   <meta property="og:site_name" content="Bill Farmer">
   <meta property="og:title" content="404 Page not found">
   <meta property="og:description" content="Random thoughts on random subjects">
@@ -29,12 +29,12 @@
 <meta name="generator" content="Hugo 0.158.0">

You are a Windows user.

When you run either hugo or hugo server, are running the command within one of the MS shells, or within a shell provided by MINGW64 as shown above? Or do you run the command from either/both?

It’s a MINGW64 shell provided as part of the Windows version of Git. I do all my development using it. The thing with this problem is that it happens randomly. I create some new content for one of my web pages using hugo server interactively. Stop the Hugo server with ^C, and then just type hugo to publish the content. When it happens if I do it again, the output is fine. Hugo seems to still think it’s a server, but it publishes the content to the right place with the local baseUrl. Notice the livereload.js script.

I asked about OS and shell because of this in your config:

publishDir: d:/billthefarmer.github.io/blog/

Which doesn’t match the directory path in your shell prompt:

/d/billthefarmer.github.io

That’s a feature of the MINGW64 shell. You type in Linux style paths and they get converted to windows style paths before being passed to executables. The path in the config file is as recognised by Hugo, it’s the same folder. I think this is a red herring. d/billthefarmer.github.io is the root folder of the GitHub project that I use to publish my web pages, there are several others, the blog being one of them.

Perhaps. But is there any harm in testing to be sure? If the publishDir is within the project directory, just use a relative path (e.g., publishDir = 'foo').

In GitHub pages the publish-your-site repository is a different repository than the Hugo sources repository . Otherwise the Hugo sources would get published as if they were web pages. In my case the publish repository is in /d/billthefarmer.github.io and the sources repository is in /d/Hugo/github.io. The source for the example above is in the /d/Hugo/github.io/blog folder. The output is in the /d/billthefarmer.github.io/blog folder. All this is not particularly relevant to the stated problem that Hugo randomly creates output as if it were a server.

that depends on the workflow and your account/repo settings.

I tend to disagaree here.

There’s a “random” error. The only known case is your setup, so there’s something in your setup that makes the difference

One reason could be the different levels of filesystem access with your process

  • Windows → MinGW → bash → MinGW FS Wrapper → Windows exe(Hugo) → Go FS wrapper … and FS caches, …
  • all the implementations and versions have changed since 2017.

If I were you I would switch to a windows only setup, and see if that helps to get rid of the problem.

In GitHub pages the publish-your-site repository is a different repository than the Hugo sources repository . Otherwise the Hugo sources would get published as if they were web pages.

Many years ago, some sites used a two repository approach: one for source, one for the build artifacts. That evolved into a two branch approach: one for source, and one for the build artifacts.

For at least the last 5 years, the recommended[1] approach is a single repository where your local public directory is excluded from source control. Then use a GitHub Action as described here.

As previously described, this issue is specific to your environment. I suspect the random nature of the problem is due to mixed build artifacts in the publication directory: some artifacts are from a previous hugo server command, while other artifacts are from the current hugo command.

Historically, in its default configuration, hugo server rendered to memory, while hugo rendered to disk. With v0.123.0[2] and later, in its default configuration, both commands render to disk. Some things to investigate…

  • Are you clearing the publication directory every time you run the hugo command?
  • Are you clearing it with rm -rf path-to-publish-dir?
  • After clearing it, do you immediately run the hugo command, or do you wait a moment?
  • Is hugo server running in another console when you run hugo?
  • If you have a virus scanner running, have you tried disabling it, or excluding all related directories?

  1. Recommended by the GitHub Actions team and the Hugo maintainers. ↩︎

  2. Released in February 2024 ↩︎

1 Like

As I’ve been using this working environment for just about everything I do, building web sites with Hugo is just an occasional documentation task, I shall stick with it. However out of interest, I have written a short bit if C to test the environment in a MINGW64 shell and in the windows shell (if you can call it that). The answer appears to depend on how you ask.

// Environment.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <windows.h>
int main(int argc, char **argv)
{
    char cwd[1024];
    for (int i = 0; i < argc; i++)
        printf("argc[%d] = %s\n", i, argv[i]);
    printf("Path (stdlib) = %s\n", getenv("PATH"));
    GetEnvironmentVariable("path", cwd, sizeof(cwd));
    printf("Path (windows) = %s\n", cwd);
    getcwd(cwd, sizeof(cwd));
    printf("CWD = %s (unistd)\n", cwd);
    GetCurrentDirectory(sizeof(cwd), cwd);
    printf("CWD = %s (windows)\n", cwd);
    FILE *file = fopen("config.yaml", "r");
    if (file == NULL)
    {
        printf("No config.yml!\n");
        exit(1);
    }
    printf("\nconfig.yml:\n");
    while (!feof(file))
    {
        fgets(cwd, sizeof(cwd), file);
        if (strncmp("baseURL", cwd, strlen("baseURL")) == 0)
        {
            printf(cwd);
            break;
        }
    }
    fclose(file);
    exit(0);
}

I renamed it hugo.exe and copied it to where my copy of Hugo lives.

D:\Hugo\github.io\blog>hugo server
argc[0] = hugo
argc[1] = server
Path (stdlib) = /c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/c/Program Files/Calibre2:/c/Program Files/gs/gs10.06.0/lib:/c/Program Files/dotnet:/c/Program Files (x86)/Windows Kits/10/Windows Performance Toolkit:/c/Program Files/gs/gs10.06.0/bin:/c/Program Files/Git/cmd:/c/Users/Bill/AppData/Local/Microsoft/WindowsApps:/c/Program Files/Java/jdk-21.0.2/bin:/usr/bin:/mingw64/bin:/d/adt/flutter/bin:/c/Users/Bill/AppData/Local/Microsoft/WinGet/Links
Path (windows) = C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Calibre2\;C:\Program Files\gs\gs10.06.0\lib;C:\Program Files\dotnet\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\gs\gs10.06.0\bin;C:\Program Files\Git\cmd;C:\Users\Bill\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jdk-21.0.2\bin;D:\msys64\usr\bin;D:\msys64\mingw64\bin;D:\adt\flutter\bin;C:\Users\Bill\AppData\Local\Microsoft\WinGet\Links;
CWD = /d/Hugo/github.io/blog (unistd)
CWD = D:\Hugo\github.io\blog (windows)

config.yml:
baseURL: https://billthefarmer.github.io/blog/

D:\Hugo\github.io\blog>
Bill@Cecelia MINGW64 /d/Hugo/github.io/blog (master)
$ hugo server
argc[0] = /usr/bin/hugo
argc[1] = server
Path (stdlib) = /c/Users/Bill/bin:/c/Program Files/Git/mingw64/bin:/c/Program Files/Git/usr/local/bin:/c/Program Files/Git/usr/bin:/c/Program Files/Git/usr/bin:/c/Program Files/Git/mingw64/bin:/c/Program Files/Git/usr/bin:/c/Users/Bill/bin:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/c/Program Files/Calibre2:/c/Program Files/gs/gs10.06.0/lib:/c/Program Files/dotnet:/c/Program Files (x86)/Windows Kits/10/Windows Performance Toolkit:/c/Program Files/gs/gs10.06.0/bin:/c/Program Files/Git/cmd:/c/Users/Bill/AppData/Local/Microsoft/WindowsApps:/c/Program Files/Java/jdk-21.0.2/bin:/usr/bin:/mingw64/bin:/d/adt/flutter/bin:/c/Users/Bill/AppData/Local/Microsoft/WinGet/Links:/c/Program Files/Git/usr/bin/vendor_perl:/c/Program Files/Git/usr/bin/core_perl:/d/adt/android-sdk-windows/platform-tools
Path (windows) = C:\Users\Bill\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\Bill\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Calibre2;C:\Program Files\gs\gs10.06.0\lib;C:\Program Files\dotnet;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\Program Files\gs\gs10.06.0\bin;C:\Program Files\Git\cmd;C:\Users\Bill\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jdk-21.0.2\bin;D:\msys64\usr\bin;D:\msys64\mingw64\bin;D:\adt\flutter\bin;C:\Users\Bill\AppData\Local\Microsoft\WinGet\Links;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl;D:\adt\android-sdk-windows\platform-tools
CWD = /d/Hugo/github.io/blog (unistd)
CWD = D:\Hugo\github.io\blog (windows)

config.yml:
baseURL: https://billthefarmer.github.io/blog/

Bill@Cecelia MINGW64 /d/Hugo/github.io/blog (master)
$

i would not call that c-program a real test case for the random appearance… it’s just a single shot execution without hugo and using bare C…

if switching is no option observing @jmooring 's questions about usage could shed some light.