From 174ef96ef21fdeddd5e936c74af1d7cd535afb93 Mon Sep 17 00:00:00 2001 From: Chris Hawk Date: Tue, 3 Mar 2026 12:21:22 -0800 Subject: [PATCH 1/5] Use input files rather than runfiles for templates --- build_defs/product.bzl | 8 ++++++-- build_defs/resource.bzl | 5 ++++- mmv1/cmd/main.go | 13 +++++-------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/build_defs/product.bzl b/build_defs/product.bzl index 9cc505995b9f..39b4afb9471e 100644 --- a/build_defs/product.bzl +++ b/build_defs/product.bzl @@ -23,7 +23,8 @@ def _tpg_product_impl(ctx): resources = [res[ResourceInfo] for res in ctx.attr.resources] tpg_resources = [res[TpgResourceInfo] for res in ctx.attr.resources] operations = [res for res in resources if res.has_operation] - inputs = [product.yaml] + [res.metadata for res in tpg_resources] + [res.src for res in tpg_resources] + [res.yaml for res in resources] + inputs = [product.yaml] + [res.metadata for res in tpg_resources] + [res.src for res in tpg_resources] + [res.yaml for res in resources] + [f for f in ctx.files._templates] + outputs = [ ctx.actions.declare_file("{}/product.go".format(product.version)), ] @@ -60,7 +61,7 @@ def _tpg_product_impl(ctx): return [ ctx.attr.product[ProductInfo], - DefaultInfo(files = depset([out for out in outputs])), + DefaultInfo(files = depset([out for out in outputs])) ] tpg_product = rule( @@ -80,5 +81,8 @@ tpg_product = rule( executable = True, cfg = "exec", ), + "_templates": attr.label( + default = Label("//mmv1/templates"), + ), }, ) diff --git a/build_defs/resource.bzl b/build_defs/resource.bzl index 9f09d9cd705b..db8239dfeb41 100644 --- a/build_defs/resource.bzl +++ b/build_defs/resource.bzl @@ -32,7 +32,7 @@ def _tpg_resource_impl(ctx): product = ctx.attr.resource[ProductInfo] resource = ctx.attr.resource[ResourceInfo] resource_name = "resource_{}_{}".format(product.name, resource.name) - inputs = [product.yaml, resource.yaml] + inputs = [product.yaml, resource.yaml] + [f for f in ctx.files._templates] outputs = [ ctx.actions.declare_file("{}/{}.go".format(product.version, resource_name)), ctx.actions.declare_file("{}/{}_generated_meta.yaml".format(product.version, resource_name)), @@ -107,5 +107,8 @@ tpg_resource = rule( executable = True, cfg = "exec", ), + "_templates": attr.label( + default = Label("//mmv1/templates"), + ), }, ) diff --git a/mmv1/cmd/main.go b/mmv1/cmd/main.go index 34332ca6afee..726ba091e9bb 100644 --- a/mmv1/cmd/main.go +++ b/mmv1/cmd/main.go @@ -2,14 +2,14 @@ package main import ( "flag" - "io/fs" "log" + "os" + "path/filepath" "reflect" "time" "github.com/GoogleCloudPlatform/magic-modules/mmv1/api" "github.com/GoogleCloudPlatform/magic-modules/mmv1/provider" - "github.com/bazelbuild/rules_go/go/runfiles" ) var versionFlag = flag.String("version", "", "provider version to generate") @@ -94,14 +94,11 @@ func main() { product.Validate() - r, err := runfiles.New() + wd, err := os.Getwd() if err != nil { - log.Fatalf("could not find runfiles: %v", err) - } - fsys, err := fs.Sub(r, "_main/mmv1") - if err != nil { - log.Fatalf("could not open runfiles subdirectory: %v", err) + log.Fatalf("could not find wd: %v", err) } + fsys := os.DirFS(filepath.Join(wd, "mmv1")) switch *providerFlag { case "tgc", "tgc_cai2hcl", "tgc_next", "oics": From fcae4672138836a0212a8c9d5b66da298599b9c3 Mon Sep 17 00:00:00 2001 From: Chris Hawk Date: Tue, 3 Mar 2026 12:21:47 -0800 Subject: [PATCH 2/5] Run go mod tidy on mmv1/go.mod --- mmv1/go.mod | 1 - mmv1/go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/mmv1/go.mod b/mmv1/go.mod index 6e9e8229959f..df2ea4901f4f 100644 --- a/mmv1/go.mod +++ b/mmv1/go.mod @@ -10,7 +10,6 @@ require ( require github.com/golang/glog v1.2.5 require ( - github.com/bazelbuild/rules_go v0.60.0 github.com/getkin/kin-openapi v0.127.0 github.com/google/go-cmp v0.7.0 github.com/otiai10/copy v1.9.0 diff --git a/mmv1/go.sum b/mmv1/go.sum index ce7f5e2fc406..75d6b0288f12 100644 --- a/mmv1/go.sum +++ b/mmv1/go.sum @@ -1,5 +1,3 @@ -github.com/bazelbuild/rules_go v0.60.0 h1:apGSxTTrFUyLNvX9NQmF4CbntWAO0/S5eALeVgB/6Qk= -github.com/bazelbuild/rules_go v0.60.0/go.mod h1:CYcohJVxs4n7eftbC39GCqaEJm3E1EME+6QAkGguKoI= 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/getkin/kin-openapi v0.127.0 h1:Mghqi3Dhryf3F8vR370nN67pAERW+3a95vomb3MAREY= From 66781cd03c34fa7ea2190d0f03e3da5385bf0472 Mon Sep 17 00:00:00 2001 From: Chris Hawk Date: Tue, 3 Mar 2026 12:22:33 -0800 Subject: [PATCH 3/5] Run bazel mod tidy --- MODULE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MODULE.bazel b/MODULE.bazel index e70519df760b..31b21dadb26c 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -7,4 +7,4 @@ go_sdk.download(version = "1.24.0") go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") go_deps.from_file(go_work = "//:go.work") -use_repo(go_deps, "com_github_getkin_kin_openapi", "com_github_golang_glog", "com_github_googlecloudplatform_declarative_resource_client_library", "com_github_hashicorp_hcl", "com_github_kylelemons_godebug", "com_github_nasa9084_go_openapi", "com_github_otiai10_copy", "in_gopkg_yaml_v2", "in_gopkg_yaml_v3", "org_bitbucket_creachadair_stringset", "org_golang_x_exp") +use_repo(go_deps, "com_github_getkin_kin_openapi", "com_github_golang_glog", "com_github_google_go_cmp", "com_github_googlecloudplatform_declarative_resource_client_library", "com_github_hashicorp_hcl", "com_github_kylelemons_godebug", "com_github_nasa9084_go_openapi", "com_github_otiai10_copy", "in_gopkg_yaml_v2", "in_gopkg_yaml_v3", "org_bitbucket_creachadair_stringset", "org_golang_x_exp") From d028ae9d73ce9a906b46e8d5704781f174c54eaf Mon Sep 17 00:00:00 2001 From: Chris Hawk Date: Tue, 3 Mar 2026 12:29:59 -0800 Subject: [PATCH 4/5] Address presubmit warnings --- build_defs/product.bzl | 9 +++++++-- build_defs/providers.bzl | 4 ++++ build_defs/resource.bzl | 12 +++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/build_defs/product.bzl b/build_defs/product.bzl index 39b4afb9471e..9e6e43da2a4d 100644 --- a/build_defs/product.bzl +++ b/build_defs/product.bzl @@ -1,3 +1,6 @@ +""" +Product-related custom build rules for Magic Modules. +""" load("//build_defs:providers.bzl", "ProductInfo", "ResourceInfo", "TpgResourceInfo") def _mm_product_impl(ctx): @@ -39,7 +42,8 @@ def _tpg_product_impl(ctx): "--output", outputs[0].path ], inputs = depset([i for i in inputs]), - outputs = outputs + outputs = outputs, + mnemonic = "TpgGenerateProduct" ) if operations: operation_go = ctx.actions.declare_file("{}/{}_operation.go".format(product.version, product.name)) @@ -55,7 +59,8 @@ def _tpg_product_impl(ctx): "--output", operation_go.path ], inputs = depset([i for i in inputs]), - outputs = [operation_go] + outputs = [operation_go], + mnemonic = "TpgGenerateProductOperation" ) outputs.append(operation_go) diff --git a/build_defs/providers.bzl b/build_defs/providers.bzl index 18b1808fd7ee..9781541497b6 100644 --- a/build_defs/providers.bzl +++ b/build_defs/providers.bzl @@ -1,3 +1,7 @@ +""" +Provider definitions for Magic Modules custom build rules. +""" + ProductInfo = provider( "Provider for Magic Modules products.", fields = { diff --git a/build_defs/resource.bzl b/build_defs/resource.bzl index db8239dfeb41..8b0166edc031 100644 --- a/build_defs/resource.bzl +++ b/build_defs/resource.bzl @@ -1,3 +1,6 @@ +""" +Resource-related custom build rules for Magic Modules. +""" load("//build_defs:providers.bzl", "ProductInfo", "ResourceInfo", "TpgResourceInfo") def _mm_resource_impl(ctx): @@ -49,7 +52,8 @@ def _tpg_resource_impl(ctx): "--output", outputs[0].path ], inputs = depset([i for i in inputs]), - outputs = [outputs[0]] + outputs = [outputs[0]], + mnemonic = "TpgGenerateResource" ) ctx.actions.run( executable = ctx.executable._compiler, @@ -63,7 +67,8 @@ def _tpg_resource_impl(ctx): "--output", outputs[1].path ], inputs = depset([i for i in inputs]), - outputs = [outputs[1]] + outputs = [outputs[1]], + mnemonic = "TpgGenerateResourceMetadata" ) if resource.has_sweeper: sweeper_go = ctx.actions.declare_file("{}/{}_sweeper.go".format(product.version, resource_name)) @@ -79,7 +84,8 @@ def _tpg_resource_impl(ctx): "--output", sweeper_go.path ], inputs = depset([i for i in inputs]), - outputs = [sweeper_go] + outputs = [sweeper_go], + mnemonic = "TpgGenerateResourceSweeper" ) outputs.append(sweeper_go) return [ From 95c07e6b6b8150cb42f68cf82f48a02c0d014b76 Mon Sep 17 00:00:00 2001 From: Chris Hawk Date: Tue, 3 Mar 2026 12:31:03 -0800 Subject: [PATCH 5/5] Run buildifier to format .bzl files --- build_defs/product.bzl | 156 ++++++++++++++------------- build_defs/providers.bzl | 38 +++---- build_defs/resource.bzl | 222 +++++++++++++++++++++------------------ 3 files changed, 226 insertions(+), 190 deletions(-) diff --git a/build_defs/product.bzl b/build_defs/product.bzl index 9e6e43da2a4d..64fcf98a42c4 100644 --- a/build_defs/product.bzl +++ b/build_defs/product.bzl @@ -1,93 +1,107 @@ """ Product-related custom build rules for Magic Modules. """ + load("//build_defs:providers.bzl", "ProductInfo", "ResourceInfo", "TpgResourceInfo") def _mm_product_impl(ctx): - return [ProductInfo( - name = ctx.label.name, - yaml = ctx.file.src, - version = ctx.attr.version, - )] + return [ProductInfo( + name = ctx.label.name, + yaml = ctx.file.src, + version = ctx.attr.version, + )] mm_product = rule( - implementation = _mm_product_impl, - attrs = { - "version": attr.string(default="ga", mandatory=False), - "src": attr.label( - allow_single_file = [".yaml"], - mandatory = True, - ), - }, + implementation = _mm_product_impl, + attrs = { + "version": attr.string(default = "ga", mandatory = False), + "src": attr.label( + allow_single_file = [".yaml"], + mandatory = True, + ), + }, ) def _tpg_product_impl(ctx): - product = ctx.attr.product[ProductInfo] - resources = [res[ResourceInfo] for res in ctx.attr.resources] - tpg_resources = [res[TpgResourceInfo] for res in ctx.attr.resources] - operations = [res for res in resources if res.has_operation] - inputs = [product.yaml] + [res.metadata for res in tpg_resources] + [res.src for res in tpg_resources] + [res.yaml for res in resources] + [f for f in ctx.files._templates] + product = ctx.attr.product[ProductInfo] + resources = [res[ResourceInfo] for res in ctx.attr.resources] + tpg_resources = [res[TpgResourceInfo] for res in ctx.attr.resources] + operations = [res for res in resources if res.has_operation] + inputs = [product.yaml] + [res.metadata for res in tpg_resources] + [res.src for res in tpg_resources] + [res.yaml for res in resources] + [f for f in ctx.files._templates] - outputs = [ - ctx.actions.declare_file("{}/product.go".format(product.version)), - ] - ctx.actions.run( - executable = ctx.executable._compiler, - arguments = [ - "--product", product.yaml.path, - "--version", product.version, - "--product_name", product.name, - "--type", "product", - "--provider", "tpg", - "--output", outputs[0].path - ], - inputs = depset([i for i in inputs]), - outputs = outputs, - mnemonic = "TpgGenerateProduct" - ) - if operations: - operation_go = ctx.actions.declare_file("{}/{}_operation.go".format(product.version, product.name)) + outputs = [ + ctx.actions.declare_file("{}/product.go".format(product.version)), + ] ctx.actions.run( executable = ctx.executable._compiler, arguments = [ - "--product", product.yaml.path, - "--resource", operations[0].yaml.path, - "--version", product.version, - "--product_name", product.name, - "--type", "operation", - "--provider", "tpg", - "--output", operation_go.path + "--product", + product.yaml.path, + "--version", + product.version, + "--product_name", + product.name, + "--type", + "product", + "--provider", + "tpg", + "--output", + outputs[0].path, ], inputs = depset([i for i in inputs]), - outputs = [operation_go], - mnemonic = "TpgGenerateProductOperation" + outputs = outputs, + mnemonic = "TpgGenerateProduct", ) - outputs.append(operation_go) + if operations: + operation_go = ctx.actions.declare_file("{}/{}_operation.go".format(product.version, product.name)) + ctx.actions.run( + executable = ctx.executable._compiler, + arguments = [ + "--product", + product.yaml.path, + "--resource", + operations[0].yaml.path, + "--version", + product.version, + "--product_name", + product.name, + "--type", + "operation", + "--provider", + "tpg", + "--output", + operation_go.path, + ], + inputs = depset([i for i in inputs]), + outputs = [operation_go], + mnemonic = "TpgGenerateProductOperation", + ) + outputs.append(operation_go) - return [ - ctx.attr.product[ProductInfo], - DefaultInfo(files = depset([out for out in outputs])) - ] + return [ + ctx.attr.product[ProductInfo], + DefaultInfo(files = depset([out for out in outputs])), + ] tpg_product = rule( - implementation = _tpg_product_impl, - attrs = { - "product": attr.label( - providers = [ProductInfo], - mandatory = True, - ), - "resources": attr.label_list( - providers = [ResourceInfo, TpgResourceInfo], - mandatory=True, - ), - "_compiler": attr.label( - default = Label("//mmv1/cmd"), - allow_single_file = True, - executable = True, - cfg = "exec", - ), - "_templates": attr.label( - default = Label("//mmv1/templates"), - ), - }, + implementation = _tpg_product_impl, + attrs = { + "product": attr.label( + providers = [ProductInfo], + mandatory = True, + ), + "resources": attr.label_list( + providers = [ResourceInfo, TpgResourceInfo], + mandatory = True, + ), + "_compiler": attr.label( + default = Label("//mmv1/cmd"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_templates": attr.label( + default = Label("//mmv1/templates"), + ), + }, ) diff --git a/build_defs/providers.bzl b/build_defs/providers.bzl index 9781541497b6..362744bff03f 100644 --- a/build_defs/providers.bzl +++ b/build_defs/providers.bzl @@ -3,29 +3,29 @@ Provider definitions for Magic Modules custom build rules. """ ProductInfo = provider( - "Provider for Magic Modules products.", - fields = { - "name": "name of the product", - "version": "version of the provider to be generated", - "yaml": "the product.yaml file", - }, + "Provider for Magic Modules products.", + fields = { + "name": "name of the product", + "version": "version of the provider to be generated", + "yaml": "the product.yaml file", + }, ) ResourceInfo = provider( - "Provider for Magic Modules resources.", - fields = { - "name": "name of the resource", - "yaml": "the [resource].yaml input file", - "has_sweeper": "whether a sweeper must be generated", - "has_operation": "whether an operation must be generated", - }, + "Provider for Magic Modules resources.", + fields = { + "name": "name of the resource", + "yaml": "the [resource].yaml input file", + "has_sweeper": "whether a sweeper must be generated", + "has_operation": "whether an operation must be generated", + }, ) TpgResourceInfo = provider( - "Provider for TPG resources.", - fields = { - "src": "the generated go file", - "metadata": "the generated metadata.yaml file", - "sweeper": "the optional generated sweeper file", - }, + "Provider for TPG resources.", + fields = { + "src": "the generated go file", + "metadata": "the generated metadata.yaml file", + "sweeper": "the optional generated sweeper file", + }, ) diff --git a/build_defs/resource.bzl b/build_defs/resource.bzl index 8b0166edc031..54395313ee1c 100644 --- a/build_defs/resource.bzl +++ b/build_defs/resource.bzl @@ -1,120 +1,142 @@ """ Resource-related custom build rules for Magic Modules. """ + load("//build_defs:providers.bzl", "ProductInfo", "ResourceInfo", "TpgResourceInfo") def _mm_resource_impl(ctx): - return [ - ctx.attr.product[ProductInfo], - ResourceInfo( - name = ctx.label.name, - yaml = ctx.file.src, - has_sweeper = ctx.attr.has_sweeper, - has_operation = ctx.attr.has_operation, - ), - DefaultInfo(files = depset([ctx.file.src])) - ] + return [ + ctx.attr.product[ProductInfo], + ResourceInfo( + name = ctx.label.name, + yaml = ctx.file.src, + has_sweeper = ctx.attr.has_sweeper, + has_operation = ctx.attr.has_operation, + ), + DefaultInfo(files = depset([ctx.file.src])), + ] mm_resource = rule( - implementation = _mm_resource_impl, - attrs = { - "src": attr.label( - allow_single_file = [".yaml"], - mandatory = True, - ), - "product": attr.label( - providers = [ProductInfo], - mandatory=True, - ), - "has_sweeper": attr.bool(default=False, mandatory=False), - "has_operation": attr.bool(default=False, mandatory=False), - }, + implementation = _mm_resource_impl, + attrs = { + "src": attr.label( + allow_single_file = [".yaml"], + mandatory = True, + ), + "product": attr.label( + providers = [ProductInfo], + mandatory = True, + ), + "has_sweeper": attr.bool(default = False, mandatory = False), + "has_operation": attr.bool(default = False, mandatory = False), + }, ) def _tpg_resource_impl(ctx): - product = ctx.attr.resource[ProductInfo] - resource = ctx.attr.resource[ResourceInfo] - resource_name = "resource_{}_{}".format(product.name, resource.name) - inputs = [product.yaml, resource.yaml] + [f for f in ctx.files._templates] - outputs = [ - ctx.actions.declare_file("{}/{}.go".format(product.version, resource_name)), - ctx.actions.declare_file("{}/{}_generated_meta.yaml".format(product.version, resource_name)), - ] - ctx.actions.run( - executable = ctx.executable._compiler, - arguments = [ - "--product", product.yaml.path, - "--resource", resource.yaml.path, - "--version", product.version, - "--product_name", product.name, - "--type", "resource", - "--provider", "tpg", - "--output", outputs[0].path - ], - inputs = depset([i for i in inputs]), - outputs = [outputs[0]], - mnemonic = "TpgGenerateResource" - ) - ctx.actions.run( - executable = ctx.executable._compiler, - arguments = [ - "--product", product.yaml.path, - "--resource", resource.yaml.path, - "--version", product.version, - "--product_name", product.name, - "--type", "metadata", - "--provider", "tpg", - "--output", outputs[1].path - ], - inputs = depset([i for i in inputs]), - outputs = [outputs[1]], - mnemonic = "TpgGenerateResourceMetadata" - ) - if resource.has_sweeper: - sweeper_go = ctx.actions.declare_file("{}/{}_sweeper.go".format(product.version, resource_name)) + product = ctx.attr.resource[ProductInfo] + resource = ctx.attr.resource[ResourceInfo] + resource_name = "resource_{}_{}".format(product.name, resource.name) + inputs = [product.yaml, resource.yaml] + [f for f in ctx.files._templates] + outputs = [ + ctx.actions.declare_file("{}/{}.go".format(product.version, resource_name)), + ctx.actions.declare_file("{}/{}_generated_meta.yaml".format(product.version, resource_name)), + ] + ctx.actions.run( + executable = ctx.executable._compiler, + arguments = [ + "--product", + product.yaml.path, + "--resource", + resource.yaml.path, + "--version", + product.version, + "--product_name", + product.name, + "--type", + "resource", + "--provider", + "tpg", + "--output", + outputs[0].path, + ], + inputs = depset([i for i in inputs]), + outputs = [outputs[0]], + mnemonic = "TpgGenerateResource", + ) ctx.actions.run( executable = ctx.executable._compiler, arguments = [ - "--product", product.yaml.path, - "--resource", resource.yaml.path, - "--version", product.version, - "--product_name", product.name, - "--type", "sweeper", - "--provider", "tpg", - "--output", sweeper_go.path + "--product", + product.yaml.path, + "--resource", + resource.yaml.path, + "--version", + product.version, + "--product_name", + product.name, + "--type", + "metadata", + "--provider", + "tpg", + "--output", + outputs[1].path, ], inputs = depset([i for i in inputs]), - outputs = [sweeper_go], - mnemonic = "TpgGenerateResourceSweeper" + outputs = [outputs[1]], + mnemonic = "TpgGenerateResourceMetadata", ) - outputs.append(sweeper_go) - return [ - ctx.attr.resource[ProductInfo], - ctx.attr.resource[ResourceInfo], - TpgResourceInfo( - src = outputs[0], - metadata = outputs[1], - ), - DefaultInfo(files = depset([out for out in outputs])), - ] + if resource.has_sweeper: + sweeper_go = ctx.actions.declare_file("{}/{}_sweeper.go".format(product.version, resource_name)) + ctx.actions.run( + executable = ctx.executable._compiler, + arguments = [ + "--product", + product.yaml.path, + "--resource", + resource.yaml.path, + "--version", + product.version, + "--product_name", + product.name, + "--type", + "sweeper", + "--provider", + "tpg", + "--output", + sweeper_go.path, + ], + inputs = depset([i for i in inputs]), + outputs = [sweeper_go], + mnemonic = "TpgGenerateResourceSweeper", + ) + outputs.append(sweeper_go) + return [ + ctx.attr.resource[ProductInfo], + ctx.attr.resource[ResourceInfo], + TpgResourceInfo( + src = outputs[0], + metadata = outputs[1], + ), + DefaultInfo(files = depset([out for out in outputs])), + ] tpg_resource = rule( - implementation = _tpg_resource_impl, - attrs = { - "resource": attr.label( - providers = [ResourceInfo, ProductInfo], - allow_single_file = [".yaml"], - mandatory = True, - ), - "version": attr.string(default="ga", mandatory=False), - "_compiler": attr.label( - default = Label("//mmv1/cmd"), - allow_single_file = True, - executable = True, - cfg = "exec", - ), - "_templates": attr.label( - default = Label("//mmv1/templates"), - ), - }, + implementation = _tpg_resource_impl, + attrs = { + "resource": attr.label( + providers = [ResourceInfo, ProductInfo], + allow_single_file = [".yaml"], + mandatory = True, + ), + "version": attr.string(default = "ga", mandatory = False), + "_compiler": attr.label( + default = Label("//mmv1/cmd"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_templates": attr.label( + default = Label("//mmv1/templates"), + ), + }, )