fix(formula): resolve formula file paths in mol pour, wisp, bond and seed
bd cook resolves its argument as a formula name from the search paths
and, failing that, as a path to a formula file. The resolver behind
bd mol pour, wisp, bond and seed (resolveAndCookFormulaWithVars) only
ever tried the name, so a formula file outside every search path cooked
but would not pour: `bd mol pour ./x.formula.toml` failed with "not
found as formula or proto ID" while `bd cook ./x.formula.toml` worked.
The two near-identical resolvers drifted when 44853d169 taught only the
cook one about registry names; nothing records an intent to keep file
paths cook-only.
Both resolvers, and the two places where bd mol bond describes an
operand before cooking it (discoverMolBondOperand and resolveOrDescribe,
which call the parser directly and refuse the bond before the shared
resolver runs), now go through one loadFormulaByNameOrPath: registry
name first, file path second. Name lookup keeps precedence, so no
existing name resolves differently.
When both lookups miss, the error names what the argument was written
as. A path reports the file error, so an unreadable or syntactically
broken formula file is named as such instead of as a registry miss. A
bare name reports the registry miss, so pour, wisp and bond keep falling
through to their proto or issue lookup with the same message as before.
Tests: TestLoadFormulaByNameOrPath_{PathFallback,RegistryWins,
ErrorNamesWhatWasTried}, TestResolveAndCookFormulaWithVars_AcceptsPath,
TestMolBondResolversAcceptFormulaPath, all hermetic under t.TempDir().
Refs #3874, #5761.
Agent-Signature: claude-code-claude-fable-5-1-unknown-reasoning on behalf of Rand
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
bc1cd64c78
commit
0eebf207d1
4 changed files with 232 additions and 11 deletions
|
|
@ -814,6 +814,7 @@ go_test(
|
|||
"format_json_alias_embedded_test.go",
|
||||
"formula_fixture_proxied_test.go",
|
||||
"formula_help_test.go",
|
||||
"formula_path_fallback_test.go",
|
||||
"formula_primitives_test.go",
|
||||
"formula_schema_test.go",
|
||||
"gate_add_waiter_contention_test.go",
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -150,20 +151,58 @@ func parseCookFlags(cmd *cobra.Command, args []string) (*cookFlags, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
// loadFormulaByNameOrPath resolves a formula argument as a registry name
|
||||
// first (the parser's search paths: .beads/formulas/ and friends) and as a
|
||||
// formula file path second. bd cook has resolved its argument this way since
|
||||
// it learned registry names, but the resolver behind bd mol pour, wisp, bond
|
||||
// and seed stayed name-only, so `bd mol pour ./x.formula.toml` failed while
|
||||
// `bd cook ./x.formula.toml` worked. Every command now shares this one lookup.
|
||||
//
|
||||
// When both lookups miss, the error that reaches the caller is the one the
|
||||
// argument was most plausibly meant to hit. An argument written as a path
|
||||
// reports the file error, so an unreadable or syntactically broken formula
|
||||
// file is named as such instead of as a registry miss. A bare name reports
|
||||
// the registry miss, so callers that fall through to a proto or issue lookup
|
||||
// (pour, wisp, bond) keep their "not found" message.
|
||||
func loadFormulaByNameOrPath(parser *formula.Parser, nameOrPath string) (*formula.Formula, error) {
|
||||
f, nameErr := parser.LoadByName(nameOrPath)
|
||||
if nameErr == nil {
|
||||
return f, nil
|
||||
}
|
||||
f, pathErr := parser.ParseFile(nameOrPath)
|
||||
if pathErr == nil {
|
||||
return f, nil
|
||||
}
|
||||
if looksLikeFormulaPath(nameOrPath) {
|
||||
return nil, pathErr
|
||||
}
|
||||
return nil, nameErr
|
||||
}
|
||||
|
||||
// looksLikeFormulaPath reports whether a formula argument was written as a
|
||||
// file path: it has a directory component, carries a formula file extension,
|
||||
// or names a file that exists.
|
||||
func looksLikeFormulaPath(arg string) bool {
|
||||
if strings.ContainsAny(arg, `/\`) {
|
||||
return true
|
||||
}
|
||||
if strings.HasSuffix(arg, formula.FormulaExtTOML) || strings.HasSuffix(arg, formula.FormulaExtJSON) {
|
||||
return true
|
||||
}
|
||||
_, err := os.Stat(arg)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// loadAndResolveFormula parses a formula file and applies all transformations.
|
||||
// It first tries to load by name from the formula registry (.beads/formulas/),
|
||||
// and falls back to parsing as a file path if that fails.
|
||||
func loadAndResolveFormula(formulaPath string, searchPaths []string) (*formula.Formula, error) {
|
||||
parser := formula.NewParser(searchPaths...)
|
||||
|
||||
// Try to load by name first (from .beads/formulas/ registry)
|
||||
f, err := parser.LoadByName(formulaPath)
|
||||
// Registry name first, file path second.
|
||||
f, err := loadFormulaByNameOrPath(parser, formulaPath)
|
||||
if err != nil {
|
||||
// Fall back to parsing as a file path
|
||||
f, err = parser.ParseFile(formulaPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing formula: %w", err)
|
||||
}
|
||||
return nil, fmt.Errorf("parsing formula: %w", err)
|
||||
}
|
||||
|
||||
// Resolve inheritance
|
||||
|
|
@ -705,8 +744,10 @@ func resolveAndCookFormulaWithVars(formulaName string, searchPaths []string, con
|
|||
// Create parser with search paths
|
||||
parser := formula.NewParser(searchPaths...)
|
||||
|
||||
// Load formula by name
|
||||
f, err := parser.LoadByName(formulaName)
|
||||
// Registry name first, file path second: the same resolution contract as
|
||||
// bd cook, so a formula file outside every search path pours, wisps,
|
||||
// bonds and seeds the way it cooks.
|
||||
f, err := loadFormulaByNameOrPath(parser, formulaName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading formula %q: %w", formulaName, err)
|
||||
}
|
||||
|
|
|
|||
179
cmd/bd/formula_path_fallback_test.go
Normal file
179
cmd/bd/formula_path_fallback_test.go
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
package main
|
||||
|
||||
// bd cook resolves its argument as a formula name from the search paths and,
|
||||
// failing that, as a formula file path. The resolver behind bd mol pour, wisp,
|
||||
// bond and seed accepted only a name, so a formula file outside every search
|
||||
// path cooked but would not pour. Both now go through loadFormulaByNameOrPath.
|
||||
// These tests pin the shared lookup, its name-before-path precedence, which
|
||||
// error survives a double miss, and the pour/bond entry points that depend on
|
||||
// it. Every fixture lives under t.TempDir() and every parser gets explicit
|
||||
// search paths, so the repository's own .beads/formulas never takes part.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/formula"
|
||||
)
|
||||
|
||||
const pathFallbackFormulaTOML = `formula = "path-fallback-resolver-test"
|
||||
version = 1
|
||||
type = "workflow"
|
||||
|
||||
[[steps]]
|
||||
id = "only-step"
|
||||
title = "Step reachable by path only"
|
||||
`
|
||||
|
||||
// writePathFallbackFormula writes the fixture in a directory that is not a
|
||||
// formula search path, so only the file-path lookup can find it.
|
||||
func writePathFallbackFormula(t *testing.T) string {
|
||||
t.Helper()
|
||||
p := filepath.Join(t.TempDir(), "sub", "path-fallback-resolver-test.formula.toml")
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(p, []byte(pathFallbackFormulaTOML), 0o600); err != nil {
|
||||
t.Fatalf("write formula fixture: %v", err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// emptySearchPath returns a directory with no formulas in it, so a parser
|
||||
// given it can never satisfy a name lookup.
|
||||
func emptySearchPath(t *testing.T) string {
|
||||
t.Helper()
|
||||
return t.TempDir()
|
||||
}
|
||||
|
||||
func TestLoadFormulaByNameOrPath_PathFallback(t *testing.T) {
|
||||
path := writePathFallbackFormula(t)
|
||||
parser := formula.NewParser(emptySearchPath(t))
|
||||
|
||||
// The name is still unknown to the registry: the path lookup adds a way
|
||||
// in, it does not widen what a name resolves to.
|
||||
if _, err := loadFormulaByNameOrPath(parser, "path-fallback-resolver-test"); err == nil {
|
||||
t.Fatal("expected name lookup to fail with an empty search path")
|
||||
}
|
||||
|
||||
f, err := loadFormulaByNameOrPath(parser, path)
|
||||
if err != nil {
|
||||
t.Fatalf("path lookup failed: %v", err)
|
||||
}
|
||||
if f.Formula != "path-fallback-resolver-test" {
|
||||
t.Errorf("formula name = %q, want path-fallback-resolver-test", f.Formula)
|
||||
}
|
||||
if f.Source != path {
|
||||
t.Errorf("formula Source = %q, want %q", f.Source, path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFormulaByNameOrPath_RegistryWins(t *testing.T) {
|
||||
// The same formula name in the registry and in a stray file in the
|
||||
// working directory: the registry copy wins, because the name lookup
|
||||
// runs first.
|
||||
registryDir := filepath.Join(t.TempDir(), ".beads", "formulas")
|
||||
if err := os.MkdirAll(registryDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
registryTOML := strings.Replace(pathFallbackFormulaTOML,
|
||||
`title = "Step reachable by path only"`,
|
||||
`title = "Registry copy wins"`, 1)
|
||||
if err := os.WriteFile(filepath.Join(registryDir, "path-fallback-resolver-test.formula.toml"),
|
||||
[]byte(registryTOML), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
parser := formula.NewParser(registryDir)
|
||||
f, err := loadFormulaByNameOrPath(parser, "path-fallback-resolver-test")
|
||||
if err != nil {
|
||||
t.Fatalf("registry lookup failed: %v", err)
|
||||
}
|
||||
if len(f.Steps) != 1 || !strings.Contains(f.Steps[0].Title, "Registry copy wins") {
|
||||
t.Errorf("expected the registry copy to win; got steps %+v", f.Steps)
|
||||
}
|
||||
}
|
||||
|
||||
// When both lookups miss, the error names what the argument was written as:
|
||||
// a bare name reports the registry miss (pour, wisp and bond fall through to
|
||||
// a proto or issue lookup on that and keep their "not found" message), a path
|
||||
// reports the file error (a typo in a path, or a broken formula file, is
|
||||
// named as such instead of as a registry miss).
|
||||
func TestLoadFormulaByNameOrPath_ErrorNamesWhatWasTried(t *testing.T) {
|
||||
parser := formula.NewParser(emptySearchPath(t))
|
||||
|
||||
_, err := loadFormulaByNameOrPath(parser, "no-such-formula")
|
||||
if err == nil || !strings.Contains(err.Error(), "not found in search paths") {
|
||||
t.Errorf("bare name: want the registry miss, got %v", err)
|
||||
}
|
||||
|
||||
missing := filepath.Join(t.TempDir(), "missing.formula.toml")
|
||||
_, err = loadFormulaByNameOrPath(parser, missing)
|
||||
if err == nil || strings.Contains(err.Error(), "not found in search paths") {
|
||||
t.Errorf("missing path: want the file error, got %v", err)
|
||||
}
|
||||
|
||||
broken := filepath.Join(t.TempDir(), "broken.formula.toml")
|
||||
if err := os.WriteFile(broken, []byte("formula = \"broken\"\n[[steps]\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = loadFormulaByNameOrPath(parser, broken)
|
||||
if err == nil || !strings.Contains(err.Error(), "parse ") {
|
||||
t.Errorf("broken file: want the parse error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveAndCookFormulaWithVars_AcceptsPath is the contract pour, wisp,
|
||||
// seed and bond's materialization rely on: a formula file outside every
|
||||
// search path cooks, and an argument that is neither a name nor a file still
|
||||
// fails, so pour's proto-ID fallback stays reachable.
|
||||
func TestResolveAndCookFormulaWithVars_AcceptsPath(t *testing.T) {
|
||||
path := writePathFallbackFormula(t)
|
||||
searchPaths := []string{emptySearchPath(t)}
|
||||
|
||||
sg, err := resolveAndCookFormulaWithVars(path, searchPaths, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("resolveAndCookFormulaWithVars(%q) failed: %v", path, err)
|
||||
}
|
||||
if sg == nil || sg.Root == nil {
|
||||
t.Fatal("expected a cooked subgraph")
|
||||
}
|
||||
if sg.Root.ID != "path-fallback-resolver-test" {
|
||||
t.Errorf("root ID = %q, want the formula name", sg.Root.ID)
|
||||
}
|
||||
|
||||
missing := filepath.Join(t.TempDir(), "missing.formula.toml")
|
||||
if _, err := resolveAndCookFormulaWithVars(missing, searchPaths, nil); err == nil {
|
||||
t.Fatal("expected failure for a nonexistent path")
|
||||
}
|
||||
}
|
||||
|
||||
// bd mol bond resolves its operands twice: once to describe them (the dry-run
|
||||
// and the pre-materialization discovery) and once to cook them. Both passes
|
||||
// must take a formula file path, or the bond is refused before the cook runs.
|
||||
func TestMolBondResolversAcceptFormulaPath(t *testing.T) {
|
||||
isolateBeadsDirForTest(t)
|
||||
t.Setenv("GT_ROOT", emptySearchPath(t))
|
||||
path := writePathFallbackFormula(t)
|
||||
ctx := context.Background()
|
||||
reader := &noIssuesMolReader{}
|
||||
|
||||
issue, name, err := resolveOrDescribe(ctx, reader, path, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("resolveOrDescribe(%q): %v", path, err)
|
||||
}
|
||||
if issue != nil || name != "path-fallback-resolver-test" {
|
||||
t.Errorf("resolveOrDescribe = (%v, %q), want (nil, path-fallback-resolver-test)", issue, name)
|
||||
}
|
||||
|
||||
sg, cooked, err := resolveOrCookToSubgraph(ctx, reader, path, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("resolveOrCookToSubgraph(%q): %v", path, err)
|
||||
}
|
||||
if !cooked || sg == nil || sg.Root == nil || sg.Root.ID != "path-fallback-resolver-test" {
|
||||
t.Errorf("resolveOrCookToSubgraph: cooked=%v subgraph=%+v, want a cooked subgraph rooted at the formula", cooked, sg)
|
||||
}
|
||||
}
|
||||
|
|
@ -815,7 +815,7 @@ func discoverMolBondOperand(ctx context.Context, localStore storage.DoltStorage,
|
|||
}, nil
|
||||
}
|
||||
parser := formula.NewParser()
|
||||
f, loadErr := parser.LoadByName(operand)
|
||||
f, loadErr := loadFormulaByNameOrPath(parser, operand)
|
||||
if loadErr == nil {
|
||||
// Discovery must fail the same way materialization would, and
|
||||
// materialization (resolveAndCookFormulaWithVars) resolves inheritance
|
||||
|
|
@ -942,7 +942,7 @@ func resolveOrDescribe(ctx context.Context, s molReader, operand string, vars ma
|
|||
}
|
||||
|
||||
parser := formula.NewParser()
|
||||
f, err := parser.LoadByName(operand)
|
||||
f, err := loadFormulaByNameOrPath(parser, operand)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("'%s' not found as issue or formula: %w", operand, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue