Skip to content

Commit

Permalink
all: fetch latest upstream change
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
  • Loading branch information
zchee committed Feb 13, 2022
1 parent c10fb95 commit 8239498
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 14 deletions.
8 changes: 5 additions & 3 deletions internal/event/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func Of64(k Key, v uint64) Label { return Label{key: k, packed: v} }
// access should be done with the From method of the key.
func (t Label) Unpack64() uint64 { return t.packed }

type stringptr unsafe.Pointer

// OfString creates a new label from a key and a string.
// This method is for implementing new key types, label creation should
// normally be done with the Of method of the key.
Expand All @@ -104,7 +106,7 @@ func OfString(k Key, v string) Label {
return Label{
key: k,
packed: uint64(hdr.Len),
untyped: unsafe.Pointer(hdr.Data),
untyped: stringptr(hdr.Data),
}
}

Expand All @@ -115,9 +117,9 @@ func OfString(k Key, v string) Label {
func (t Label) UnpackString() string {
var v string
hdr := (*reflect.StringHeader)(unsafe.Pointer(&v))
hdr.Data = uintptr(t.untyped.(unsafe.Pointer))
hdr.Data = uintptr(t.untyped.(stringptr))
hdr.Len = int(t.packed)
return *(*string)(unsafe.Pointer(hdr))
return v
}

// Valid returns true if the Label is a valid one (it has a key).
Expand Down
28 changes: 26 additions & 2 deletions internal/fastwalk/fastwalk_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const blockSize = 8 << 10
const unknownFileMode os.FileMode = os.ModeNamedPipe | os.ModeSocket | os.ModeDevice

func readDir(dirName string, fn func(dirName, entName string, typ os.FileMode) error) error {
fd, err := syscall.Open(dirName, 0, 0)
fd, err := open(dirName, 0, 0)
if err != nil {
return &os.PathError{Op: "open", Path: dirName, Err: err}
}
Expand All @@ -36,7 +36,7 @@ func readDir(dirName string, fn func(dirName, entName string, typ os.FileMode) e
for {
if bufp >= nbuf {
bufp = 0
nbuf, err = syscall.ReadDirent(fd, buf)
nbuf, err = readDirent(fd, buf)
if err != nil {
return os.NewSyscallError("readdirent", err)
}
Expand Down Expand Up @@ -127,3 +127,27 @@ func parseDirEnt(buf []byte) (consumed int, name string, typ os.FileMode) {
}
return
}

// According to https://golang.org/doc/go1.14#runtime
// A consequence of the implementation of preemption is that on Unix systems, including Linux and macOS
// systems, programs built with Go 1.14 will receive more signals than programs built with earlier releases.
//
// This causes syscall.Open and syscall.ReadDirent sometimes fail with EINTR errors.
// We need to retry in this case.
func open(path string, mode int, perm uint32) (fd int, err error) {
for {
fd, err := syscall.Open(path, mode, perm)
if err != syscall.EINTR {
return fd, err
}
}
}

func readDirent(fd int, buf []byte) (n int, err error) {
for {
nbuf, err := syscall.ReadDirent(fd, buf)
if err != syscall.EINTR {
return nbuf, err
}
}
}
27 changes: 24 additions & 3 deletions internal/gocommand/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,19 @@ type Invocation struct {
Verb string
Args []string
BuildFlags []string
ModFlag string
ModFile string
Overlay string

// If ModFlag is set, the go command is invoked with -mod=ModFlag.
ModFlag string

// If ModFile is set, the go command is invoked with -modfile=ModFile.
ModFile string

// If WorkFile is set, the go command is invoked with -workfile=WorkFile.
WorkFile string

// If Overlay is set, the go command is invoked with -overlay=Overlay.
Overlay string

// If CleanEnv is set, the invocation will run only with the environment
// in Env, not starting with os.Environ.
CleanEnv bool
Expand All @@ -160,6 +170,9 @@ func (i *Invocation) runWithFriendlyError(ctx context.Context, stdout, stderr io
}

func (i *Invocation) run(ctx context.Context, stdout, stderr io.Writer) error {
if i.ModFile != "" && i.WorkFile != "" {
return fmt.Errorf("bug: go command invoked with both -modfile and -workfile")
}
log := i.Logf
if log == nil {
log = func(string, ...interface{}) {}
Expand All @@ -172,6 +185,11 @@ func (i *Invocation) run(ctx context.Context, stdout, stderr io.Writer) error {
goArgs = append(goArgs, "-modfile="+i.ModFile)
}
}
appendWorkFile := func() {
if i.WorkFile != "" {
goArgs = append(goArgs, "-workfile="+i.WorkFile)
}
}
appendModFlag := func() {
if i.ModFlag != "" {
goArgs = append(goArgs, "-mod="+i.ModFlag)
Expand All @@ -190,16 +208,19 @@ func (i *Invocation) run(ctx context.Context, stdout, stderr io.Writer) error {
// mod needs the sub-verb before flags.
goArgs = append(goArgs, i.Args[0])
appendModFile()
appendWorkFile()
goArgs = append(goArgs, i.Args[1:]...)
case "get":
goArgs = append(goArgs, i.BuildFlags...)
appendModFile()
appendWorkFile()
goArgs = append(goArgs, i.Args...)

default: // notably list and build.
goArgs = append(goArgs, i.BuildFlags...)
appendModFile()
appendModFlag()
appendWorkFile()
appendOverlayFlag()
goArgs = append(goArgs, i.Args...)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/gocommand/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"golang.org/x/mod/semver"
)

// ModuleJSON holds information about a module.
type ModuleJSON struct {
Path string // module path
Version string // module version
Versions []string // available module versions (with -versions)
Replace *ModuleJSON // replaced by this module
Time *time.Time // time version was created
Update *ModuleJSON // available update, if any (with -u)
Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file for this module, if any
GoMod string // path to go.mod file used when loading this module, if any
GoVersion string // go version used in module
}

Expand Down
2 changes: 1 addition & 1 deletion internal/gocommand/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// It returns the X in Go 1.X.
func GoVersion(ctx context.Context, inv Invocation, r *Runner) (int, error) {
inv.Verb = "list"
inv.Args = []string{"-e", "-f", `{{context.ReleaseTags}}`}
inv.Args = []string{"-e", "-f", `{{context.ReleaseTags}}`, `--`, `unsafe`}
inv.Env = append(append([]string{}, inv.Env...), "GO111MODULE=off")
// Unset any unneeded flags, and remove them from BuildFlags, if they're
// present.
Expand Down
2 changes: 2 additions & 0 deletions internal/imports/mkstdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func main() {
mustOpen(api("go1.13.txt")),
mustOpen(api("go1.14.txt")),
mustOpen(api("go1.15.txt")),
mustOpen(api("go1.16.txt")),
mustOpen(api("go1.17.txt")),

// The API of the syscall/js package needs to be computed explicitly,
// because it's not included in the GOROOT/api/go1.*.txt files at this time.
Expand Down
6 changes: 4 additions & 2 deletions internal/imports/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ func (r *ModuleResolver) init() error {
err := r.initAllMods()
// We expect an error when running outside of a module with
// GO111MODULE=on. Other errors are fatal.
if err != nil && !strings.Contains(err.Error(), "working directory is not part of a module") {
return err
if err != nil {
if errMsg := err.Error(); !strings.Contains(errMsg, "working directory is not part of a module") && !strings.Contains(errMsg, "go.mod file not found") {
return err
}
}
}

Expand Down
15 changes: 13 additions & 2 deletions internal/imports/sortimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package imports
import (
"go/ast"
"go/token"
"log"
"sort"
"strconv"
)
Expand Down Expand Up @@ -60,6 +61,7 @@ func sortImports(localPrefix string, fset *token.FileSet, f *ast.File) {

// mergeImports merges all the import declarations into the first one.
// Taken from golang.org/x/tools/ast/astutil.
// This does not adjust line numbers properly
func mergeImports(fset *token.FileSet, f *ast.File) {
if len(f.Decls) <= 1 {
return
Expand Down Expand Up @@ -237,8 +239,17 @@ func sortSpecs(localPrefix string, fset *token.FileSet, f *ast.File, specs []ast
p := s.Pos()
line := fset.File(p).Line(p)
for previousLine := line - 1; previousLine >= firstSpecLine; {
fset.File(p).MergeLine(previousLine)
previousLine--
// MergeLine can panic. Avoid the panic at the cost of not removing the blank line
// golang/go#50329
if previousLine > 0 && previousLine < fset.File(p).LineCount() {
fset.File(p).MergeLine(previousLine)
previousLine--
} else {
// try to gather some data to diagnose how this could happen
req := "Please report what the imports section of your go file looked like."
log.Printf("panic avoided: first:%d line:%d previous:%d max:%d. %s",
firstSpecLine, line, previousLine, fset.File(p).LineCount(), req)
}
}
}
return specs
Expand Down
Loading

0 comments on commit 8239498

Please sign in to comment.