Skip to content

parser: Drop empty sub maps from hugo config output#14861

Open
ChrisJr404 wants to merge 1 commit into
gohugoio:masterfrom
ChrisJr404:fix-config-empty-sub-maps
Open

parser: Drop empty sub maps from hugo config output#14861
ChrisJr404 wants to merge 1 commit into
gohugoio:masterfrom
ChrisJr404:fix-config-empty-sub-maps

Conversation

@ChrisJr404
Copy link
Copy Markdown

Took a stab at this. Happy to drop the PR if you've already got something underway.

ReplacingJSONMarshaller.MarshalJSON walks the unmarshalled map and deletes zero-valued leaves when OmitEmpty is true, but it skips the truthiness check for any value that is itself a map. So once recursion empties a child map, the now-empty parent map sticks around in the output. With the issue's input, that surfaces as:

[[permalinks]]
  pattern = '/:year/:month/:slug/'
  [permalinks.target]
    path = '/{photo,photo/**}'

    [permalinks.target.sites]
      [permalinks.target.sites.complements]

      [permalinks.target.sites.matrix]

sitesmatrix.Sites.IsZero is defined and would return true here, but at this point in the pipeline everything is already a map[string]any, so the struct-level IsZero is gone.

The change after recursing into a child map, drop the key if the child is now empty. Slices of maps are deliberately left alone since removing slice entries would shift indices and the existing behavior never did that.

Verified locally with the issue's hugo.toml. Output for permalinks, privacy, etc. is now collapsed to just the populated fields, in --format toml, --format yaml, and --format json. --printZero is unchanged (that path doesn't enter the OmitEmpty block).

Added a regression test in parser/lowercase_camel_json_test.go covering:

  • a nested chain of maps that should fully prune away
  • a target.sites.{matrix,complements} shape with empty slice fields
  • a slice-of-maps where the entry has an empty sub map but a populated sibling key (verifies the slice path still strips the sub map but keeps the entry)

Fixes #14855

When OmitEmpty is true, the marshaller recursively pruned zero-valued
leaves from the unmarshalled map, but left empty parent maps in place.
This surfaced in 'hugo config' as empty TOML tables such as
[permalinks.target.sites] and its nested children, even when every
field underneath was zero.

After recursing into a child map, delete the parent's key if the
child is now empty. Slices of maps are unaffected because removing
slice entries would shift indices.

Fixes gohugoio#14855

Signed-off-by: ChrisJr404 <chris@hacknow.com>
@gemini-code-assist
Copy link
Copy Markdown

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@bep
Copy link
Copy Markdown
Member

bep commented May 11, 2026

Can you push the "sign the CLA" button above? If you already did,. you need to check if your email address for the commit matches one of the addresses connected to your GitHub account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In "hugo config", don't print empty sub maps

3 participants