larksuite/cli · error
Hooks.NewArgs is required
Error message
Hooks.NewArgs is required
What it means
Besides the type pair, CompileCommandDefinition requires Hooks.NewArgs — the hook that constructs a fresh args pointer for each invocation. A nil NewArgs hook means the runtime could never instantiate the command's args, so compilation is rejected.
Source
Thrown at shortcuts/common/typed_external.go:25
import (
"context"
"fmt"
"io"
"reflect"
"github.com/larksuite/cli/internal/commandbridge"
)
// CompileCommandDefinition is the single sealed entry from commandhost into
// the existing shortcut compiler. All authoring fields retain
// extension/command as their owner; the internal token prevents this function
// from becoming a second public compiler API.
func CompileCommandDefinition(definition commandbridge.Definition, _ commandbridge.Access) (Shortcut, error) {
if definition.ArgsType == nil || definition.DataType == nil {
return Shortcut{}, fmt.Errorf("ArgsType and DataType are required")
}
if definition.Hooks.NewArgs == nil {
return Shortcut{}, fmt.Errorf("Hooks.NewArgs is required")
}
newArgs, err := probeNewArgs(definition.Hooks.NewArgs)
if err != nil {
return Shortcut{}, err
}
if newArgs == nil || reflect.TypeOf(newArgs) != reflect.PointerTo(definition.ArgsType) {
return Shortcut{}, fmt.Errorf("Hooks.NewArgs must return *%s", definition.ArgsType)
}
output := definition.Output
if definition.PageOutput {
output.Meta.Pagination = true
}
compiled, err := compileDefinitionParts(
definition.Metadata,
definition.Input,
output,
definition.ArgsType,View on GitHub (pinned to 7fd6ef3c07)
Solutions
- Set Hooks.NewArgs to a function returning a pointer to the args type: func() any { return &MyArgs{} }
- Ensure the Hooks struct literal includes NewArgs alongside Execute/Renderers
- If args are genuinely absent, that is unsupported — every typed command needs an args type and NewArgs
Example fix
// before
Hooks: commandbridge.Hooks{Execute: execute},
// after
Hooks: commandbridge.Hooks{Execute: execute, NewArgs: func() any { return &MyArgs{} }}, Defensive patterns
Strategy: type-guard
Validate before calling
if definition.Hooks.NewArgs == nil { return errors.New("Hooks.NewArgs is required") } Type guard
func hasNewArgs(h commandbridge.Hooks) bool { return h.NewArgs != nil } Try / catch
sc, err := common.CompileCommandDefinition(def, access)
if err != nil {
return fmt.Errorf("compile %s: %w", def.Metadata.Command, err)
} Prevention
- Provide NewArgs together with Execute in every Hooks literal
- Use a shared helper that builds Hooks with NewArgs mandatory
- Compile all commands in a startup test so missing hooks fail fast
When it happens
Trigger: definition.Hooks.NewArgs left nil while ArgsType/DataType are set; building Hooks struct literal without the NewArgs field; a hook removed during refactoring.
Common situations: Authoring a new typed command and populating only Hooks.Execute; copying a Hooks literal from a command that used a different construction pattern; zero-value Hooks struct.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- ArgsType and DataType are required
- Hooks.NewArgs must return *%s
- %v
- hook %q panic: %v
- %s.When must be trimmed
AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04).
Data as JSON: /api/errors/148739a2e34d1b1c.
Report an issue: GitHub.