{"record":{"id":"e8316a01dc7c150f","repo":"gohugoio/hugo","slug":"failed-to-build-js-batch-w","errorCode":null,"errorMessage":"failed to build JS batch: %w","messagePattern":"failed to build JS batch: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/esbuild/batch.go","lineNumber":508,"sourceCode":"\t\t\t\treturn k.scriptID == vv.Key()\n\t\t\t}\n\t\t\tfor _, vvv := range instances.Filter(predicate) {\n\t\t\t\tbt.Instances = append(bt.Instances, scriptInstanceBatchTemplateContext{opts: vvv})\n\t\t\t}\n\n\t\t\tt.Scripts = append(t.Scripts, bt)\n\t\t}\n\n\t\tfor _, vv := range g.runnersOptions.ByKey() {\n\t\t\trunnerKeyPath := keyPath + \"_\" + vv.Key().String()\n\t\t\trunnerImpPath := paths.AddLeadingSlash(runnerKeyPath + \"_runner\" + vv.Compiled().Resource.MediaType().FirstSuffix.FullSuffix)\n\t\t\tt.Runners = append(t.Runners, scriptRunnerTemplateContext{opts: vv, Import: runnerImpPath})\n\t\t\taddResource(g.id, runnerImpPath, vv.Compiled().Resource, false)\n\t\t}\n\n\t\tr, s, err := b.client.buildBatchGroup(ctx, t)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to build JS batch: %w\", err)\n\t\t}\n\n\t\tstate.importerImportContext.Set(s, importContext{\n\t\t\tname:           s,\n\t\t\tresourceGetter: nil,\n\t\t\tdm:             g.dependencyManager,\n\t\t})\n\n\t\taddResource(g.id, s, r, true)\n\t}\n\n\tmediaTypes := b.client.d.ResourceSpec.MediaTypes()\n\n\texternalOptions := b.configOptions.Compiled().Options\n\tif externalOptions.Format == \"\" {\n\t\texternalOptions.Format = \"esm\"\n\t}\n\tif externalOptions.Format != \"esm\" {","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/internal/js/esbuild/batch.go#L490-L526","documentation":"During the per-group build loop inside build(), buildBatchGroup (batch.go:506-509) executes the runner template and creates a resource via FromString; if either step fails the error is wrapped as 'failed to build JS batch'. Unlike 397 (which carries the batch id), this variant wraps the group-level failure without the id and is one layer deeper.","triggerScenarios":"The runner template (batchGroupTemplateContext) fails to execute (bad template syntax, missing field), or createClient.FromString rejects the generated JS (empty/invalid content, path conflict). Occurs once per script group in the batch.","commonSituations":"A runner template referencing a field removed from batchGroupTemplateContext after an upgrade; generated JS that is empty due to no scripts in the group; a virtual path collision from duplicate group ids.","solutions":["Inspect the wrapped error to distinguish template-execution failure from resource-creation failure.","For template errors, check the runner template against the current batchGroupTemplateContext fields.","For FromString errors, verify each group has valid scripts and a unique keyPath.","Reproduce with a single group to isolate the failing one."],"exampleFix":"// before: runner template references {{ .MissingField }}\n\n// after: use a field that exists on batchGroupTemplateContext (e.g. {{ .ID }})","handlingStrategy":"try-catch","validationCode":"// Ensure each group has at least one script and a unique keyPath before build.\nfunc validateGroup(g *scriptGroup) error {\n    if g.id == \"\" { return fmt.Errorf(\"group id empty\") }\n    if len(g.scriptsOptions.ByKey()) == 0 && len(g.runnersOptions.ByKey()) == 0 {\n        return fmt.Errorf(\"group %q has no scripts/runners\", g.id)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// Wrap per-group build to surface a clear id in logs.\nfor _, g := range groups {\n    if err := buildGroup(ctx, g); err != nil {\n        return fmt.Errorf(\"group %q: %w\", g.id, err)\n    }\n}","preventionTips":["Keep runner templates in sync with batchGroupTemplateContext fields after upgrades.","Ensure unique group ids to avoid virtual path collisions.","Build groups in isolation while debugging to localize failures."],"tags":["esbuild","batch","js","template","build"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}