diff --git a/cmd/paramgen/internal/paramgen.go b/cmd/paramgen/internal/paramgen.go index c9a7e690..69992403 100644 --- a/cmd/paramgen/internal/paramgen.go +++ b/cmd/paramgen/internal/paramgen.go @@ -123,6 +123,18 @@ func parsePackage(path string) (*ast.Package, error) { if len(pkgs) == 0 { return nil, fmt.Errorf("no source-code package in directory %s", path) } + // Ignore files with go:build constraint set to "tools" (common pattern in + // Conduit connectors). + for pkgName, pkg := range pkgs { + for fileName, f := range pkg.Files { + if hasBuildConstraint(f, "tools") { + delete(pkg.Files, fileName) + } + } + if len(pkg.Files) == 0 { + delete(pkgs, pkgName) + } + } if len(pkgs) > 1 { return nil, fmt.Errorf("multiple packages %v in directory %s", maps.Keys(pkgs), path) } @@ -132,6 +144,20 @@ func parsePackage(path string) (*ast.Package, error) { panic("unreachable") } +// hasBuildConstraint is a very naive way to check if a file has a build +// constraint. It is sufficient for our use case. +func hasBuildConstraint(f *ast.File, constraint string) bool { + text := fmt.Sprintf("//go:build %s", constraint) + for _, cg := range f.Comments { + for _, c := range cg.List { + if c.Text == text { + return true + } + } + } + return false +} + func findStruct(pkg *ast.Package, name string) (*ast.StructType, *ast.File, error) { var structType *ast.StructType var file *ast.File diff --git a/cmd/paramgen/internal/testdata/complex/tools.go b/cmd/paramgen/internal/testdata/complex/tools.go new file mode 100644 index 00000000..7360cec5 --- /dev/null +++ b/cmd/paramgen/internal/testdata/complex/tools.go @@ -0,0 +1,19 @@ +// Copyright © 2024 Meroxa, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build tools + +// This file should be ignored because of the build tag. + +package main