Skip to content

generate --output-multiple-files panics on a template with no file #156

Description

@crhntr

muxt generate --output-multiple-files panics when a template has no file of its own, for example one added with template.New("GET /health Health()").Parse(...).

panic: runtime error: index out of range [0] with length 0

github.com/typelate/muxt/internal/generate.accumulateReceiverMethods.func1(...)
slices.IndexFunc[...](...)
github.com/typelate/muxt/internal/generate.accumulateReceiverMethods(...)
github.com/typelate/muxt/internal/generate.hydrateGroup(...)
github.com/typelate/muxt/internal/generate.TemplateRoutesFiles(...)

accumulateReceiverMethods reads field.Names[0].Name for every field in the receiver interface. With --output-multiple-files the main file's interface embeds the per-file interfaces, and an embedded field has no Names, so the lookup panics as soon as a route in the main file adds a method.

Reproduced with a binary built from f7e9368:

module server, index.gohtml holding {{define "GET /{$} Home()"}}, and

var templates = template.Must(template.Must(template.ParseFS(templateFiles, "*.gohtml")).
	New("GET /health Health()").Parse(`ok`))

muxt generate --use-receiver-type=T --output-multiple-files

Found while writing snapshot tests for --output-multiple-files; the archive in #151 documents only what does work, because this case panics.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions