diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index fa63765a22..726d86dddd 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -690,6 +690,16 @@ func createBuildArgs(buildCfg Config) ([]string, error) { args = append(args, fmt.Sprintf("-ldflags=%s", strings.Join(buildCfg.Ldflags, " "))) } + // Reject any flags that attempt to set --toolexec (with or + // without =, with one or two -s) + for _, a := range args { + for _, d := range []string{"-", "--"} { + if a == d+"toolexec" || strings.HasPrefix(a, d+"toolexec=") { + return nil, fmt.Errorf("cannot set %s", a) + } + } + } + return args, nil } diff --git a/test/build-configs/.ko.yaml b/test/build-configs/.ko.yaml index c92d94308b..d8994ea965 100644 --- a/test/build-configs/.ko.yaml +++ b/test/build-configs/.ko.yaml @@ -19,3 +19,9 @@ builds: - id: bar-app dir: ./bar main: ./cmd +- id: toolexec + dir: ./toolexec + main: ./cmd + flags: + - -toolexec + - go diff --git a/test/build-configs/toolexec/cmd/main.go b/test/build-configs/toolexec/cmd/main.go new file mode 100644 index 0000000000..2f0a0918ba --- /dev/null +++ b/test/build-configs/toolexec/cmd/main.go @@ -0,0 +1,21 @@ +// Copyright 2022 Google LLC All Rights Reserved. +// +// 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. + +package main + +import "fmt" + +func main() { + fmt.Println("toolexec") +} diff --git a/test/build-configs/toolexec/go.mod b/test/build-configs/toolexec/go.mod new file mode 100644 index 0000000000..32d35d09ae --- /dev/null +++ b/test/build-configs/toolexec/go.mod @@ -0,0 +1,17 @@ +// Copyright 2022 Google LLC All Rights Reserved. +// +// 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. + +module example.com/toolexec + +go 1.16