imports: fix reading from stdin on Windows
Don't panic when reading from stdin on Windows. This is a regression from https://golang.org/cl/43454 Also fix some weird behavior with stdin processing I noticed during reviewing the code: don't allow the -w (write) flag, and adust the filename shown with the -d (diff) flag. Fixes golang/go#20941 Change-Id: I73d0a1dc74c919238a3bb72823585bbf1b7daba1 Reviewed-on: https://go-review.googlesource.com/47810 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Jones <rbjones@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
72ed06fbe2
commit
6e57528ade
|
@ -144,12 +144,19 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
|
|||
fmt.Fprintln(out, filename)
|
||||
}
|
||||
if *write {
|
||||
if argType == fromStdin {
|
||||
// filename is "<standard input>"
|
||||
return errors.New("can't use -w on stdin")
|
||||
}
|
||||
err = ioutil.WriteFile(filename, res, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if *doDiff {
|
||||
if argType == fromStdin {
|
||||
filename = "stdin.go" // because <standard input>.orig looks silly
|
||||
}
|
||||
data, err := diff(src, res, filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("computing diff: %s", err)
|
||||
|
|
|
@ -254,10 +254,12 @@ func fixImports(fset *token.FileSet, f *ast.File, filename string) (added []stri
|
|||
results := make(chan result)
|
||||
for pkgName, symbols := range refs {
|
||||
go func(pkgName string, symbols map[string]bool) {
|
||||
sibling := packageInfo.Imports[pkgName]
|
||||
if sibling.Path != "" {
|
||||
results <- result{ipath: sibling.Path, name: sibling.Alias}
|
||||
return
|
||||
if packageInfo != nil {
|
||||
sibling := packageInfo.Imports[pkgName]
|
||||
if sibling.Path != "" {
|
||||
results <- result{ipath: sibling.Path, name: sibling.Alias}
|
||||
return
|
||||
}
|
||||
}
|
||||
ipath, rename, err := findImport(pkgName, symbols, filename)
|
||||
r := result{ipath: ipath, err: err}
|
||||
|
|
|
@ -1803,3 +1803,14 @@ func TestShouldTraverse(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 20941: this used to panic on Windows.
|
||||
func TestProcessStdin(t *testing.T) {
|
||||
got, err := Process("<standard input>", []byte("package main\nfunc main() {\n\tfmt.Println(123)\n}\n"), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(got), `"fmt"`) {
|
||||
t.Errorf("expected fmt import; got: %s", got)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue