fix(yaegiutil): refuse incomplete Yaegi interface wrappers at load

The generated symbol tables carried 25 hand-added nil guards across three
packages, re-applied by hand after every regeneration because `yaegi extract`
does not emit them. Regeneration silently dropped them, and three package docs
instructed the next agent to put them back.

The guards also answered the wrong way. A nil WResolve returned ("", nil), so a
malformed secret engine served an empty secret and reported success; a nil
WStop reported a clean shutdown for a module that never ran one.

Reject the wrapper where it enters the daemon instead. Yaegi type-checks
"var X Iface = impl{}" and refuses an incomplete impl on its own, but the
wrapper type is exported into the symbol table, so interpreted code can build
one directly and struct-literal semantics leave the omitted method nil.
Resolve now names the missing method and skips the plugin.

Generated tables are used verbatim again. Also names the wanted type on a
failed assertion, which formatted as "want <nil>" for every interface target.
This commit is contained in:
sam committed 2026-09-20 12:05:39 +10:00
1 parent b2fcf25c0b
commit d4ce6799b6
16 files changed
+212 -361

No files matched your search

@@ -41,21 +41,12 @@ type _github_com_samcharles93_archie_core_internal_plugin_Module struct {
}
func (W _github_com_samcharles93_archie_core_internal_plugin_Module) Manifest() plugin.Manifest {
if W.WManifest == nil {
return plugin.Manifest{}
}
return W.WManifest()
}
func (W _github_com_samcharles93_archie_core_internal_plugin_Module) Start(a0 context.Context) error {
if W.WStart == nil {
return nil
}
return W.WStart(a0)
}
func (W _github_com_samcharles93_archie_core_internal_plugin_Module) Stop(a0 context.Context) error {
if W.WStop == nil {
return nil
}
return W.WStop(a0)
}
@@ -67,14 +58,8 @@ type _github_com_samcharles93_archie_core_internal_plugin_Plugin struct {
}
func (W _github_com_samcharles93_archie_core_internal_plugin_Plugin) Name() string {
if W.WName == nil {
return ""
}
return W.WName()
}
func (W _github_com_samcharles93_archie_core_internal_plugin_Plugin) Version() string {
if W.WVersion == nil {
return ""
}
return W.WVersion()
}
@@ -2,11 +2,9 @@
// generated by `go generate`. Kept separate from internal/plugin itself
// so the generated file can import it without a self-import cycle.
//
// IMPORTANT: Running `go generate` overwrites the generated file with
// Yaegi's default wrapper which does NOT include nil-guards on function
// fields. The `TestGeneratedWrapperHasNilGuards` test catches this if
// regeneration strips the guards. Re-apply nil-guards manually after
// regeneration until Yaegi supports generating them natively.
// The generated file is used verbatim. A wrapper whose method fields are not
// all populated is refused in yaegiutil.Resolve, so it needs no hand-added
// nil-guards -- an earlier convention that every regeneration silently undid.
package pluginextract
import "reflect"
@@ -27,32 +27,10 @@ func TestSymbolsContainsExpectedPluginEntries(t *testing.T) {
}
}
func TestGeneratedWrapperHasNilGuards(t *testing.T) {
// Regression: go generate strips nil-guards from _Plugin wrapper.
// Verify that Plugin is present in the Symbols table and is an
// interface type (as expected for yaegi interface extraction).
pkgKey := "github.com/samcharles93/archie-core/internal/plugin/plugin"
pkg := Symbols[pkgKey]
pluginType, ok := pkg["Plugin"]
if !ok {
t.Fatal("Plugin missing from Symbols")
}
// Plugin is an interface type — yaegi represents interfaces
// differently from struct pointers.
kind := pluginType.Kind()
if kind != reflect.Interface && kind != reflect.Pointer {
t.Errorf("Plugin type kind = %v, want Interface or Ptr", kind)
}
// Verify WName and WVersion exist on the Plugin interface type.
// For interface types, we check the method set.
if kind == reflect.Interface {
methodCount := pluginType.Type().NumMethod()
if methodCount < 2 {
t.Errorf("Plugin interface has %d methods, expected at least 2 (Name, Version)", methodCount)
}
}
}
// The former TestGeneratedWrapperHasNilGuards was retired: it was named for a
// regression Yaegi cannot produce. Yaegi type-checks the assignment to an
// interface before building a wrapper, so a wrapper never reaches Go with a nil
// method field, and the hand-added guards it referred to only survived until the
// next regeneration. That invariant is pinned directly in internal/plugin by
// TestPartialImplementationIsRejectedBeforeWrapping and
// TestCompleteImplementationWrapsWithNoNilMethods.
@@ -89,29 +89,10 @@ func (testModule) Manifest() plugin.Manifest {
func (testModule) Start(context.Context) error { return nil }
func (testModule) Stop(context.Context) error { return nil }
// TestWrapperNilGuardsPreserved follows the established pattern: yaegi's
// fresh wrapper generation omits nil-guards on interface-wrappers, so they
// are re-applied by hand after every regeneration. A zero-value wrapper
// (all function fields nil) must not panic.
func TestWrapperNilGuardsPreserved(t *testing.T) {
var mod _github_com_samcharles93_archie_core_internal_plugin_Module
if err := mod.Start(context.Background()); err != nil {
t.Errorf("Start on nil WStart = %v, want nil", err)
}
if got := mod.Manifest(); got.ID != "" || got.Name != "" || got.APIVersion != "" {
t.Errorf("Manifest on nil WManifest = %#v, want empty", got)
}
if err := mod.Stop(context.Background()); err != nil {
t.Errorf("Stop on nil WStop = %v, want nil", err)
}
var legacy _github_com_samcharles93_archie_core_internal_plugin_Plugin
if got := legacy.Name(); got != "" {
t.Errorf("Name on nil WName = %q, want empty", got)
}
if got := legacy.Version(); got != "" {
t.Errorf("Version on nil WVersion = %q, want empty", got)
}
}
// The former TestWrapperNilGuardsPreserved was retired. It pinned hand-added
// nil guards that every regeneration silently dropped, and the behaviour it
// protected was a lie: a nil WStop reported a clean shutdown for a module that
// never ran one. Such a wrapper is now refused in yaegiutil.Resolve, before it
// can reach a call site.
var _ plugin.Module = testModule{}