From d4ce6799b683567844bac399fe4e09a1353023d8 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 20 Sep 2026 12:05:39 +1000 Subject: [PATCH] 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 " for every interface target. --- .../eda/module/log/logextract/logextract.go | 7 +- ...93-archie-core-internal-domain-workflow.go | 71 +------- .../workflow/wfextract/reachability_test.go | 50 +---- .../domain/workflow/wfextract/wfextract.go | 8 +- internal/plugin/plugin.go | 8 +- internal/plugin/plugin_test.go | 171 ++++++++++-------- ...amcharles93-archie-core-internal-plugin.go | 15 -- .../plugin/pluginextract/pluginextract.go | 8 +- .../pluginextract/pluginextract_test.go | 36 +--- .../plugin/pluginextract/reachability_test.go | 29 +-- internal/secret/loaddir_test.go | 72 ++------ ...amcharles93-archie-core-internal-secret.go | 9 - .../secret/secretextract/secretextract.go | 8 +- internal/secret/secretextract/wrapper_test.go | 20 +- internal/yaegiutil/yaegiutil.go | 44 ++++- internal/yaegiutil/yaegiutil_test.go | 17 ++ 16 files changed, 212 insertions(+), 361 deletions(-) diff --git a/internal/domain/eda/module/log/logextract/logextract.go b/internal/domain/eda/module/log/logextract/logextract.go index f8a8d8af8..61123db13 100644 --- a/internal/domain/eda/module/log/logextract/logextract.go +++ b/internal/domain/eda/module/log/logextract/logextract.go @@ -3,10 +3,9 @@ // from the log package 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. 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 logextract import "reflect" diff --git a/internal/domain/workflow/wfextract/github_com-samcharles93-archie-core-internal-domain-workflow.go b/internal/domain/workflow/wfextract/github_com-samcharles93-archie-core-internal-domain-workflow.go index 0920c494d..c3229b9be 100644 --- a/internal/domain/workflow/wfextract/github_com-samcharles93-archie-core-internal-domain-workflow.go +++ b/internal/domain/workflow/wfextract/github_com-samcharles93-archie-core-internal-domain-workflow.go @@ -4,13 +4,12 @@ package wfextract import ( "context" - "go/constant" - "go/token" - "reflect" - "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/events" + "go/constant" + "go/token" + "reflect" ) func init() { @@ -135,44 +134,21 @@ type _github_com_samcharles93_archie_core_internal_domain_workflow_Forger struct } func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Forger) CloseIssue(ctx context.Context, owner string, repo string, number int, comment string) error { - if W.WCloseIssue == nil { - return nil - } return W.WCloseIssue(ctx, owner, repo, number, comment) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Forger) Comment(ctx context.Context, owner string, repo string, number int, body string) (int64, error) { - if W.WComment == nil { - return 0, nil - } return W.WComment(ctx, owner, repo, number, body) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Forger) CreatePR(ctx context.Context, owner string, repo string, title string, head string, base string, body string) (int, error) { - if W.WCreatePR == nil { - return 0, nil - } return W.WCreatePR(ctx, owner, repo, title, head, base, body) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Forger) CreateReviewComments(ctx context.Context, owner string, repo string, number int, reviewedHeadSHA string, comments []workflow.ReviewComment) error { - if W.WCreateReviewComments == nil { - return nil - } return W.WCreateReviewComments(ctx, owner, repo, number, reviewedHeadSHA, comments) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Forger) LinkBranch(ctx context.Context, owner string, repo string, issueNumber int, branch string) error { - if W.WLinkBranch == nil { - return nil - } return W.WLinkBranch(ctx, owner, repo, issueNumber, branch) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Forger) ReplyToReview(ctx context.Context, owner string, repo string, number int, commentID int64, body string) error { - if W.WReplyToReview == nil { - return nil - } return W.WReplyToReview(ctx, owner, repo, number, commentID, body) } @@ -183,9 +159,6 @@ type _github_com_samcharles93_archie_core_internal_domain_workflow_Reviewer stru } func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Reviewer) Review(ctx context.Context, req workflow.ReviewRequest) workflow.ReviewReport { - if W.WReview == nil { - return workflow.ReviewReport{} - } return W.WReview(ctx, req) } @@ -198,23 +171,12 @@ type _github_com_samcharles93_archie_core_internal_domain_workflow_Store struct } func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Store) InsertEvent(ctx context.Context, e events.Event) (int64, error) { - if W.WInsertEvent == nil { - return 0, nil - } return W.WInsertEvent(ctx, e) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Store) Transition(ctx context.Context, taskID int64, from string, to string, detail string) error { - if W.WTransition == nil { - return nil - } return W.WTransition(ctx, taskID, from, to, detail) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Store) Update(ctx context.Context, t *task.Task) error { - if W.WUpdate == nil { - return nil - } return W.WUpdate(ctx, t) } @@ -231,50 +193,23 @@ type _github_com_samcharles93_archie_core_internal_domain_workflow_Trees struct } func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Trees) ChangedFiles(ctx context.Context, dir string, base string) ([]string, error) { - if W.WChangedFiles == nil { - return nil, nil - } return W.WChangedFiles(ctx, dir, base) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Trees) ChangedLines(ctx context.Context, dir string, base string) (int, error) { - if W.WChangedLines == nil { - return 0, nil - } return W.WChangedLines(ctx, dir, base) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Trees) CommitAll(ctx context.Context, dir string, message string) (bool, error) { - if W.WCommitAll == nil { - return false, nil - } return W.WCommitAll(ctx, dir, message) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Trees) Diff(ctx context.Context, dir string, base string) (string, error) { - if W.WDiff == nil { - return "", nil - } return W.WDiff(ctx, dir, base) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Trees) Prepare(ctx context.Context, owner string, repo string, base string, issue int, title string, body string, labels string) (dir string, branch string, err error) { - if W.WPrepare == nil { - return "", "", nil - } return W.WPrepare(ctx, owner, repo, base, issue, title, body, labels) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Trees) Push(ctx context.Context, dir string, branch string) error { - if W.WPush == nil { - return nil - } return W.WPush(ctx, dir, branch) } - func (W _github_com_samcharles93_archie_core_internal_domain_workflow_Trees) Snapshot(ctx context.Context, dir string, destDir string) error { - if W.WSnapshot == nil { - return nil - } return W.WSnapshot(ctx, dir, destDir) } diff --git a/internal/domain/workflow/wfextract/reachability_test.go b/internal/domain/workflow/wfextract/reachability_test.go index fcb319900..0d33379a8 100644 --- a/internal/domain/workflow/wfextract/reachability_test.go +++ b/internal/domain/workflow/wfextract/reachability_test.go @@ -1,7 +1,6 @@ package wfextract import ( - "context" "reflect" "testing" @@ -76,50 +75,11 @@ func UseRouting() error { } } -// TestWrapperNilGuardsPreserved is the Go-side nil-guard regression test, -// following the established pattern in secretextract/wrapper_test.go and -// pluginextract's TestGeneratedWrapperHasNilGuards: yaegi's fresh wrapper -// generation omits nil-guards, 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 forger _github_com_samcharles93_archie_core_internal_domain_workflow_Forger - if err := forger.CloseIssue(context.Background(), "", "", 0, ""); err != nil { - t.Errorf("CloseIssue on nil WCloseIssue = %v, want nil", err) - } - if _, err := forger.CreatePR(context.Background(), "", "", "", "", "", ""); err != nil { - t.Errorf("CreatePR on nil WCreatePR = %v, want nil", err) - } - if err := forger.LinkBranch(context.Background(), "", "", 0, ""); err != nil { - t.Errorf("LinkBranch on nil WLinkBranch = %v, want nil", err) - } - - if _, err := forger.Comment(context.Background(), "", "", 0, ""); err != nil { - t.Errorf("Comment on nil WComment = %v, want nil", err) - } - if err := forger.ReplyToReview(context.Background(), "", "", 0, 0, ""); err != nil { - t.Errorf("ReplyToReview on nil WReplyToReview = %v, want nil", err) - } - if err := forger.CreateReviewComments(context.Background(), "", "", 0, "", nil); err != nil { - t.Errorf("CreateReviewComments on nil WCreateReviewComments = %v, want nil", err) - } - - var reviewer _github_com_samcharles93_archie_core_internal_domain_workflow_Reviewer - if got := reviewer.Review(context.Background(), workflow.ReviewRequest{}); len(got.Findings) != 0 || got.Status != "" || got.Summary != "" { - t.Errorf("Review on nil WReview = %#v, want empty report", got) - } - - var trees _github_com_samcharles93_archie_core_internal_domain_workflow_Trees - if _, _, err := trees.Prepare(context.Background(), "", "", "", 0, "", "", ""); err != nil { - t.Errorf("Prepare on nil WPrepare = %v, want nil", err) - } - if _, err := trees.Diff(context.Background(), "", ""); err != nil { - t.Errorf("Diff on nil WDiff = %v, want nil", err) - } - if _, err := trees.ChangedLines(context.Background(), "", ""); err != nil { - t.Errorf("ChangedLines on nil WChangedLines = %v, want nil", err) - } -} +// 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 WCloseIssue reported a closed issue that was +// never touched. A wrapper missing a method is now refused in +// yaegiutil.Resolve, before it can reach a call site. // TestChangeCaptureIsNotCallableFromInterpretedCode is the acceptance test for // "the sandbox surface did not widen", in the same shape as the reachability diff --git a/internal/domain/workflow/wfextract/wfextract.go b/internal/domain/workflow/wfextract/wfextract.go index 1a4ffed4b..abd498cfe 100644 --- a/internal/domain/workflow/wfextract/wfextract.go +++ b/internal/domain/workflow/wfextract/wfextract.go @@ -2,11 +2,9 @@ // generated by `go generate`. Kept separate from internal/domain/workflow // 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 `TestWrapperNilGuardsPreserved` 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 wfextract import "reflect" diff --git a/internal/plugin/plugin.go b/internal/plugin/plugin.go index 1d557271c..3a7162013 100644 --- a/internal/plugin/plugin.go +++ b/internal/plugin/plugin.go @@ -18,9 +18,11 @@ import ( "github.com/samcharles93/archie-core/internal/yaegiutil" ) -// Plugin is a core plugin loaded by the daemon at startup. Each .go file -// in ~/.config/archie/plugins/ must export a variable named "Plugin" that -// satisfies this interface. +// Plugin is a core plugin loaded by the daemon at startup. Each .go file in +// ~/.config/archie/plugins/ must export a variable named "Plugin" declared +// with an interface type: "var Plugin plugin.Plugin = impl{}". A +// concrete-typed export is refused, because Yaegi only bridges an interpreted +// value to Go as an interface when the declaration names one. type Plugin interface { Name() string Version() string diff --git a/internal/plugin/plugin_test.go b/internal/plugin/plugin_test.go index 5c6af6081..8014783d6 100644 --- a/internal/plugin/plugin_test.go +++ b/internal/plugin/plugin_test.go @@ -3,11 +3,16 @@ package plugin_test import ( "os" "path/filepath" + "reflect" + "strings" "testing" "time" + "github.com/traefik/yaegi/interp" + "github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/plugin/pluginextract" + "github.com/samcharles93/archie-core/internal/yaegiutil" ) // ── Plugin interface ───────────────────────────────────────────────── @@ -22,6 +27,65 @@ func TestPluginInterfaceExists(t *testing.T) { var _ nameVersioner = p } +func TestPartialImplementationIsRejectedBeforeWrapping(t *testing.T) { + // Why the generated interface wrappers carry no nil-guards: Yaegi + // type-checks the assignment to the interface before it ever builds a + // wrapper, so a type missing a method never reaches Go as a wrapper with + // a nil method field. Guarding the generated wrappers by hand defends a + // state Yaegi cannot produce, and every regeneration silently drops it. + i, err := yaegiutil.New(interp.Options{}, symbols) + if err != nil { + t.Fatal(err) + } + _, err = yaegiutil.Resolve[plugin.Plugin](i, `package main + +import "github.com/samcharles93/archie-core/internal/plugin" + +type partial struct{} + +func (partial) Name() string { return "partial" } + +var Plugin plugin.Plugin = partial{} +`, "main.Plugin") + if err == nil { + t.Fatal("want error for a Plugin missing Version(), got nil") + } + if !strings.Contains(err.Error(), "cannot use type") { + t.Errorf("want a Yaegi assignment type error, got: %v", err) + } +} + +func TestCompleteImplementationWrapsWithNoNilMethods(t *testing.T) { + i, err := yaegiutil.New(interp.Options{}, symbols) + if err != nil { + t.Fatal(err) + } + p, err := yaegiutil.Resolve[plugin.Plugin](i, `package main + +import "github.com/samcharles93/archie-core/internal/plugin" + +type complete struct{} + +func (complete) Name() string { return "n" } +func (complete) Version() string { return "v" } + +var Plugin plugin.Plugin = complete{} +`, "main.Plugin") + if err != nil { + t.Fatal(err) + } + rv := reflect.ValueOf(p) + if rv.Kind() != reflect.Struct { + t.Fatalf("want a Yaegi wrapper struct, got %T", p) + } + for j := range rv.NumField() { + f := rv.Type().Field(j) + if f.Type.Kind() == reflect.Func && rv.Field(j).IsNil() { + t.Errorf("wrapper field %s is nil; Yaegi is expected to populate every method", f.Name) + } + } +} + // ── LoadDir behavioral tests ───────────────────────────────────────── var symbols = pluginextract.Symbols @@ -178,11 +242,10 @@ func TestLoadDirNonexistentDirReturnsNil(t *testing.T) { // These tests try to BREAK LoadDir. A correct implementation must // degrade gracefully -- skip the bad plugin, load the rest, never panic. -func TestLoadDirNilFunctionFieldsLoadsWithoutPanic(t *testing.T) { - // A _Plugin with nil WName/WVersion must LOAD without panicking - // AND must not panic when Name()/Version() is called -- the wrapper - // must nil-guard. Otherwise logging plugin names at startup crashes - // the daemon. +func TestLoadDirRefusesWrapperWithNilFunctionFields(t *testing.T) { + // A _Plugin with nil WName/WVersion must be refused at load. It + // previously loaded and answered "" for both, so the daemon logged a + // nameless plugin as if it were healthy. dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, "nilfuncs.go"), []byte(`package main @@ -200,19 +263,8 @@ var Plugin = plugin._Plugin{ if err != nil { t.Fatal(err) } - if len(plugins) != 1 { - t.Fatalf("LoadDir returned %d plugins, want 1 (nil funcs should not prevent loading)", len(plugins)) - } - - // These must not panic -- the _Plugin wrapper must nil-guard. - // The daemon calls Name()/Version() at startup to log loaded plugins. - name := plugins[0].Name() - version := plugins[0].Version() - if name != "" { - t.Errorf("Name() = %q, want empty string from nil WName", name) - } - if version != "" { - t.Errorf("Version() = %q, want empty string from nil WVersion", version) + if len(plugins) != 0 { + t.Fatalf("LoadDir returned %d plugins, want 0 (a wrapper missing methods must be refused)", len(plugins)) } } @@ -284,6 +336,32 @@ var Plugin partial } } +func TestLoadDirSkipsHandBuiltPartialWrapper(t *testing.T) { + // Yaegi type-checks "var Plugin plugin.Plugin = impl{}" and refuses an + // incomplete impl, but interpreted code can also build the generated + // wrapper directly, where struct-literal semantics leave an omitted method + // nil and the call panics at first use. LoadDir must refuse it instead. + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "partialwrap.go"), []byte(`package main + +import "github.com/samcharles93/archie-core/internal/plugin" + +var Plugin = plugin._Plugin{ + WName: func() string { return "halfbuilt" }, +} +`), 0o644); err != nil { + t.Fatal(err) + } + + plugins, err := plugin.LoadDir(dir, symbols) + if err != nil { + t.Fatal(err) + } + if len(plugins) != 0 { + t.Fatalf("LoadDir returned %d plugins from a wrapper missing WVersion, want 0", len(plugins)) + } +} + func TestLoadDirEmptyDirReturnsEmptySlice(t *testing.T) { dir := t.TempDir() plugins, err := plugin.LoadDir(dir, symbols) @@ -383,59 +461,10 @@ var Plugin = struct{}{} _ = plugins } -func TestGeneratedWrapperHasNilGuards(t *testing.T) { - // C4: the generated _Plugin wrapper must nil-guard WName and WVersion. - // If go generate strips these guards, the daemon panics at startup - // when logging plugin names. This test verifies the guards exist - // and will fail if someone regenerates without them. - // - // The _Plugin type is in the pluginextract package. We verify - // indirectly by loading a plugin with nil funcs and calling - // Name()/Version() -- they must return empty strings, not panic. - - dir := t.TempDir() - if err := os.WriteFile(filepath.Join(dir, "nilguard.go"), []byte(`package main - -import "github.com/samcharles93/archie-core/internal/plugin" - -var Plugin = plugin._Plugin{ - WName: nil, - WVersion: nil, -} -`), 0o644); err != nil { - t.Fatal(err) - } - - plugins, err := plugin.LoadDir(dir, symbols) - if err != nil { - t.Fatal(err) - } - if len(plugins) != 1 { - t.Fatal("plugin with nil funcs not loaded") - } - - // These must not panic. If they do, go generate was run and stripped - // the nil guards from the generated wrapper. - var name, version string - func() { - defer func() { - if r := recover(); r != nil { - t.Fatalf("Name()/Version() panicked with nil funcs -- "+ - "the generated _Plugin wrapper is missing nil guards. "+ - "Re-run `go generate` may have overwritten them: %v", r) - } - }() - name = plugins[0].Name() - version = plugins[0].Version() - }() - - if name != "" { - t.Errorf("Name() = %q, want empty from nil WName", name) - } - if version != "" { - t.Errorf("Version() = %q, want empty from nil WVersion", version) - } -} +// The former TestGeneratedWrapperHasNilGuards was retired. It pinned +// hand-added nil guards in the generated wrapper, which every regeneration +// silently dropped. LoadDir now refuses such a plugin outright; see +// TestLoadDirRefusesWrapperWithNilFunctionFields. func TestLoadDirPluginWithGoBuildTag(t *testing.T) { // Build tags in Yaegi-interpreted code are just comments -- they diff --git a/internal/plugin/pluginextract/github_com-samcharles93-archie-core-internal-plugin.go b/internal/plugin/pluginextract/github_com-samcharles93-archie-core-internal-plugin.go index eea32b2cb..2987d2770 100644 --- a/internal/plugin/pluginextract/github_com-samcharles93-archie-core-internal-plugin.go +++ b/internal/plugin/pluginextract/github_com-samcharles93-archie-core-internal-plugin.go @@ -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() } diff --git a/internal/plugin/pluginextract/pluginextract.go b/internal/plugin/pluginextract/pluginextract.go index 0b79c7bb0..89f3a07d6 100644 --- a/internal/plugin/pluginextract/pluginextract.go +++ b/internal/plugin/pluginextract/pluginextract.go @@ -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" diff --git a/internal/plugin/pluginextract/pluginextract_test.go b/internal/plugin/pluginextract/pluginextract_test.go index 6564480d6..8a45f6e6c 100644 --- a/internal/plugin/pluginextract/pluginextract_test.go +++ b/internal/plugin/pluginextract/pluginextract_test.go @@ -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. diff --git a/internal/plugin/pluginextract/reachability_test.go b/internal/plugin/pluginextract/reachability_test.go index bb4fd9fcc..1645d447a 100644 --- a/internal/plugin/pluginextract/reachability_test.go +++ b/internal/plugin/pluginextract/reachability_test.go @@ -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{} diff --git a/internal/secret/loaddir_test.go b/internal/secret/loaddir_test.go index ad653bb57..b3dcf6dae 100644 --- a/internal/secret/loaddir_test.go +++ b/internal/secret/loaddir_test.go @@ -148,11 +148,11 @@ var Engine = secret._Engine{ // ── Adversarial tests ──────────────────────────────────────────────── -func TestLoadDirNilFunctionFieldsLoadsWithoutPanic(t *testing.T) { - // A _Engine with nil WName/WVersion/WResolve must LOAD without - // panicking AND must not panic when called -- the wrapper must - // nil-guard. Otherwise resolving a secret from a malformed custom - // engine crashes the daemon. +func TestLoadDirRefusesEngineWithNilFunctionFields(t *testing.T) { + // A hand-built _Engine with nil method fields must be refused at load. + // It previously loaded and answered ("", nil) from a nil WResolve, so a + // malformed engine returned an empty secret and reported success -- a + // silent wrong answer is worse for a secret lookup than a refusal. dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, "nilfuncs.go"), []byte(`package main @@ -172,19 +172,8 @@ var Engine = secret._Engine{ if err != nil { t.Fatal(err) } - if n != 1 { - t.Fatalf("LoadDir returned %d, want 1 (nil funcs should not prevent loading)", n) - } - - e, _ := r.Get("") - if name := e.Name(); name != "" { - t.Errorf("Name() = %q, want empty from nil WName", name) - } - if version := e.Version(); version != "" { - t.Errorf("Version() = %q, want empty from nil WVersion", version) - } - if v, err := e.Resolve("k"); err != nil || v != "" { - t.Errorf("Resolve() = (%q, %v), want (\"\", nil) from nil WResolve", v, err) + if n != 0 { + t.Fatalf("LoadDir returned %d, want 0 (an engine missing methods must be refused)", n) } } @@ -314,45 +303,8 @@ var Engine = secret._Engine{ } } -func TestGeneratedWrapperHasNilGuards(t *testing.T) { - // If go generate is re-run on secretextract, it overwrites the - // generated wrapper with Yaegi's default (no nil-guards). This test - // fails loudly if that happens -- a custom engine that omits - // WResolve would otherwise panic the daemon on first secret lookup. - dir := t.TempDir() - if err := os.WriteFile(filepath.Join(dir, "nilguard.go"), []byte(`package main - -import "github.com/samcharles93/archie-core/internal/secret" - -var Engine = secret._Engine{ - WName: nil, - WVersion: nil, - WResolve: nil, -} -`), 0o644); err != nil { - t.Fatal(err) - } - - r := secret.NewRegistry() - n, err := r.LoadDir(dir, symbols) - if err != nil { - t.Fatal(err) - } - if n != 1 { - t.Fatal("engine with nil funcs not loaded") - } - - e, _ := r.Get("") - func() { - defer func() { - if rec := recover(); rec != nil { - t.Fatalf("Name()/Version()/Resolve() panicked with nil funcs -- "+ - "the generated _Engine wrapper is missing nil guards. "+ - "Re-run of `go generate` may have overwritten them: %v", rec) - } - }() - _ = e.Name() - _ = e.Version() - _, _ = e.Resolve("k") - }() -} +// The former TestGeneratedWrapperHasNilGuards was retired. It pinned +// hand-added nil guards in the generated wrapper, which every regeneration +// silently dropped, and the behaviour it protected -- a nil WResolve +// answering ("", nil) -- was a silent wrong answer. LoadDir now refuses such +// an engine outright; see TestLoadDirRefusesEngineWithNilFunctionFields. diff --git a/internal/secret/secretextract/github_com-samcharles93-archie-core-internal-secret.go b/internal/secret/secretextract/github_com-samcharles93-archie-core-internal-secret.go index 3047ee57e..f1dfc3eec 100644 --- a/internal/secret/secretextract/github_com-samcharles93-archie-core-internal-secret.go +++ b/internal/secret/secretextract/github_com-samcharles93-archie-core-internal-secret.go @@ -32,20 +32,11 @@ type _github_com_samcharles93_archie_core_internal_secret_Engine struct { } func (W _github_com_samcharles93_archie_core_internal_secret_Engine) Name() string { - if W.WName == nil { - return "" - } return W.WName() } func (W _github_com_samcharles93_archie_core_internal_secret_Engine) Resolve(key string) (string, error) { - if W.WResolve == nil { - return "", nil - } return W.WResolve(key) } func (W _github_com_samcharles93_archie_core_internal_secret_Engine) Version() string { - if W.WVersion == nil { - return "" - } return W.WVersion() } diff --git a/internal/secret/secretextract/secretextract.go b/internal/secret/secretextract/secretextract.go index 3908f36b8..f827dc1a2 100644 --- a/internal/secret/secretextract/secretextract.go +++ b/internal/secret/secretextract/secretextract.go @@ -2,11 +2,9 @@ // generated by `go generate`. Kept separate from internal/secret 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 secretextract import "reflect" diff --git a/internal/secret/secretextract/wrapper_test.go b/internal/secret/secretextract/wrapper_test.go index 35d2ef548..7e54f5188 100644 --- a/internal/secret/secretextract/wrapper_test.go +++ b/internal/secret/secretextract/wrapper_test.go @@ -38,20 +38,6 @@ func TestEngineWrapperResolvePropagatesError(t *testing.T) { } } -func TestEngineWrapperNilGuards(t *testing.T) { - // The zero-value wrapper (all function fields nil) must not panic; this - // is the nil-guard behaviour TestGeneratedWrapperHasNilGuards protects - // against regenerate wiping out (see package doc comment). - var w _github_com_samcharles93_archie_core_internal_secret_Engine - - if got, want := w.Name(), ""; got != want { - t.Errorf("Name() with nil WName = %q, want %q", got, want) - } - if got, want := w.Version(), ""; got != want { - t.Errorf("Version() with nil WVersion = %q, want %q", got, want) - } - got, err := w.Resolve("key") - if got != "" || err != nil { - t.Errorf("Resolve() with nil WResolve = (%q, %v), want (\"\", nil)", got, err) - } -} +// The former TestEngineWrapperNilGuards was retired along with the hand-added +// nil guards it pinned. A wrapper missing a method is now refused where it +// enters the daemon, in yaegiutil.Resolve, so it never reaches a call site. diff --git a/internal/yaegiutil/yaegiutil.go b/internal/yaegiutil/yaegiutil.go index 2164c776f..482591f5b 100644 --- a/internal/yaegiutil/yaegiutil.go +++ b/internal/yaegiutil/yaegiutil.go @@ -12,6 +12,7 @@ package yaegiutil import ( "fmt" "reflect" + "strings" "time" "github.com/traefik/yaegi/interp" @@ -75,11 +76,52 @@ func Resolve[T any](i *interp.Interpreter, src, exportPath string) (result T, er } fn, ok := v.Interface().(T) if !ok { - return result, fmt.Errorf("%s is %T, want %T", exportPath, v.Interface(), result) + return result, fmt.Errorf("%s is %T, want %s", exportPath, v.Interface(), reflect.TypeFor[T]()) + } + if err := checkComplete(fn); err != nil { + return result, fmt.Errorf("%s %w", exportPath, err) } return fn, nil } +// checkComplete rejects a Yaegi interface wrapper that is missing a method +// implementation. Yaegi type-checks "var X Iface = impl{}" and refuses an +// incomplete impl before it ever builds a wrapper, but the generated wrapper +// type is itself exported into the symbol table, so interpreted code can build +// one directly ("var X = pkg._Iface{WName: ...}"). Struct-literal semantics +// leave an omitted method nil, the wrapper still satisfies the interface +// statically, and the omission surfaces as a nil-call panic at first use. +// Values that are not wrappers (plain funcs, ordinary Go types) pass through. +func checkComplete(v any) error { + rv := reflect.ValueOf(v) + for rv.Kind() == reflect.Pointer && !rv.IsNil() { + rv = rv.Elem() + } + if rv.Kind() != reflect.Struct { + return nil + } + rt := rv.Type() + // Every Yaegi wrapper carries the interpreted value alongside its methods. + if _, isWrapper := rt.FieldByName("IValue"); !isWrapper { + return nil + } + + var missing []string + for i := range rt.NumField() { + f := rt.Field(i) + if f.Type.Kind() != reflect.Func || !strings.HasPrefix(f.Name, "W") { + continue + } + if rv.Field(i).IsNil() { + missing = append(missing, strings.TrimPrefix(f.Name, "W")) + } + } + if len(missing) > 0 { + return fmt.Errorf("does not implement %s", strings.Join(missing, ", ")) + } + return nil +} + // Safe runs fn and recovers any panic -- interpreted code is untrusted // relative to the daemon's own logic, so a nil dereference or // out-of-range index inside it must become an error, not a crash. diff --git a/internal/yaegiutil/yaegiutil_test.go b/internal/yaegiutil/yaegiutil_test.go index 147ffd402..fb271cb51 100644 --- a/internal/yaegiutil/yaegiutil_test.go +++ b/internal/yaegiutil/yaegiutil_test.go @@ -37,6 +37,23 @@ func Greet() string { return "hi" }`, "main.Greet") } } +func TestResolveWrongTypeNamesTheWantedType(t *testing.T) { + // The zero value of an interface T formats as under %T, so a failed + // assertion used to read "want " and tell the operator nothing. + i, err := yaegiutil.New(interp.Options{}) + if err != nil { + t.Fatal(err) + } + _, err = yaegiutil.Resolve[interface{ Name() string }](i, `package main +var Greet = 42`, "main.Greet") + if err == nil { + t.Fatal("want error for wrong type, got nil") + } + if strings.Contains(err.Error(), "") { + t.Errorf("error must name the wanted interface, got: %v", err) + } +} + func TestResolveMissingExport(t *testing.T) { i, err := yaegiutil.New(interp.Options{}) if err != nil {