cmd/release: embed releaselet.go source code statically
Previously, the release binary dynamically looked for the releaselet.go
source code. It first checked in the current working directory, and
then used the go/build package to find the location of source code of
golang.org/x/build/cmd/release package on disk.
The release binary is not generally executed by hand, rather it is used
internally by releasebot command. That makes it hard to know in advance
where the current working directory for release will be (that directory
is $HOME/go-releasebot-work/<release> that releasebot creates itself).
Further, with the proliferation of module mode, it's no longer viable
to be able to find the location of a package by its import path via
go/build.Import. As a result, there's now a higher risk of cmd/release
not finding, or finding a wrong version of releaselet.go.
Change the release binary to instead statically embed the releaselet.go
source code as a constant string, in a static.go file that is generated
via a go:generate directive.
Add a test to ensure the embedded copy of releaselet.go doesn't get out
of sync.
The embedding approach was loosely based on approach taken in package
golang.org/x/tools/godoc/static, but it was simplified for the smaller
needs of cmd/release (i.e., embedding a single text file). We rely on
fmt's %q verb to do the escaping, which has the risk of changing between
Go versions, but it's unlikely to happen often to warrant worrying yet.
Fixes golang/go#33443
Change-Id: Ie7a9481c33a7d9668d696d3827e5681b07b37094
Reviewed-on: https://go-review.googlesource.com/c/build/+/189817
Reviewed-by: Filippo Valsorda <filippo@golang.org>
2019-08-10 22:16:18 +03:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-02-20 02:54:48 +03:00
|
|
|
//go:build ignore
|
cmd/release: embed releaselet.go source code statically
Previously, the release binary dynamically looked for the releaselet.go
source code. It first checked in the current working directory, and
then used the go/build package to find the location of source code of
golang.org/x/build/cmd/release package on disk.
The release binary is not generally executed by hand, rather it is used
internally by releasebot command. That makes it hard to know in advance
where the current working directory for release will be (that directory
is $HOME/go-releasebot-work/<release> that releasebot creates itself).
Further, with the proliferation of module mode, it's no longer viable
to be able to find the location of a package by its import path via
go/build.Import. As a result, there's now a higher risk of cmd/release
not finding, or finding a wrong version of releaselet.go.
Change the release binary to instead statically embed the releaselet.go
source code as a constant string, in a static.go file that is generated
via a go:generate directive.
Add a test to ensure the embedded copy of releaselet.go doesn't get out
of sync.
The embedding approach was loosely based on approach taken in package
golang.org/x/tools/godoc/static, but it was simplified for the smaller
needs of cmd/release (i.e., embedding a single text file). We rely on
fmt's %q verb to do the escaping, which has the risk of changing between
Go versions, but it's unlikely to happen often to warrant worrying yet.
Fixes golang/go#33443
Change-Id: Ie7a9481c33a7d9668d696d3827e5681b07b37094
Reviewed-on: https://go-review.googlesource.com/c/build/+/189817
Reviewed-by: Filippo Valsorda <filippo@golang.org>
2019-08-10 22:16:18 +03:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// Command makestatic writes the generated file "static.go".
|
|
|
|
// It is intended to be invoked via "go generate" (directive in "release.go").
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
b, err := ioutil.ReadFile("releaselet.go")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error reading releaselet.go: %v\n", err)
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf, `// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Code generated by makestatic.go; DO NOT EDIT.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
const releaselet = %q
|
|
|
|
`, b)
|
|
|
|
err = ioutil.WriteFile("static.go", buf.Bytes(), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error writing static.go: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|