gohugoio/hugo · error

failed to read import %q: %w

Error message

failed to read import %q: %w

What it means

Thrown inside the batch build's ImportOnLoadFunc when resources.InternalResourceSourceContent fails to read the source content of a previously resolved import resource. The wrapped error reveals the underlying read failure (file missing, I/O error, or a stale cache entry pointing at a deleted source).

Source

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

					if resolved != nil {
						resolvePath := resources.InternalResourceTargetPath(resolved)
						dm.AddIdentity(identity.FirstIdentity(resolved))
						imp := PrefixHugoVirtual + resolvePath
						state.importResource.Set(imp, resolved)
						state.importerImportContext.Set(imp, importContext)
						return imp

					}
				}
				return ""
			},
			ImportOnLoadFunc: func(args api.OnLoadArgs) (string, error) {
				imp := args.Path

				if r, found := state.importResource.Get(imp); found {
					content, err := resources.InternalResourceSourceContent(ctx, r)
					if err != nil {
						return "", fmt.Errorf("failed to read import %q: %w", resources.InternalResourceSourcePathBestEffort(r), err)
					}
					return content, nil
				}
				return "", nil
			},
			ImportParamsOnLoadFunc: func(args api.OnLoadArgs) json.RawMessage {
				if importContext, found := state.importerImportContext.Get(args.Path); found {
					if !importContext.scriptOptions.IsZero() {
						return importContext.scriptOptions.Params
					}
				}
				return nil
			},
			ErrorMessageResolveFunc: func(args api.Message) *ErrorMessageResolved {
				if loc := args.Location; loc != nil {
					path := strings.TrimPrefix(loc.File, NsHugoImportResolveFunc+":")
					if r, found := state.importResource.Get(path); found {
						sourcePath := resources.InternalResourceSourcePathBestEffort(r)

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Verify the asset file printed in the %q still exists at that path and is readable.
  2. Restart `hugo server` to clear the in-memory importResource cache and re-resolve.
  3. Delete the resources/_gen and _tmp directories to drop cached state, then rebuild.
  4. Check filesystem permissions on the /assets tree.
Defensive patterns

Strategy: try-catch

Try / catch

Wrap the batch build call in error handling; on this specific error, log the import path from the wrapped error, clear the resources/_gen cache, and retry once. If it persists, surface the path to the user as a missing-asset diagnostic.

Prevention

When it happens

Trigger: During a js.Batcher build, an import that was successfully resolved and cached in state.importResource cannot have its source content read at esbuild load time. The resolve step succeeded but InternalResourceSourceContent(ctx, r) returned an error when esbuild asked for the bytes.

Common situations: Hugo server running while the asset file is renamed, moved, or deleted mid-build; resources/_gen cache pointing at a stale resource; permission denied on the source file; cross-platform path separator mismatches producing a path that no longer opens.

Related errors


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