Skip to content

Commit

Permalink
re-generate with Go 1.16.4
Browse files Browse the repository at this point in the history
Fairly minor changes since the last time we re-vendored, with Go 1.15.x.

And test with Go 1.16.x too.
  • Loading branch information
mvdan committed May 20, 2021
1 parent 20d3d7f commit 1af1350
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.14.x, 1.15.x]
go-version: [1.15.x, 1.16.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
15 changes: 12 additions & 3 deletions gen.go
@@ -1,6 +1,7 @@
// Copyright (c) 2019, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information

//go:build ignore
// +build ignore

package main
Expand All @@ -22,7 +23,8 @@ func main() {
pkgs, err := listPackages(context.TODO(), nil,
"cmd/gofmt",

// These are internal cmd dependencies. Copy them.
// These are internal dependencies. Copy them.
"internal/execabs",
"cmd/internal/diff",
)
if err != nil {
Expand All @@ -32,8 +34,11 @@ func main() {
if pkg.ImportPath == "cmd/gofmt" {
copyGofmt(pkg)
} else {
parts := strings.Split(pkg.ImportPath, "/")
copyInternal(pkg, filepath.Join(parts[1:]...))
dir := pkg.ImportPath
if strings.HasPrefix(dir, "cmd/internal") {
dir = dir[len("cmd/"):]
}
copyInternal(pkg, filepath.FromSlash(dir))
}
}
}
Expand Down Expand Up @@ -280,5 +285,9 @@ func fixImports(body string) string {
"cmd/internal/",
"mvdan.cc/gofumpt/internal/",
-1)
body = strings.Replace(body,
`"internal/`,
`"mvdan.cc/gofumpt/internal/`,
-1)
return body
}
18 changes: 9 additions & 9 deletions gofmt.go
Expand Up @@ -14,7 +14,7 @@ import (
"go/scanner"
"go/token"
"io"
"io/ioutil"
"io/fs"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -75,15 +75,15 @@ func initParserMode() {
}
}

func isGoFile(f os.FileInfo) bool {
func isGoFile(f fs.DirEntry) bool {
// ignore non-Go files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}

// If in == nil, the source is the contents of the file with the given filename.
func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error {
var perm os.FileMode = 0o644
var perm fs.FileMode = 0o644
if in == nil {
f, err := os.Open(filename)
if err != nil {
Expand All @@ -98,7 +98,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
perm = fi.Mode().Perm()
}

src, err := ioutil.ReadAll(in)
src, err := io.ReadAll(in)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
if err != nil {
return err
}
err = ioutil.WriteFile(filename, res, perm)
err = os.WriteFile(filename, res, perm)
if err != nil {
os.Rename(bakname, filename)
return err
Expand All @@ -180,7 +180,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
return err
}

func visitFile(path string, f os.FileInfo, err error) error {
func visitFile(path string, f fs.DirEntry, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, nil, os.Stdout, false)
}
Expand All @@ -193,7 +193,7 @@ func visitFile(path string, f os.FileInfo, err error) error {
}

func walkDir(path string) {
filepath.Walk(path, visitFile)
filepath.WalkDir(path, visitFile)
}

func main() {
Expand Down Expand Up @@ -298,9 +298,9 @@ const chmodSupported = runtime.GOOS != "windows"
// backupFile writes data to a new file named filename<number> with permissions perm,
// with <number randomly chosen such that the file name is unique. backupFile returns
// the chosen file name.
func backupFile(filename string, data []byte, perm os.FileMode) (string, error) {
func backupFile(filename string, data []byte, perm fs.FileMode) (string, error) {
// create backup file
f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename))
f, err := os.CreateTemp(filepath.Dir(filename), filepath.Base(filename))
if err != nil {
return "", err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/diff/diff.go
Expand Up @@ -9,8 +9,9 @@ package diff
import (
"io/ioutil"
"os"
"os/exec"
"runtime"

exec "mvdan.cc/gofumpt/internal/execabs"
)

// Returns diff of two arrays of bytes in diff tool format.
Expand Down
69 changes: 69 additions & 0 deletions internal/execabs/execabs.go
@@ -0,0 +1,69 @@
// Copyright 2021 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.

// Package execabs is a drop-in replacement for os/exec
// that requires PATH lookups to find absolute paths.
// That is, execabs.Command("cmd") runs the same PATH lookup
// as exec.Command("cmd"), but if the result is a path
// which is relative, the Run and Start methods will report
// an error instead of running the executable.
package execabs

import (
"context"
"fmt"
"os/exec"
"path/filepath"
"reflect"
"unsafe"
)

var ErrNotFound = exec.ErrNotFound

type (
Cmd = exec.Cmd
Error = exec.Error
ExitError = exec.ExitError
)

func relError(file, path string) error {
return fmt.Errorf("%s resolves to executable relative to current directory (.%c%s)", file, filepath.Separator, path)
}

func LookPath(file string) (string, error) {
path, err := exec.LookPath(file)
if err != nil {
return "", err
}
if filepath.Base(file) == file && !filepath.IsAbs(path) {
return "", relError(file, path)
}
return path, nil
}

func fixCmd(name string, cmd *exec.Cmd) {
if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) {
// exec.Command was called with a bare binary name and
// exec.LookPath returned a path which is not absolute.
// Set cmd.lookPathErr and clear cmd.Path so that it
// cannot be run.
lookPathErr := (*error)(unsafe.Pointer(reflect.ValueOf(cmd).Elem().FieldByName("lookPathErr").Addr().Pointer()))
if *lookPathErr == nil {
*lookPathErr = relError(name, cmd.Path)
}
cmd.Path = ""
}
}

func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, name, arg...)
fixCmd(name, cmd)
return cmd
}

func Command(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
fixCmd(name, cmd)
return cmd
}

0 comments on commit 1af1350

Please sign in to comment.