gohugoio/hugo · error

resource not set

Error message

resource not set

What it means

Thrown by scriptOptions.compileOptions when the decoded options map for a batch script lacks a non-nil Resource field. Resource (the Hugo resource to compile) is mandatory for every script entry in a batch.

Source

Thrown at internal/js/esbuild/batch.go:1339

		paramsJSON, err = json.Marshal(v.Params)
		if err != nil {
			panic(err)
		}
	}

	if v.Export == "" {
		v.Export = defaults.defaultExport
	}

	compiled := scriptOptions{
		Resource:      v.Resource,
		Export:        v.Export,
		ImportContext: resource.NewCachedResourceGetter(v.ImportContext),
		Params:        paramsJSON,
	}

	if compiled.Resource == nil {
		return scriptOptions{}, fmt.Errorf("resource not set")
	}

	return compiled, nil
}

type scriptRunnerTemplateContext struct {
	opts   optionsGetSetter[scriptID, scriptOptions]
	Import string
}

func (s *scriptRunnerTemplateContext) Export() string {
	return s.opts.Compiled().Export
}

func (c scriptRunnerTemplateContext) MarshalJSON() (b []byte, err error) {
	return json.Marshal(&struct {
		ID string `json:"id"`
	}{

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Ensure each script option map has Resource set to a non-nil Hugo resource.
  2. Verify the template that assembles the options — confirm .Resources.Get returns non-nil before passing it on.
  3. Print the options map with warnf "%#v" $opts to confirm the key/value at runtime.

Example fix

// before
{{ $opts := dict "Export" "default" }}
{{ $batcher.Scripts $opts }}

// after
{{ $r := .Resources.Get "main.ts" }}
{{ $opts := dict "Resource" $r "Export" "default" }}
{{ $batcher.Scripts $opts }}
Defensive patterns

Strategy: validation

Validate before calling

// Before passing options to the batcher, ensure Resource is non-nil.
if opts["Resource"] == nil {
    return fmt.Errorf("batch script options missing required Resource: %#v", opts)
}

Prevention

When it happens

Trigger: Calling js.Batcher and a script's option map either omits the Resource key or passes nil. The map is decoded via mapstructure.WeakDecode, so a missing key decodes to nil and trips the check at batch.go:1338.

Common situations: Template uses a wrong key name (e.g., src instead of resource); a partial that should supply the resource received nil from .Resources.Get but still forwarded the map; refactoring that dropped the Resource key.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/bda034420e563aaa. Report an issue: GitHub.