Capture and validate invocation features

This commit is contained in:
2026-11-22 20:52:04 +00:00
parent 68515668de
commit ea3ce25001
8 changed files with 276 additions and 2 deletions

View File

@@ -28,6 +28,8 @@ type installationCandidate struct {
aliases *internalalias.Resolver
flagAliases map[string]resolvedFlagAliases
discoveryFlags map[string]resolvedFlagAliases
features []*invocationFeatureRecord
featureFlags []commandmeta.Flag
changed bool
}
@@ -91,6 +93,8 @@ func (a *App) installBatch(declarations ...Declaration) error {
a.aliases = candidate.aliases
a.flagAliases = candidate.flagAliases
a.discoveryFlags = candidate.discoveryFlags
a.features = candidate.features
a.featureFlags = candidate.featureFlags
a.snapshot = nil
return nil
}
@@ -154,11 +158,18 @@ func (a *App) buildInstallationCandidate(commands []Command, addedRules []Routin
}
candidate.aliases = resolver
candidate.features, candidate.featureFlags, err = collectInvocationFeatures(candidate.commands)
if err != nil {
return nil, err
}
tree := buildCanonicalTree(candidate.commands)
if err := validateInvocationFlagCollisions(tree, candidate.featureFlags); err != nil {
return nil, err
}
if err := validateCommandAliases(rules, resolver, tree); err != nil {
return nil, err
}
flagAliases, discoveryFlags, err := compileFlagAliases(rules, tree, resolver)
flagAliases, discoveryFlags, err := compileFlagAliases(rules, tree, resolver, candidate.featureFlags)
if err != nil {
return nil, err
}
@@ -365,3 +376,67 @@ func acceptsOperandPrefix(command Command, count int) bool {
}
return maximum < 0 || count <= maximum
}
// collectInvocationFeatures walks declarations in stable root, child and attachment order.
func collectInvocationFeatures(commands map[string]*commandNode) ([]*invocationFeatureRecord, []commandmeta.Flag, error) {
names := make([]string, 0, len(commands))
for name := range commands {
names = append(names, name)
}
sort.Strings(names)
seen := make(map[*invocationFeatureRecord]bool)
var features []*invocationFeatureRecord
var flags []commandmeta.Flag
var visit func(Command)
visit = func(command Command) {
if command.record == nil || command.record.absent {
return
}
for _, feature := range command.record.features {
if !seen[feature.record] {
seen[feature.record] = true
features = append(features, feature.record)
flags = append(flags, feature.record.payload.bridgeFlags()...)
}
}
for _, child := range command.record.children {
visit(child)
}
}
for _, name := range names {
visit(commands[name].command)
}
seenFlags := helpFlagSpellings()
for _, flag := range flags {
for _, spelling := range featureSpellings(flag) {
key := flagSpellingKey(spelling)
if _, exists := seenFlags[key]; exists {
return nil, nil, fmt.Errorf("invocation feature flag %s conflicts with another flag", spelling.token())
}
seenFlags[key] = spelling
}
}
return features, flags, nil
}
func featureSpellings(flag commandmeta.Flag) []capturedFlagSpelling {
spellings := []capturedFlagSpelling{{name: flag.Name}}
if flag.Short != "" {
spellings = append(spellings, capturedFlagSpelling{name: flag.Short, short: true})
}
return spellings
}
func validateInvocationFlagCollisions(roots map[string]*canonicalNode, flags []commandmeta.Flag) error {
for _, node := range indexCanonicalNodes(roots) {
local := canonicalFlagSpellings(node.command)
for _, flag := range flags {
for _, spelling := range featureSpellings(flag) {
if _, exists := local[flagSpellingKey(spelling)]; exists {
return fmt.Errorf("invocation feature flag %s conflicts with command %q", spelling.token(), node.path.String())
}
}
}
}
return nil
}