Files
skillshare/internal/opencode/package.go
T
Rusty ShacklefordandWillie 43059a1428 fix(opencode): preserve v2 hook and plugin directory packages (#915)
* fix(opencode): preserve v2 directory packages

Stage the reviewed issue 913 source for isolated fork validation. Preserve
package directories and manifests without executing managed packages or
installing their dependencies, and include receipt and localized UI fixes.

Reviewed local source: 15ac88e2f266571b8a6dc8d777302db0f4dcae44
Exact source tree: 45ebabfe2b

Static checks and independent inspection completed. Go, integration,
dashboard, rendering, and hosted runtime validation remain pending.
This fork staging commit is not an upstream publication or merge.

* test(opencode): align schema and snapshot assertions

Check the version-aware binding schema and verify native unregister retains the unchanged managed snapshot.

* fix(opencode): resolve Windows npm native launchers

Probe recognized native Opencode executables without invoking a batch shell; exercise the default version probe on hosted Windows.

* test(opencode): normalize Windows fixture line endings

Avoid doubling carriage returns when Git checks out the npm fixture with CRLF.

* fix(opencode): keep an undetected OpenCode version from blocking other agents

Review of the v2 directory package change found that one failed
`opencode --version` probe failed the whole hooks plan, so a machine
without the binary (a second machine sharing the config, CI, a
devcontainer) could no longer sync hooks to any agent, and doctor and
the dashboard preview failed with it.

Hooks:
- A probe that fails or reports an unknown major now skips only the
  OpenCode bindings with one warning per target. Outputs already owned
  there are parked, so they are neither rewritten nor removed while the
  layout is unknown. The probe runs once per plan, list and import.
- A `package` binding on OpenCode v1 is skipped with a warning instead
  of failing the plan.
- The v2 dependency warning is reported once, and only when a package
  file is written, so a synced hook no longer keeps doctor yellow.
- `hooks import` reports an unreadable file such as a symlink as that
  package's problem; other packages and flat files stay importable.
  The package validation problem is reported once instead of twice.
- Directories under `plugins/` are listed as unmanaged only on v2.

Plugins:
- The inventory leaves `plugins/skillshare-NAME` registrations to
  hooks, so a synced v2 hook is not offered as an importable plugin.

Dashboard: the new warnings are translated in every locale, a
`hook NAME:` prefix no longer defeats translation, the native preview
translates its per-file error, and the YAML editor documents `package`.

Docs: the English pages and the built-in skill describe the behavior
above; the ja, ko, zh-Hans and zh-Hant pages now carry the same
content as the English ones. The runbook drives the CLI with mdproof
assertions instead of prose expectations.

Refs #913.

---------

Co-authored-by: Willie <egg358358@gmail.com>
2026-11-16 01:50:14 +08:00

77 lines
2.5 KiB
Go

// Package opencode describes native loader inputs without loading plugin code.
package opencode
import (
"encoding/json"
"fmt"
"net/url"
"path"
"path/filepath"
"strings"
)
// ConfigKey recognizes the installed major rather than guessing a future API.
func ConfigKey(version string) (string, error) {
fields := strings.Fields(version)
if len(fields) > 0 {
v := strings.TrimPrefix(fields[len(fields)-1], "v")
if strings.HasPrefix(v, "1.") {
return "plugin", nil
}
if strings.HasPrefix(v, "2.") {
return "plugins", nil
}
}
return "", fmt.Errorf("unrecognized OpenCode version; supports versions 1.x and 2.x")
}
// FileURL keeps the native file URL spelling used by existing receipts.
func FileURL(p string) string {
return (&url.URL{Scheme: "file", Path: filepath.ToSlash(p)}).String()
}
// Package validates the server entry selected by OpenCode v2. It leaves exports,
// dependencies intact. Conditional server exports fail closed; read never executes code.
func Package(read func(string) ([]byte, error)) error {
var m struct {
Name string `json:"name"`
Exports any `json:"exports"`
}
data, err := read("package.json")
if err != nil {
return fmt.Errorf("OpenCode package.json: %w", err)
}
if err = json.Unmarshal(data, &m); err != nil {
return fmt.Errorf("OpenCode package.json: %w", err)
}
if m.Name != "" && m.Exports != nil {
exports := m.Exports
if obj, ok := exports.(map[string]any); ok {
if server, has := obj["./server"]; has {
exports = server
} else if root, has := obj["."]; has {
exports = root
}
}
entry, ok := exports.(string)
if !ok {
return fmt.Errorf("OpenCode server exports must be a string (. or ./server); conditional and fallback exports cannot be verified offline")
}
if !strings.HasPrefix(entry, "./") || strings.ContainsAny(entry, "\\:") || !filepath.IsLocal(filepath.FromSlash(strings.TrimPrefix(entry, "./"))) {
return fmt.Errorf("OpenCode export %q must stay inside the package", entry)
}
if _, err := read(path.Clean(strings.TrimPrefix(entry, "./"))); err != nil {
return fmt.Errorf("OpenCode export %q is missing: %w", entry, err)
}
return nil
}
for _, base := range []string{"server", "index"} {
for _, ext := range []string{".ts", ".tsx", ".js"} {
if _, err := read(base + ext); err == nil {
return nil
}
}
}
return fmt.Errorf("OpenCode v2 package requires name and exports, or a root server/index .ts, .tsx or .js file; --entry cannot override the directory loader")
}