Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ One repository, one self-contained brick per directory. A Go brick
carries its own go.mod and a TypeScript brick its own package.json,
each with its own CHANGELOG and lint configuration, released
independently under a path-prefixed tag such as `mailkit/v0.1.0` or
`gottext/v0.4.0`. Bricks share code only through those published tags,
never through sibling source, so what you pin is what you get.
`gottext/v0.4.0`. Bricks share code only through those published tags
or the Go pseudo-version of a commit on main, never through sibling
source, so what you pin is what you get.

## Reporting security issues

Expand Down
2 changes: 2 additions & 0 deletions pluginkit/graphwire/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ stdlib-only `pluginkit` module so its gqlparser dependency never enters
### Changed

- The module needs Go 1.27.4.
- `Run` reads each plugin.json with `wire.Read` and rejects every manifest `wire.Read` rejects, with its error, a breaking change.
- The module requires `github.com/gopherium/framework/pluginkit` for that reader, so an application on this version takes that pluginkit too.

## [0.4.0] - 2026-09-26

Expand Down
5 changes: 4 additions & 1 deletion pluginkit/graphwire/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/gopherium/framework/pluginkit/graphwire

go 1.27.4

require github.com/vektah/gqlparser/v2 v2.5.36
require (
github.com/gopherium/framework/pluginkit v0.12.1-0.20261121220358-91b488ad6dbb
github.com/vektah/gqlparser/v2 v2.5.36
)
2 changes: 2 additions & 0 deletions pluginkit/graphwire/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gopherium/framework/pluginkit v0.12.1-0.20261121220358-91b488ad6dbb h1:AYsAXgC+XOYwvRVuG3eFYe3MajstMW5VF3BohM8yMqs=
github.com/gopherium/framework/pluginkit v0.12.1-0.20261121220358-91b488ad6dbb/go.mod h1:8ruHaYkA80It7CN0/UxZxJTrdAds9DUDOwqOB7Td49g=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
Expand Down
47 changes: 19 additions & 28 deletions pluginkit/graphwire/graphwire.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
package graphwire

import (
"encoding/json"
"errors"
"fmt"
"go/format"
"go/token"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"

"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/parser"

"github.com/gopherium/framework/pluginkit/wire"
)

// Config parameterizes the resolver root generation for the consuming application.
Expand Down Expand Up @@ -66,12 +66,10 @@ func validateConfig(cfg Config) error {
return nil
}

// manifest is the subset of plugin.json the graph wiring reads.
// manifest is one plugin.json as wire reads it, with the plugin root it came from.
type manifest struct {
ID string `json:"id"`
Backend string `json:"backend"`
GraphQL bool `json:"graphql"`
root string
wire.Manifest
root string
}

// roots returns the plugin root directories scanned in order, defaulting to plugins.
Expand Down Expand Up @@ -103,8 +101,6 @@ func loadRoots(dir string, roots []string) ([]manifest, error) {
return manifests, nil
}

var idPattern = regexp.MustCompile(`^[a-z][a-z0-9-]*$`)

// loadManifests loads the plugin manifest in each subdirectory of dir.
func loadManifests(dir string) ([]manifest, error) {
entries, err := os.ReadDir(dir)
Expand All @@ -116,8 +112,7 @@ func loadManifests(dir string) ([]manifest, error) {
if !entry.IsDir() {
continue
}
path := filepath.Join(dir, entry.Name(), "plugin.json")
m, err := loadManifest(path, entry.Name())
m, err := loadManifest(dir, entry.Name())
if err != nil {
return nil, err
}
Expand All @@ -126,29 +121,25 @@ func loadManifests(dir string) ([]manifest, error) {
return manifests, nil
}

// loadManifest reads and validates one plugin manifest.
func loadManifest(path, dir string) (manifest, error) {
data, err := os.ReadFile(path)
if err != nil {
return manifest{}, fmt.Errorf("graphwire: %s: %w", path, err)
}
var m manifest
if err := json.Unmarshal(data, &m); err != nil {
return manifest{}, fmt.Errorf("graphwire: %s: %w", path, err)
// loadManifest reads and checks the plugin.json in directory name under dir through wire, its path before any error.
func loadManifest(dir, name string) (manifest, error) {
read, err := wire.Read(os.DirFS(filepath.Join(dir, name)))
if err == nil {
err = validateManifest(read, name)
}
if err := validateManifest(m, path, dir); err != nil {
return manifest{}, err
if err != nil {
return manifest{}, fmt.Errorf("graphwire: %s: %w", filepath.Join(dir, name, "plugin.json"), err)
}
return m, nil
return manifest{Manifest: read}, nil
}

// validateManifest checks the manifest fields the graph wiring relies on.
func validateManifest(m manifest, path, dir string) error {
if !idPattern.MatchString(m.ID) || m.ID != dir {
return fmt.Errorf("graphwire: %s: id %q does not match directory %q", path, m.ID, dir)
// validateManifest checks that m names its directory dir and that a graphql plugin has a backend.
func validateManifest(m wire.Manifest, dir string) error {
if m.ID != dir {
return fmt.Errorf("id %q does not match directory %q", m.ID, dir)
}
if m.GraphQL && m.Backend == "" {
return fmt.Errorf("graphwire: %s: graphql plugins require a backend", path)
return errors.New("graphql plugins require a backend")
}
return nil
}
Expand Down
74 changes: 74 additions & 0 deletions pluginkit/graphwire/graphwire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"slices"
"strings"
"testing"

"github.com/gopherium/framework/pluginkit/wire"
)

var testConfig = Config{
Expand Down Expand Up @@ -694,3 +696,75 @@ func TestGraphQLPluginsRequireABackend(t *testing.T) {
t.Errorf("Run() error = %v, want the missing backend reported", err)
}
}

func TestRunRejectsEveryManifestWireRejects(t *testing.T) {
t.Parallel()

cases := map[string]struct {
dir string
manifest string
want string
}{
"graphql spelled in another case": {"alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "GraphQL": true}`,
`the key "GraphQL" must be spelled "graphql"`},
"graphql repeated": {"alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/alpha", "graphql": false, "graphql": true}`,
`the key "graphql" appears twice`},
"backend repeated": {"alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/one", "backend": "example.com/two", "graphql": true}`,
`the key "backend" appears twice`},
"text that is not UTF-8": {"alpha",
`{"id": "alpha", "name": "Alph` + "\xff" + `", "backend": "example.com/alpha", "graphql": true}`,
"the manifest holds text that is not valid UTF-8"},
"an id out of the id pattern": {"Alpha",
`{"id": "Alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`,
`id "Alpha" must match ^[a-z][a-z0-9-]*$`},
"an id naming another directory": {"alpha",
`{"id": "beta", "name": "Beta", "backend": "example.com/myapp/plugins/beta", "graphql": true}`,
`id "beta" does not match directory "alpha"`},
"no name": {"alpha",
`{"id": "alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`,
"name is required"},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()

root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, tc.dir, tc.manifest, alphaSchema)
if err := os.MkdirAll(filepath.Join(root, "cmd", "myapp"), 0o755); err != nil {
t.Fatalf("creating the output directory: %v", err)
}
path := filepath.Join(root, "plugins", tc.dir, "plugin.json")

err := Run(root, testConfig)

if want := "graphwire: " + path + ": " + tc.want; err == nil || err.Error() != want {
t.Errorf("Run() error = %v, want %q", err, want)
}
_, wireErr := wire.Load(root, []string{"plugins"}, nil)
if want := "pluginwire: " + path + ": " + tc.want; wireErr == nil || wireErr.Error() != want {
t.Errorf("wire.Load() error = %v, want %q", wireErr, want)
}
})
}
}

func TestRunChecksTheManifestBeforeItsDirectory(t *testing.T) {
t.Parallel()

root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha",
`{"id": "beta", "backend": "example.com/myapp/plugins/beta", "graphql": true}`,
alphaSchema)

err := Run(root, testConfig)

want := "graphwire: " + filepath.Join(root, "plugins", "alpha", "plugin.json") + ": name is required"
if err == nil || err.Error() != want {
t.Errorf("Run() error = %v, want %q", err, want)
}
}