diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 09ef2ad2eb..cc34ca1227 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -278,7 +278,6 @@ tasks: - "-//tests/core/coverage:issue3017_test" - "-//tests/core/cross:proto_test" - "-//tests/core/go_binary:go_default_test" - - "-//tests/extras/go_embed_data:go_default_test" - "-//tests/core/go_path:go_path" - "-//tests/core/go_path:go_path_test" - "-//tests/core/go_path/pkg/lib:embed_test" diff --git a/README.rst b/README.rst index 94697cb000..3f993437e3 100644 --- a/README.rst +++ b/README.rst @@ -40,7 +40,6 @@ Go rules for Bazel_ .. _go_binary: docs/go/core/rules.md#go_binary .. _go_context: go/toolchains.rst#go_context .. _go_download_sdk: go/toolchains.rst#go_download_sdk -.. _go_embed_data: docs/go/extras/extras.md#go_embed_data .. _go_host_sdk: go/toolchains.rst#go_host_sdk .. _go_library: docs/go/core/rules.md#go_library .. _go_local_sdk: go/toolchains.rst#go_local_sdk @@ -174,8 +173,6 @@ Documentation * `Extra rules `_ - * `go_embed_data`_ - * `nogo build-time static analysis`_ * `Build modes `_ diff --git a/WORKSPACE b/WORKSPACE index b8a6cbdcf3..df571bc349 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -22,10 +22,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() -load("@io_bazel_rules_go//extras:embed_data_deps.bzl", "go_embed_data_dependencies") - -go_embed_data_dependencies() - # Used by //tests:buildifier_test. http_archive( name = "com_github_bazelbuild_buildtools", diff --git a/docs/go/extras/BUILD.bazel b/docs/go/extras/BUILD.bazel index 3a7d8c0535..7d8111587e 100644 --- a/docs/go/extras/BUILD.bazel +++ b/docs/go/extras/BUILD.bazel @@ -10,7 +10,6 @@ bzl_library( srcs = ["extras.bzl"], visibility = ["//visibility:public"], deps = [ - "//extras:embed_data", "//extras:gomock", ], ) diff --git a/docs/go/extras/extras.bzl b/docs/go/extras/extras.bzl index e58395ae2f..3ebe466626 100644 --- a/docs/go/extras/extras.bzl +++ b/docs/go/extras/extras.bzl @@ -11,7 +11,6 @@ to make life a little easier. ## Contents - [gazelle](#gazelle) - [gomock](#gomock) -- [go_embed_data](#go_embed_data) ## Additional resources - [gazelle rule] @@ -28,8 +27,5 @@ This rule has moved. See [gazelle rule] in the Gazelle repository. """ load("//extras:gomock.bzl", _gomock = "gomock") -load("//extras:embed_data.bzl", _go_embed_data = "go_embed_data") gomock = _gomock - -go_embed_data = _go_embed_data diff --git a/docs/go/extras/extras.md b/docs/go/extras/extras.md index e3697f8fa0..b66a4dcbd2 100644 --- a/docs/go/extras/extras.md +++ b/docs/go/extras/extras.md @@ -13,7 +13,6 @@ to make life a little easier. ## Contents - [gazelle](#gazelle) - [gomock](#gomock) -- [go_embed_data](#go_embed_data) ## Additional resources - [gazelle rule] @@ -29,50 +28,6 @@ This rule has moved. See [gazelle rule] in the Gazelle repository. - - - - - -## go_embed_data - -
-go_embed_data(name, flatten, package, src, srcs, string, unpack, var)
-
- -**Deprecated**: Will be removed in rules_go 0.39. - -`go_embed_data` generates a .go file that contains data from a file or a -list of files. It should be consumed in the srcs list of one of the -[core go rules]. - -Before using `go_embed_data`, you must add the following snippet to your -WORKSPACE: - -``` bzl -load("@io_bazel_rules_go//extras:embed_data_deps.bzl", "go_embed_data_dependencies") - -go_embed_data_dependencies() -``` - -`go_embed_data` accepts the attributes listed below. - - -### **Attributes** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| flatten | If True and srcs is used, map keys are file base names instead of relative paths. | Boolean | optional | False | -| package | Go package name for the generated .go file. | String | optional | "" | -| src | A single file to embed. This cannot be used at the same time as srcs. The generated file will have a variable of type []byte or string with the contents of this file. | Label | optional | None | -| srcs | A list of files to embed. This cannot be used at the same time as src. The generated file will have a variable of type map[string][]byte or map[string]string with the contents of each file. The map keys are relative paths of the files from the repository root. Keys for files in external repositories will be prefixed with "external/repo/" where "repo" is the name of the external repository. | List of labels | optional | [] | -| string | If True, the embedded data will be stored as string instead of []byte. | Boolean | optional | False | -| unpack | If True, sources are treated as archives and their contents will be stored. Supported formats are .zip and .tar | Boolean | optional | False | -| var | Name of the variable that will contain the embedded data. | String | optional | "Data" | - - ## gomock diff --git a/extras/BUILD.bazel b/extras/BUILD.bazel index 9bdca7f8e7..69a706e0c0 100644 --- a/extras/BUILD.bazel +++ b/extras/BUILD.bazel @@ -13,31 +13,6 @@ filegroup( visibility = ["//visibility:public"], ) -bzl_library( - name = "bindata", - srcs = ["bindata.bzl"], - visibility = ["//visibility:public"], - deps = ["@io_bazel_rules_go//go:def"], -) - -bzl_library( - name = "embed_data", - srcs = ["embed_data.bzl"], - visibility = ["//visibility:public"], - deps = [ - "//go/private:context", - "//go/private:go_toolchain", - ], -) - -bzl_library( - name = "embed_data_deps", - srcs = ["embed_data_deps.bzl"], - visibility = ["//visibility:public"], - # Don't list dependency on @bazel_tools//tools/build_defs/repo.bzl - deps = [], # keep -) - bzl_library( name = "gomock", srcs = ["gomock.bzl"], diff --git a/extras/bindata.bzl b/extras/bindata.bzl deleted file mode 100644 index 40c119f9a4..0000000000 --- a/extras/bindata.bzl +++ /dev/null @@ -1,87 +0,0 @@ -# Copyright 2018 The Bazel Authors. 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. - -"""bindata.bzl provides the bindata rule for embedding data in .go files""" - -load( - "//go:def.bzl", - "go_context", -) -load( - "//go/private:go_toolchain.bzl", - "GO_TOOLCHAIN", -) - -def _bindata_impl(ctx): - print("Embedding is now better handled by using rules_go's built-in embedding functionality (https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_library-embedsrcs). The `bindata` rule is deprecated and will be removed in rules_go version 0.39.") - go = go_context(ctx) - out = go.declare_file(go, ext = ".go") - arguments = ctx.actions.args() - arguments.add_all([ - "-o", - out, - "-pkg", - ctx.attr.package, - "-prefix", - ctx.label.package, - ]) - if not ctx.attr.compress: - arguments.add("-nocompress") - if not ctx.attr.metadata: - arguments.add("-nometadata") - if not ctx.attr.memcopy: - arguments.add("-nomemcopy") - if not ctx.attr.modtime: - arguments.add_all(["-modtime", "0"]) - if ctx.attr.extra_args: - arguments.add_all(ctx.attr.extra_args) - srcs = [f.path for f in ctx.files.srcs] - if ctx.attr.strip_external and any([f.startswith("external/") for f in srcs]): - arguments.add("-prefix", ctx.label.workspace_root + "/" + ctx.label.package) - arguments.add_all(srcs) - ctx.actions.run( - inputs = ctx.files.srcs, - outputs = [out], - mnemonic = "GoBindata", - executable = ctx.executable._bindata, - arguments = [arguments], - ) - return [ - DefaultInfo( - files = depset([out]), - ), - ] - -bindata = rule( - implementation = _bindata_impl, - attrs = { - "srcs": attr.label_list(allow_files = True), - "package": attr.string(mandatory = True), - "compress": attr.bool(default = True), - "metadata": attr.bool(default = False), - "memcopy": attr.bool(default = True), - "modtime": attr.bool(default = False), - "strip_external": attr.bool(default = False), - "extra_args": attr.string_list(), - "_bindata": attr.label( - executable = True, - cfg = "exec", - default = "@com_github_kevinburke_go_bindata//go-bindata:go-bindata", - ), - "_go_context_data": attr.label( - default = "//:go_context_data", - ), - }, - toolchains = [GO_TOOLCHAIN], -) diff --git a/extras/embed_data.bzl b/extras/embed_data.bzl deleted file mode 100644 index fb3519e23a..0000000000 --- a/extras/embed_data.bzl +++ /dev/null @@ -1,146 +0,0 @@ -# Copyright 2017 The Bazel Authors. 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. - -load( - "//go/private:context.bzl", #TODO: This ought to be def - "go_context", -) -load( - "//go/private:go_toolchain.bzl", - "GO_TOOLCHAIN", -) - -_DOC = """**Deprecated**: Will be removed in rules_go 0.39. - -`go_embed_data` generates a .go file that contains data from a file or a -list of files. It should be consumed in the srcs list of one of the -[core go rules]. - -Before using `go_embed_data`, you must add the following snippet to your -WORKSPACE: - -``` bzl -load("@io_bazel_rules_go//extras:embed_data_deps.bzl", "go_embed_data_dependencies") - -go_embed_data_dependencies() -``` - -`go_embed_data` accepts the attributes listed below. -""" - -def _go_embed_data_impl(ctx): - print("Embedding is now better handled by using rules_go's built-in embedding functionality (https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_library-embedsrcs). The `go_embed_data` rule is deprecated and will be removed in rules_go version 0.39.") - - go = go_context(ctx) - if ctx.attr.src and ctx.attr.srcs: - fail("%s: src and srcs attributes cannot both be specified" % ctx.label) - if ctx.attr.src and ctx.attr.flatten: - fail("%s: src and flatten attributes cannot both be specified" % ctx.label) - - args = ctx.actions.args() - if ctx.attr.src: - srcs = [ctx.file.src] - else: - srcs = ctx.files.srcs - args.add("-multi") - - if ctx.attr.package: - package = ctx.attr.package - else: - _, _, package = ctx.label.package.rpartition("/") - if package == "": - fail("%s: must provide package attribute for go_embed_data rules in the repository root directory" % ctx.label) - - out = go.declare_file(go, ext = ".go") - args.add_all([ - "-workspace", - ctx.workspace_name, - "-label", - str(ctx.label), - "-out", - out, - "-package", - package, - "-var", - ctx.attr.var, - ]) - if ctx.attr.flatten: - args.add("-flatten") - if ctx.attr.string: - args.add("-string") - if ctx.attr.unpack: - args.add("-unpack") - args.add("-multi") - args.add_all(srcs) - - library = go.new_library(go, srcs = [out]) - source = go.library_to_source(go, {}, library, ctx.coverage_instrumented()) - - ctx.actions.run( - outputs = [out], - inputs = srcs, - executable = ctx.executable._embed, - arguments = [args], - mnemonic = "GoSourcesData", - ) - return [ - DefaultInfo(files = depset([out])), - library, - source, - ] - -go_embed_data = rule( - implementation = _go_embed_data_impl, - doc = _DOC, - attrs = { - "package": attr.string( - doc = "Go package name for the generated .go file.", - ), - "var": attr.string( - default = "Data", - doc = "Name of the variable that will contain the embedded data.", - ), - "src": attr.label( - allow_single_file = True, - doc = """A single file to embed. This cannot be used at the same time as `srcs`. - The generated file will have a variable of type `[]byte` or `string` with the contents of this file.""", - ), - "srcs": attr.label_list( - allow_files = True, - doc = """A list of files to embed. This cannot be used at the same time as `src`. - The generated file will have a variable of type `map[string][]byte` or `map[string]string` with the contents - of each file. The map keys are relative paths of the files from the repository root. Keys for files in external - repositories will be prefixed with `"external/repo/"` where "repo" is the name of the external repository.""", - ), - "flatten": attr.bool( - doc = "If `True` and `srcs` is used, map keys are file base names instead of relative paths.", - ), - "unpack": attr.bool( - doc = "If `True`, sources are treated as archives and their contents will be stored. Supported formats are `.zip` and `.tar`", - ), - "string": attr.bool( - doc = "If `True`, the embedded data will be stored as `string` instead of `[]byte`.", - ), - "_embed": attr.label( - default = "//go/tools/builders:embed", - executable = True, - cfg = "exec", - ), - "_go_context_data": attr.label( - default = "//:go_context_data", - ), - }, - toolchains = [GO_TOOLCHAIN], -) -# See /docs/go/extras/extras.md#go_embed_data for full documentation. diff --git a/extras/embed_data_deps.bzl b/extras/embed_data_deps.bzl deleted file mode 100644 index 1e2d6e8844..0000000000 --- a/extras/embed_data_deps.bzl +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2019 The Bazel Authors. 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. - -"""Repository dependencies for embed_data.bzl""" - -load( - "@bazel_tools//tools/build_defs/repo:git.bzl", - "git_repository", -) - -def go_embed_data_dependencies(): - print("Embedding is now better handled by using rules_go's built-in embedding functionality (https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_library-embedsrcs). The `go_embed_data_dependencies` macro is deprecated and will be removed in rules_go version 0.39.") - - if "com_github_kevinburke_go_bindata" not in native.existing_rules(): - git_repository( - name = "com_github_kevinburke_go_bindata", - remote = "https://github.com/kevinburke/go-bindata", - # v3.13.0+incompatible, "latest" as of 2019-07-08 - commit = "53d73b98acf3bd9f56d7f9136ed8e1be64756e1d", - patches = [Label("//third_party:com_github_kevinburke_go_bindata-gazelle.patch")], - patch_args = ["-p1"], - shallow_since = "1545009224 +0000", - # gazelle args: -go_prefix github.com/kevinburke/go-bindata - ) diff --git a/go/BUILD.bazel b/go/BUILD.bazel index f88616c257..a67cb408f0 100644 --- a/go/BUILD.bazel +++ b/go/BUILD.bazel @@ -46,7 +46,6 @@ bzl_library( srcs = ["def.bzl"], visibility = ["//visibility:public"], deps = [ - "//extras:embed_data", "//extras:gomock", "//go/private:context", "//go/private:go_toolchain", diff --git a/go/def.bzl b/go/def.bzl index 2060b2f8bf..95261e68b1 100644 --- a/go/def.bzl +++ b/go/def.bzl @@ -53,10 +53,6 @@ load( "//go/private/rules:source.bzl", _go_source = "go_source", ) -load( - "//extras:embed_data.bzl", - _go_embed_data = "go_embed_data", -) load( "//extras:gomock.bzl", _gomock = "gomock", @@ -126,7 +122,6 @@ TOOLS_NOGO = [ RULES_GO_VERSION = "0.40.0" go_context = _go_context -go_embed_data = _go_embed_data gomock = _gomock go_sdk = _go_sdk go_tool_library = _go_tool_library diff --git a/go/tools/builders/BUILD.bazel b/go/tools/builders/BUILD.bazel index 28724714e8..45e11fd1cb 100644 --- a/go/tools/builders/BUILD.bazel +++ b/go/tools/builders/BUILD.bazel @@ -77,12 +77,6 @@ filegroup( visibility = ["//visibility:public"], ) -go_binary( - name = "embed", - srcs = ["embed.go"], - visibility = ["//visibility:public"], -) - go_source( name = "nogo_srcs", srcs = [ diff --git a/go/tools/builders/embed.go b/go/tools/builders/embed.go deleted file mode 100644 index e68da97443..0000000000 --- a/go/tools/builders/embed.go +++ /dev/null @@ -1,340 +0,0 @@ -// Copyright 2017 The Bazel Authors. 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. - -// embed generates a .go file from the contents of a list of data files. It is -// invoked by go_embed_data as an action. -package main - -import ( - "archive/tar" - "archive/zip" - "bufio" - "errors" - "flag" - "fmt" - "io" - "log" - "os" - "path" - "path/filepath" - "strconv" - "strings" - "text/template" - "unicode/utf8" -) - -var headerTpl = template.Must(template.New("embed").Parse(`// Generated by go_embed_data for {{.Label}}. DO NOT EDIT. - -package {{.Package}} - -`)) - -var multiFooterTpl = template.Must(template.New("embed").Parse(` -var {{.Var}} = map[string]{{.Type}}{ -{{- range $i, $f := .FoundSources}} - {{$.Key $f}}: {{$.Var}}_{{$i}}, -{{- end}} -} - -`)) - -func main() { - log.SetPrefix("embed: ") - log.SetFlags(0) // don't print timestamps - if err := run(os.Args); err != nil { - log.Fatal(err) - } -} - -type configuration struct { - Label, Package, Var string - Multi bool - sources []string - FoundSources []string - out, workspace string - flatten, unpack, strData bool -} - -func (c *configuration) Type() string { - if c.strData { - return "string" - } else { - return "[]byte" - } -} - -func (c *configuration) Key(filename string) string { - workspacePrefix := "external/" + c.workspace + "/" - key := filepath.FromSlash(strings.TrimPrefix(filename, workspacePrefix)) - if c.flatten { - key = path.Base(filename) - } - return strconv.Quote(key) -} - -func run(args []string) error { - c, err := newConfiguration(args) - if err != nil { - return err - } - - f, err := os.Create(c.out) - if err != nil { - return err - } - defer f.Close() - w := bufio.NewWriter(f) - defer w.Flush() - - if err := headerTpl.Execute(w, c); err != nil { - return err - } - - if c.Multi { - return embedMultipleFiles(c, w) - } - return embedSingleFile(c, w) -} - -func newConfiguration(args []string) (*configuration, error) { - var c configuration - flags := flag.NewFlagSet("embed", flag.ExitOnError) - flags.StringVar(&c.Label, "label", "", "Label of the rule being executed (required)") - flags.StringVar(&c.Package, "package", "", "Go package name (required)") - flags.StringVar(&c.Var, "var", "", "Variable name (required)") - flags.BoolVar(&c.Multi, "multi", false, "Whether the variable is a map or a single value") - flags.StringVar(&c.out, "out", "", "Go file to generate (required)") - flags.StringVar(&c.workspace, "workspace", "", "Name of the workspace (required)") - flags.BoolVar(&c.flatten, "flatten", false, "Whether to access files by base name") - flags.BoolVar(&c.strData, "string", false, "Whether to store contents as strings") - flags.BoolVar(&c.unpack, "unpack", false, "Whether to treat files as archives to unpack.") - flags.Parse(args[1:]) - if c.Label == "" { - return nil, errors.New("error: -label option not provided") - } - if c.Package == "" { - return nil, errors.New("error: -package option not provided") - } - if c.Var == "" { - return nil, errors.New("error: -var option not provided") - } - if c.out == "" { - return nil, errors.New("error: -out option not provided") - } - if c.workspace == "" { - return nil, errors.New("error: -workspace option not provided") - } - c.sources = flags.Args() - if !c.Multi && len(c.sources) != 1 { - return nil, fmt.Errorf("error: -multi flag not given, so want exactly one source; got %d", len(c.sources)) - } - if c.unpack { - if !c.Multi { - return nil, errors.New("error: -multi flag is required for -unpack mode.") - } - for _, src := range c.sources { - if ext := filepath.Ext(src); ext != ".zip" && ext != ".tar" { - return nil, fmt.Errorf("error: -unpack flag expects .zip or .tar extension (got %q)", ext) - } - } - } - return &c, nil -} - -func embedSingleFile(c *configuration, w io.Writer) error { - dataBegin, dataEnd := "\"", "\"\n" - if !c.strData { - dataBegin, dataEnd = "[]byte(\"", "\")\n" - } - - if _, err := fmt.Fprintf(w, "var %s = %s", c.Var, dataBegin); err != nil { - return err - } - if err := embedFileContents(w, c.sources[0]); err != nil { - return err - } - _, err := fmt.Fprint(w, dataEnd) - return err -} - -func embedMultipleFiles(c *configuration, w io.Writer) error { - dataBegin, dataEnd := "\"", "\"\n" - if !c.strData { - dataBegin, dataEnd = "[]byte(\"", "\")\n" - } - - if _, err := fmt.Fprint(w, "var (\n"); err != nil { - return err - } - if err := findSources(c, func(i int, f io.Reader) error { - if _, err := fmt.Fprintf(w, "\t%s_%d = %s", c.Var, i, dataBegin); err != nil { - return err - } - if _, err := io.Copy(&escapeWriter{w}, f); err != nil { - return err - } - if _, err := fmt.Fprint(w, dataEnd); err != nil { - return err - } - return nil - }); err != nil { - return err - } - if _, err := fmt.Fprint(w, ")\n"); err != nil { - return err - } - if err := multiFooterTpl.Execute(w, c); err != nil { - return err - } - return nil -} - -func findSources(c *configuration, cb func(i int, f io.Reader) error) error { - if c.unpack { - for _, filename := range c.sources { - ext := filepath.Ext(filename) - if ext == ".zip" { - if err := findZipSources(c, filename, cb); err != nil { - return err - } - } else if ext == ".tar" { - if err := findTarSources(c, filename, cb); err != nil { - return err - } - } else { - panic("unknown archive extension: " + ext) - } - } - return nil - } - for _, filename := range c.sources { - f, err := os.Open(filename) - if err != nil { - return err - } - err = cb(len(c.FoundSources), bufio.NewReader(f)) - f.Close() - if err != nil { - return err - } - c.FoundSources = append(c.FoundSources, filename) - } - return nil -} - -func findZipSources(c *configuration, filename string, cb func(i int, f io.Reader) error) error { - r, err := zip.OpenReader(filename) - if err != nil { - return err - } - defer r.Close() - for _, file := range r.File { - f, err := file.Open() - if err != nil { - return err - } - err = cb(len(c.FoundSources), f) - f.Close() - if err != nil { - return err - } - c.FoundSources = append(c.FoundSources, file.Name) - } - return nil -} - -func findTarSources(c *configuration, filename string, cb func(i int, f io.Reader) error) error { - tf, err := os.Open(filename) - if err != nil { - return err - } - defer tf.Close() - reader := tar.NewReader(bufio.NewReader(tf)) - for { - h, err := reader.Next() - if err == io.EOF { - return nil - } - if err != nil { - return err - } - if h.Typeflag != tar.TypeReg { - continue - } - if err := cb(len(c.FoundSources), &io.LimitedReader{ - R: reader, - N: h.Size, - }); err != nil { - return err - } - c.FoundSources = append(c.FoundSources, h.Name) - } -} - -func embedFileContents(w io.Writer, filename string) error { - f, err := os.Open(filename) - if err != nil { - return err - } - defer f.Close() - - _, err = io.Copy(&escapeWriter{w}, bufio.NewReader(f)) - return err -} - -type escapeWriter struct { - w io.Writer -} - -func (w *escapeWriter) Write(data []byte) (n int, err error) { - n = len(data) - - for err == nil && len(data) > 0 { - // https://golang.org/ref/spec#String_literals: "Within the quotes, any - // character may appear except newline and unescaped double quote. The - // text between the quotes forms the value of the literal, with backslash - // escapes interpreted as they are in rune literals […]." - switch b := data[0]; b { - case '\\': - _, err = w.w.Write([]byte(`\\`)) - case '"': - _, err = w.w.Write([]byte(`\"`)) - case '\n': - _, err = w.w.Write([]byte(`\n`)) - - case '\x00': - // https://golang.org/ref/spec#Source_code_representation: "Implementation - // restriction: For compatibility with other tools, a compiler may - // disallow the NUL character (U+0000) in the source text." - _, err = w.w.Write([]byte(`\x00`)) - - default: - // https://golang.org/ref/spec#Source_code_representation: "Implementation - // restriction: […] A byte order mark may be disallowed anywhere else in - // the source." - const byteOrderMark = '\uFEFF' - - if r, size := utf8.DecodeRune(data); r != utf8.RuneError && r != byteOrderMark { - _, err = w.w.Write(data[:size]) - data = data[size:] - continue - } - - _, err = fmt.Fprintf(w.w, `\x%02x`, b) - } - data = data[1:] - } - - return n - len(data), err -} diff --git a/tests/core/README.rst b/tests/core/README.rst index 835b9ba734..455c0b60f9 100644 --- a/tests/core/README.rst +++ b/tests/core/README.rst @@ -22,7 +22,6 @@ Contents * `.. _#2067: https://github.com/bazelbuild/rules_go/issues/2067 `_ * `Runfiles functionality `_ * `go_download_sdk `_ -* `go_embed_data `_ * `race instrumentation `_ * `stdlib functionality `_ * `Basic go_binary functionality `_ diff --git a/tests/extras/go_embed_data/BUILD.bazel b/tests/extras/go_embed_data/BUILD.bazel deleted file mode 100644 index e7b88ef7b6..0000000000 --- a/tests/extras/go_embed_data/BUILD.bazel +++ /dev/null @@ -1,107 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_embed_data", "go_library", "go_test") -load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") - -go_test( - name = "go_default_test", - srcs = ["embed_test.go"], - data = [ - "BUILD.bazel", - "//:AUTHORS", - "@com_google_protobuf//:LICENSE", - ], - embed = [":go_default_library"], - deps = ["//go/tools/bazel:go_default_library"], -) - -go_library( - name = "go_default_library", - srcs = [ - ":cgo", - ":empty", - ":ext", - ":flat", - ":local", - ":single", - ":str", - ":unpack", - ], - importpath = "github.com/bazelbuild/rules_go/tests/go_embed_data", -) - -go_embed_data( - name = "cgo", - srcs = ["error.c"], - package = "go_embed_data", - var = "cgo", -) - -go_embed_data( - name = "empty", - package = "go_embed_data", - var = "empty", -) - -go_embed_data( - name = "single", - src = "//:AUTHORS", - package = "go_embed_data", - var = "single", -) - -go_embed_data( - name = "local", - srcs = [ - ":BUILD.bazel", - "@io_bazel_rules_go//:AUTHORS", - ], - package = "go_embed_data", - var = "local", -) - -go_embed_data( - name = "ext", - srcs = ["@com_google_protobuf//:LICENSE"], - package = "go_embed_data", - var = "ext", -) - -go_embed_data( - name = "flat", - srcs = [":BUILD.bazel"], - flatten = True, - package = "go_embed_data", - var = "flat", -) - -go_embed_data( - name = "str", - srcs = [":BUILD.bazel"], - package = "go_embed_data", - string = True, - var = "str", -) - -go_embed_data( - name = "unpack", - srcs = [ - ":embedded_tar", - ":embedded_zip", - ], - package = "go_embed_data", - unpack = True, - var = "unpack", -) - -pkg_tar( - name = "embedded_tar", - srcs = [":BUILD.bazel"], - package_dir = "/from-tar", -) - -genrule( - name = "embedded_zip", - srcs = [":BUILD.bazel"], - outs = ["embedded_zip.zip"], - cmd = "$(location @bazel_tools//tools/zip:zipper) c $@ from-zip/BUILD.bazel=$(location :BUILD.bazel)", - tools = ["@bazel_tools//tools/zip:zipper"], -) diff --git a/tests/extras/go_embed_data/README.rst b/tests/extras/go_embed_data/README.rst deleted file mode 100644 index e3ebc4ec4e..0000000000 --- a/tests/extras/go_embed_data/README.rst +++ /dev/null @@ -1,11 +0,0 @@ -go_embed_data -============= - -.. _go_embed_data: /docs/go/extras/extras.md#go-embed-data - -Tests to ensure basic features of `go_embed_data`_ are working correctly. - -embed_test ----------- - -Depends on multiple ``go_embed_data`` targets and verifies their contents. diff --git a/tests/extras/go_embed_data/embed_test.go b/tests/extras/go_embed_data/embed_test.go deleted file mode 100644 index fdcd0fe0eb..0000000000 --- a/tests/extras/go_embed_data/embed_test.go +++ /dev/null @@ -1,131 +0,0 @@ -package go_embed_data - -import ( - "io" - "log" - "os" - "path/filepath" - "testing" - - "github.com/bazelbuild/rules_go/go/tools/bazel" -) - -func TestMain(m *testing.M) { - // This test must run from the workspace root since it accesses files using - // relative paths. We look at parent directories until we find AUTHORS, - // then change to that directory. - for { - if _, err := os.Stat("AUTHORS"); err == nil { - break - } - if err := os.Chdir(".."); err != nil { - log.Fatal(err) - } - if wd, err := os.Getwd(); err != nil { - log.Fatal(err) - } else if wd == "/" { - log.Fatal("could not locate workspace root") - } - } - os.Exit(m.Run()) -} - -func TestCgo(t *testing.T) { - if len(cgo) == 0 { - t.Fatalf("cgo is empty") - } -} - -func TestEmpty(t *testing.T) { - if len(empty) != 0 { - t.Fatalf("empty is not empty") - } -} - -func TestSingle(t *testing.T) { - checkFile(t, "AUTHORS", single) -} - -func TestLocal(t *testing.T) { - for path, data := range local { - checkFile(t, path, data) - } -} - -func TestExternal(t *testing.T) { - for path, data := range ext { - checkFile(t, path, data) - } -} - -func TestFlat(t *testing.T) { - for key := range flat { - if filepath.Base(key) != key { - t.Errorf("filename %q is not flat", key) - } - } -} - -func TestString(t *testing.T) { - for _, data := range str { - var _ string = data // just check the type; contents covered by other tests. - } -} - -func TestUnpack(t *testing.T) { - for _, data := range unpack { - checkFile(t, "tests/extras/go_embed_data/BUILD.bazel", data) - } - for _, key := range []string{ - "from-zip/BUILD.bazel", - // Note: Bazel's pkg_tar always adds a leading "./" to its outputs, - // but tars generated from other sources can match the original - // inputs more exactly. - "./from-tar/BUILD.bazel", - } { - if _, ok := unpack[key]; !ok { - t.Errorf("filename %q is not in unpacked set", key) - } - } -} - -func checkFile(t *testing.T, rawPath string, data []byte) { - path, err := bazel.Runfile(rawPath) - if err != nil { - t.Error(err) - return - } - - f, err := os.Open(path) - if err != nil { - t.Error(err) - return - } - defer f.Close() - - count := 0 - buffer := make([]byte, 8192) - for { - n, err := f.Read(buffer) - if err != nil && err != io.EOF { - t.Error(err) - return - } - if n == 0 { - return - } - if n > len(data) { - t.Errorf("%q: file on disk is longer than embedded data", path) - return - } - - for i := 0; i < n; i++ { - if buffer[i] != data[i] { - t.Errorf("data mismatch on file %q at offset %d", path, count+i) - return - } - } - count += n - data = data[n:] - } -} diff --git a/tests/extras/go_embed_data/error.c b/tests/extras/go_embed_data/error.c deleted file mode 100644 index 41c28ebf41..0000000000 --- a/tests/extras/go_embed_data/error.c +++ /dev/null @@ -1 +0,0 @@ -#error do not compile diff --git a/tests/legacy/examples/bindata/BUILD.bazel b/tests/legacy/examples/bindata/BUILD.bazel deleted file mode 100644 index b4d58a268d..0000000000 --- a/tests/legacy/examples/bindata/BUILD.bazel +++ /dev/null @@ -1,22 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") -load("@io_bazel_rules_go//extras:bindata.bzl", "bindata") - -bindata( - name = "data", - srcs = glob(["data/*"]), - package = "bindata", - strip_external = True, -) - -go_library( - name = "go_default_library", - srcs = [":data"], - importpath = "github.com/bazelbuild/rules_go/examples/bindata", -) - -go_test( - name = "go_default_test", - size = "small", - srcs = ["bindata_test.go"], - embed = [":go_default_library"], -) diff --git a/tests/legacy/examples/bindata/bindata_test.go b/tests/legacy/examples/bindata/bindata_test.go deleted file mode 100644 index f3eeb1c495..0000000000 --- a/tests/legacy/examples/bindata/bindata_test.go +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright 2016 The Bazel Authors. 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 bindata - -import ( - "testing" -) - -func TestMessage(t *testing.T) { - data, err := Asset("data/message.txt") - if err != nil { - // Asset was not found. - t.Errorf("Asset was not embeded: %s", err) - } - got := string(data) - const expected = "Some embedded data" - if got != expected { - t.Errorf("Got embedded asset %q\nexpected %q", got, expected) - } -} diff --git a/tests/legacy/examples/bindata/data/message.txt b/tests/legacy/examples/bindata/data/message.txt deleted file mode 100644 index 44ae74f438..0000000000 --- a/tests/legacy/examples/bindata/data/message.txt +++ /dev/null @@ -1 +0,0 @@ -Some embedded data \ No newline at end of file diff --git a/third_party/com_github_kevinburke_go_bindata-gazelle.patch b/third_party/com_github_kevinburke_go_bindata-gazelle.patch deleted file mode 100644 index a9c0cba31e..0000000000 --- a/third_party/com_github_kevinburke_go_bindata-gazelle.patch +++ /dev/null @@ -1,95 +0,0 @@ -diff -urN a/BUILD.bazel b/BUILD.bazel ---- a/BUILD.bazel 1969-12-31 19:00:00.000000000 -0500 -+++ b/BUILD.bazel 2000-01-01 00:00:00.000000000 -0000 -@@ -0,0 +1,32 @@ -+load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") -+ -+go_library( -+ name = "go_default_library", -+ srcs = [ -+ "asset.go", -+ "bits.go", -+ "bytewriter.go", -+ "config.go", -+ "convert.go", -+ "debug.go", -+ "doc.go", -+ "release.go", -+ "restore.go", -+ "safefile.go", -+ "stringwriter.go", -+ "toc.go", -+ ], -+ importpath = "github.com/kevinburke/go-bindata", -+ visibility = ["//visibility:public"], -+) -+ -+go_test( -+ name = "go_default_test", -+ srcs = [ -+ "benchmark_test.go", -+ "convert_test.go", -+ "release_test.go", -+ "safefile_test.go", -+ ], -+ embed = [":go_default_library"], -+) -diff -urN a/go-bindata/BUILD.bazel b/go-bindata/BUILD.bazel ---- a/go-bindata/BUILD.bazel 1969-12-31 19:00:00.000000000 -0500 -+++ b/go-bindata/BUILD.bazel 2000-01-01 00:00:00.000000000 -0000 -@@ -0,0 +1,19 @@ -+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") -+ -+go_library( -+ name = "go_default_library", -+ srcs = [ -+ "AppendSliceValue.go", -+ "main.go", -+ "version.go", -+ ], -+ importpath = "github.com/kevinburke/go-bindata/go-bindata", -+ visibility = ["//visibility:private"], -+ deps = ["//:go_default_library"], -+) -+ -+go_binary( -+ name = "go-bindata", -+ embed = [":go_default_library"], -+ visibility = ["//visibility:public"], -+) -diff -urN a/testdata/assets/BUILD.bazel b/testdata/assets/BUILD.bazel ---- a/testdata/assets/BUILD.bazel 1969-12-31 19:00:00.000000000 -0500 -+++ b/testdata/assets/BUILD.bazel 2000-01-01 00:00:00.000000000 -0000 -@@ -0,0 +1,8 @@ -+load("@io_bazel_rules_go//go:def.bzl", "go_library") -+ -+go_library( -+ name = "go_default_library", -+ srcs = ["bindata.go"], -+ importpath = "github.com/kevinburke/go-bindata/testdata/assets", -+ visibility = ["//visibility:public"], -+) -diff -urN a/testdata/out/BUILD.bazel b/testdata/out/BUILD.bazel ---- a/testdata/out/BUILD.bazel 1969-12-31 19:00:00.000000000 -0500 -+++ b/testdata/out/BUILD.bazel 2000-01-01 00:00:00.000000000 -0000 -@@ -0,0 +1,20 @@ -+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") -+ -+go_library( -+ name = "go_default_library", -+ srcs = [ -+ "compress-memcopy.go", -+ "compress-nomemcopy.go", -+ "debug.go", -+ "nocompress-memcopy.go", -+ "nocompress-nomemcopy.go", -+ ], -+ importpath = "github.com/kevinburke/go-bindata/testdata/out", -+ visibility = ["//visibility:private"], -+) -+ -+go_binary( -+ name = "out", -+ embed = [":go_default_library"], -+ visibility = ["//visibility:public"], -+)