diff --git a/cmd/controller-gen/main.go b/cmd/controller-gen/main.go index f880b04ce..b421028a9 100644 --- a/cmd/controller-gen/main.go +++ b/cmd/controller-gen/main.go @@ -24,6 +24,7 @@ import ( "strings" "github.com/spf13/cobra" + "golang.org/x/tools/go/packages" "sigs.k8s.io/controller-tools/pkg/applyconfiguration" "sigs.k8s.io/controller-tools/pkg/crd" @@ -129,6 +130,7 @@ func main() { helpLevel := 0 whichLevel := 0 showVersion := false + var buildTags []string cmd := &cobra.Command{ Use: "controller-gen", @@ -173,7 +175,8 @@ func main() { } // otherwise, set up the runtime for actually running the generators - rt, err := genall.FromOptions(optionsRegistry, rawOpts) + tagsFlag := fmt.Sprintf("-tags=%s", strings.Join(buildTags, ",")) + rt, err := genall.FromOptionsWithConfig(&packages.Config{BuildFlags: []string{tagsFlag}}, optionsRegistry, rawOpts) if err != nil { return err } @@ -192,6 +195,7 @@ func main() { cmd.Flags().CountVarP(&whichLevel, "which-markers", "w", "print out all markers available with the requested generators\n(up to -www for the most detailed output, or -wwww for json output)") cmd.Flags().CountVarP(&helpLevel, "detailed-help", "h", "print out more detailed help\n(up to -hhh for the most detailed output, or -hhhh for json output)") cmd.Flags().BoolVar(&showVersion, "version", false, "show version") + cmd.Flags().StringSliceVar(&buildTags, "load-build-tags", []string{"ignore_autogenerated"}, "build tags to use when loading Go packages") cmd.Flags().Bool("help", false, "print out usage and a summary of options") oldUsage := cmd.UsageFunc() cmd.SetUsageFunc(func(c *cobra.Command) error { diff --git a/pkg/genall/genall.go b/pkg/genall/genall.go index 33f1f8c2f..b6f37409b 100644 --- a/pkg/genall/genall.go +++ b/pkg/genall/genall.go @@ -218,7 +218,11 @@ func (g GenerationContext) ReadFile(path string) ([]byte, error) { // ForRoots produces a Runtime to run the given generators against the // given packages. It outputs to /dev/null by default. func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) { - roots, err := loader.LoadRoots(rootPaths...) + return g.ForRootsWithConfig(&packages.Config{}, rootPaths...) +} + +func (g Generators) ForRootsWithConfig(cfg *packages.Config, rootPaths ...string) (*Runtime, error) { + roots, err := loader.LoadRootsWithConfig(cfg, rootPaths...) if err != nil { return nil, err } diff --git a/pkg/genall/options.go b/pkg/genall/options.go index ac57d7bed..192235b76 100644 --- a/pkg/genall/options.go +++ b/pkg/genall/options.go @@ -20,6 +20,7 @@ import ( "fmt" "strings" + "golang.org/x/tools/go/packages" "sigs.k8s.io/controller-tools/pkg/markers" ) @@ -74,13 +75,17 @@ func RegistryFromOptions(optionsRegistry *markers.Registry, options []string) (* // further modified. Not default generators are used if none are specified -- you can check // the output and rerun for that. func FromOptions(optionsRegistry *markers.Registry, options []string) (*Runtime, error) { + return FromOptionsWithConfig(&packages.Config{}, optionsRegistry, options) +} + +func FromOptionsWithConfig(cfg *packages.Config, optionsRegistry *markers.Registry, options []string) (*Runtime, error) { protoRt, err := protoFromOptions(optionsRegistry, options) if err != nil { return nil, err } // make the runtime - genRuntime, err := protoRt.Generators.ForRoots(protoRt.Paths...) + genRuntime, err := protoRt.Generators.ForRootsWithConfig(cfg, protoRt.Paths...) if err != nil { return nil, err } diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index 2c5cf4c97..8062b72d8 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -374,8 +374,14 @@ func LoadRootsWithConfig(cfg *packages.Config, roots ...string) ([]*Package, err if l.cfg.Fset == nil { l.cfg.Fset = token.NewFileSet() } - // put our build flags first so that callers can override them - l.cfg.BuildFlags = append([]string{"-tags", "ignore_autogenerated"}, l.cfg.BuildFlags...) + + // put our build flags first so that callers can override them. + // + // NOTE: if callers provide their own `-tags` flag, then our hardcoded `ignore_autogenerated` tag + // will be ignored. Callers that provide custom tags MUST include `ignore_autogenerated` in their + // custom tag set if they want that tag to remain active. Users can explicitly pass custom build + // flags with `-tags=""` to disable use of the default `ignore_autogenerated` tag. + l.cfg.BuildFlags = append([]string{"-tags=ignore_autogenerated"}, l.cfg.BuildFlags...) // Visit the import graphs of the loaded, root packages. If an imported // package refers to another loaded, root package, then replace the