Skip to content

Commit d33b19b

Browse files
authored
refactor: validate orion axl types everywhere (#113)
### Changes are visible to end-users: no ### Test plan - Covered by existing test cases - New test cases added
1 parent 75f1392 commit d33b19b

File tree

19 files changed

+301
-158
lines changed

19 files changed

+301
-158
lines changed

language/orion/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ bazel_dep(name = "gazelle", version = "0.45.0")
1010
# Go modules
1111
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
1212
go_deps.from_file(go_mod = "//:go.mod")
13-
use_repo(go_deps, "com_github_bmatcuk_doublestar_v4", "com_github_emirpasic_gods", "com_github_itchyny_gojq", "com_github_mikefarah_yq_v4", "net_starlark_go", "org_golang_x_sync")
13+
use_repo(go_deps, "com_github_bazelbuild_buildtools", "com_github_bmatcuk_doublestar_v4", "com_github_emirpasic_gods", "com_github_itchyny_gojq", "com_github_mikefarah_yq_v4", "net_starlark_go", "org_golang_x_sync")
1414

1515
####### Dev dependencies ########
1616

language/orion/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ replace github.com/aspect-build/aspect-gazelle/common => ../../common
66

77
require (
88
github.com/bazelbuild/bazel-gazelle v0.45.0 // NOTE: keep in sync with MODULE.bazel
9-
github.com/bazelbuild/buildtools v0.0.0-20250930140053-2eb4fccefb52 // indirect
9+
github.com/bazelbuild/buildtools v0.0.0-20250930140053-2eb4fccefb52
1010
github.com/bmatcuk/doublestar/v4 v4.9.1
1111
github.com/emirpasic/gods v1.18.1
1212
github.com/fatih/color v1.18.0 // indirect

language/orion/host.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ func (h *GazelleHost) LoadPlugin(pluginDir, pluginPath string) {
163163

164164
err := starzelle.LoadProxy(h, pluginDir, pluginPath)
165165
if err != nil {
166-
BazelLog.Infof("Failed to load orion plugin %q/%q: %v\n", pluginDir, pluginPath, err)
166+
BazelLog.Infof("Failed to load orion plugin %v\n", err)
167167

168168
// Try to remove the `parentDir` from the error message to align paths
169169
// with the user's workspace relative paths, and to remove sandbox paths
170170
// when run in tests.
171171
errStr := strings.ReplaceAll(err.Error(), pluginDir+"/", "")
172172

173-
fmt.Printf("Failed to load orion plugin %q: %v\n", pluginPath, errStr)
173+
fmt.Printf("Failed to load orion plugin %v\n", errStr)
174174
return
175175
}
176176
}

language/orion/plugin/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go_library(
44
name = "plugin",
55
srcs = [
66
"database.go",
7+
"plugin.bzl.go",
78
"plugin.go",
89
"plugin.star.go",
910
"queries.go",
@@ -15,7 +16,9 @@ go_library(
1516
visibility = ["//visibility:public"],
1617
deps = [
1718
"//starlark/utils",
19+
"@com_github_bazelbuild_buildtools//build",
1820
"@com_github_bmatcuk_doublestar_v4//:doublestar",
21+
"@gazelle//rule",
1922
"@net_starlark_go//starlark",
2023
],
2124
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package plugin
2+
3+
import (
4+
"github.com/bazelbuild/bazel-gazelle/rule"
5+
bzl "github.com/bazelbuild/buildtools/build"
6+
)
7+
8+
// ---------------- TargetSource
9+
10+
var _ rule.BzlExprValue = (*TargetSource)(nil)
11+
12+
func (ts TargetSource) BzlExpr() bzl.Expr {
13+
return &bzl.StringExpr{Value: ts.Path}
14+
}

language/orion/plugin/plugin.star.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,18 @@ func addTarget(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tupl
251251

252252
var attrs map[string]interface{}
253253
if starAttrs != nil {
254-
attrs = starUtils.ReadMap2(starAttrs, readTargetAttributeValue)
254+
attrs, err = starUtils.ReadMap2(starAttrs, readTargetAttributeValue)
255+
if err != nil {
256+
return nil, err
257+
}
255258
}
256259

257260
var symbols []Symbol
258261
if starSymbols != nil {
259-
symbols = starUtils.ReadList(starSymbols, readSymbol)
262+
symbols, err = starUtils.ReadList(starSymbols, readSymbol)
263+
if err != nil {
264+
return nil, err
265+
}
260266
}
261267

262268
ai := fn.Receiver().(*declareTargetActionsImpl)

language/orion/plugin/target.star.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,23 @@ func (te TargetSymbol) AttrNames() []string {
103103

104104
// ---------------- utils
105105

106-
func readTargetImport(v starlark.Value) TargetImport {
107-
return v.(TargetImport)
108-
}
109-
110-
func readSymbol(v starlark.Value) Symbol {
111-
return v.(Symbol)
112-
}
113-
114-
func readLabel(v starlark.Value) Label {
115-
return v.(Label)
106+
func readSymbol(v starlark.Value) (Symbol, error) {
107+
s, ok := v.(Symbol)
108+
if !ok {
109+
return Symbol{}, fmt.Errorf("expected Symbol, got %T", v)
110+
}
111+
return s, nil
116112
}
117113

118-
func readTargetAttributeValue(v starlark.Value) interface{} {
114+
func readTargetAttributeValue(v starlark.Value) (interface{}, error) {
115+
// Types that are both starlark.Value and plugin.*
119116
switch v := v.(type) {
120117
case TargetImport:
121-
return readTargetImport(v)
118+
return v, nil
122119
case Label:
123-
return readLabel(v)
120+
return v, nil
124121
case TargetSource:
125-
return v.Path
122+
return v, nil
126123
}
127124

128125
return starUtils.ReadRecurse(v, readTargetAttributeValue)

language/orion/starlark/stdlib/path.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package starlark
22

33
import (
4+
"fmt"
45
"path"
56

67
utils "github.com/aspect-build/aspect-gazelle/language/orion/starlark/utils"
@@ -36,7 +37,11 @@ func path_ext(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwar
3637
}
3738

3839
func path_join(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
39-
return starlark.String(path.Join(utils.ReadStringTuple(args)...)), nil
40+
parts, err := utils.ReadStringTuple(args)
41+
if err != nil {
42+
return nil, fmt.Errorf("path.join: %w", err)
43+
}
44+
return starlark.String(path.Join(parts...)), nil
4045
}
4146

4247
var Path = utils.CreateModule("path", map[string]utils.ModuleFunction{

0 commit comments

Comments
 (0)