From 4c4be04ba741fccb3b6078d057cf1299b163192b Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Mon, 21 Mar 2022 17:37:16 -0700 Subject: [PATCH 1/2] go1.18 compatibility: Add workaround for local replace directives --- internal/sbom/mod.go | 3 +++ internal/sbom/mod_1.18.go | 2 +- internal/sbom/mod_1.18_workaround.go | 40 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 internal/sbom/mod_1.18_workaround.go diff --git a/internal/sbom/mod.go b/internal/sbom/mod.go index c3925d18aa..35daf9e8a9 100644 --- a/internal/sbom/mod.go +++ b/internal/sbom/mod.go @@ -75,6 +75,9 @@ func quoteValue(value string) bool { // https://cs.opensource.google/go/go/+/release-branch.go1.18:src/runtime/debug/mod.go;drc=release-branch.go1.18;l=121 func ParseBuildInfo(data string) (bi *BuildInfo, err error) { + // Workaround for parsing go1.18 output from go1.17-compiled ko: + data = fixLocalReplaceDirectives(data) + lineNum := 1 defer func() { if err != nil { diff --git a/internal/sbom/mod_1.18.go b/internal/sbom/mod_1.18.go index 5defd2c6f3..e3893d05c9 100644 --- a/internal/sbom/mod_1.18.go +++ b/internal/sbom/mod_1.18.go @@ -25,7 +25,7 @@ import ( type BuildInfo debug.BuildInfo func ParseBuildInfo(data string) (*BuildInfo, error) { - dbi, err := debug.ParseBuildInfo(string(data)) + dbi, err := debug.ParseBuildInfo(fixLocalReplaceDirectives(data)) if err != nil { return nil, fmt.Errorf("parsing build info: %w", err) } diff --git a/internal/sbom/mod_1.18_workaround.go b/internal/sbom/mod_1.18_workaround.go new file mode 100644 index 0000000000..54ac091dc5 --- /dev/null +++ b/internal/sbom/mod_1.18_workaround.go @@ -0,0 +1,40 @@ +// 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 sbom + +import ( + "bufio" + "strings" +) + +var tab = "\t" + +// fixLocalReplaceDirectives is a workaround for go v1.18: +// debug.ParseBuildInfo expects a 3rd field (checksum), but go v1.18 does not populate the 3rd field +func fixLocalReplaceDirectives(data string) string { + var lines []string + sc := bufio.NewScanner(strings.NewReader(data)) + for sc.Scan() { + elem := strings.Split(sc.Text(), tab) + // Add extra tab to: + // => ../api (devel) + if len(elem) == 3 && elem[0] == "=>" && elem[2] == "(devel)" { + lines = append(lines, sc.Text()+tab) + continue + } + lines = append(lines, sc.Text()) + } + return strings.Join(lines, "\n") +} From 256f31a6e8f4eada109f644bd1b3a37430dee1d8 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Mon, 21 Mar 2022 18:34:53 -0700 Subject: [PATCH 2/2] Fix actual bug where trailing tabs were stripped --- internal/sbom/mod.go | 3 --- internal/sbom/mod_1.18.go | 2 +- internal/sbom/mod_1.18_workaround.go | 40 ---------------------------- internal/sbom/sbom.go | 4 ++- 4 files changed, 4 insertions(+), 45 deletions(-) delete mode 100644 internal/sbom/mod_1.18_workaround.go diff --git a/internal/sbom/mod.go b/internal/sbom/mod.go index 35daf9e8a9..c3925d18aa 100644 --- a/internal/sbom/mod.go +++ b/internal/sbom/mod.go @@ -75,9 +75,6 @@ func quoteValue(value string) bool { // https://cs.opensource.google/go/go/+/release-branch.go1.18:src/runtime/debug/mod.go;drc=release-branch.go1.18;l=121 func ParseBuildInfo(data string) (bi *BuildInfo, err error) { - // Workaround for parsing go1.18 output from go1.17-compiled ko: - data = fixLocalReplaceDirectives(data) - lineNum := 1 defer func() { if err != nil { diff --git a/internal/sbom/mod_1.18.go b/internal/sbom/mod_1.18.go index e3893d05c9..d8bf2b4a54 100644 --- a/internal/sbom/mod_1.18.go +++ b/internal/sbom/mod_1.18.go @@ -25,7 +25,7 @@ import ( type BuildInfo debug.BuildInfo func ParseBuildInfo(data string) (*BuildInfo, error) { - dbi, err := debug.ParseBuildInfo(fixLocalReplaceDirectives(data)) + dbi, err := debug.ParseBuildInfo(data) if err != nil { return nil, fmt.Errorf("parsing build info: %w", err) } diff --git a/internal/sbom/mod_1.18_workaround.go b/internal/sbom/mod_1.18_workaround.go deleted file mode 100644 index 54ac091dc5..0000000000 --- a/internal/sbom/mod_1.18_workaround.go +++ /dev/null @@ -1,40 +0,0 @@ -// 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 sbom - -import ( - "bufio" - "strings" -) - -var tab = "\t" - -// fixLocalReplaceDirectives is a workaround for go v1.18: -// debug.ParseBuildInfo expects a 3rd field (checksum), but go v1.18 does not populate the 3rd field -func fixLocalReplaceDirectives(data string) string { - var lines []string - sc := bufio.NewScanner(strings.NewReader(data)) - for sc.Scan() { - elem := strings.Split(sc.Text(), tab) - // Add extra tab to: - // => ../api (devel) - if len(elem) == 3 && elem[0] == "=>" && elem[2] == "(devel)" { - lines = append(lines, sc.Text()+tab) - continue - } - lines = append(lines, sc.Text()) - } - return strings.Join(lines, "\n") -} diff --git a/internal/sbom/sbom.go b/internal/sbom/sbom.go index 1728fbd786..d7e91b23c9 100644 --- a/internal/sbom/sbom.go +++ b/internal/sbom/sbom.go @@ -19,6 +19,7 @@ import ( "bytes" "fmt" "strings" + "unicode" ) // massageGoModVersion massages the output of `go version -m` into a form that @@ -41,7 +42,8 @@ func massageGoVersionM(b []byte) ([]byte, error) { return nil, fmt.Errorf("malformed input: %w", err) } for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) + // NOTE: debug.ParseBuildInfo relies on trailing tabs. + line := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace) fmt.Fprintln(&out, line) } if err := scanner.Err(); err != nil {