Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/cmd/go/testdata/script/gofmt_with_symlink.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Test that gofmt-ing a symlink works correctly. See #79735.

# Skip on systems without unix-style symlinks
[GOOS:windows] skip

cd $WORK/a
cp issue-original.go issue-expected.go
exec go fmt issue-expected.go
! cmp -q issue-expected.go issue-original.go

# Create that symlink.
exec ln -s issue-original.go issue-symlink.go

# Now go fmt it.
exec go fmt issue-symlink.go

# Ensure that the symlinked file is equal to the expected.
cmp issue-expected.go issue-symlink.go

# Our source code and setup.
-- $WORK/a/go.mod --
module gofmt_symlink

-- $WORK/a/issue-original.go --
package main;

import "fmt"

func main() {
fmt.Println( "Hello, world!!!" )
}
6 changes: 3 additions & 3 deletions src/cmd/gofmt/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func processFile(filename string, info fs.FileInfo, in io.Reader, r *reporter) e
}

perm := info.Mode().Perm()
if err := writeFile(filename, src, res, perm, info.Size()); err != nil {
if err := writeFile(filename, src, res, perm); err != nil {
return err
}
}
Expand Down Expand Up @@ -465,7 +465,7 @@ func fileWeight(path string, info fs.FileInfo) int64 {
}

// writeFile updates a file with the new formatted data.
func writeFile(filename string, orig, formatted []byte, perm fs.FileMode, size int64) error {
func writeFile(filename string, orig, formatted []byte, perm fs.FileMode) error {
// Make a temporary backup file before rewriting the original file.
bakname, err := backupFile(filename, orig, perm)
if err != nil {
Expand All @@ -489,7 +489,7 @@ func writeFile(filename string, orig, formatted []byte, perm fs.FileMode, size i
}

n, err := fout.Write(formatted)
if err == nil && int64(n) < size {
if err == nil {
err = fout.Truncate(int64(n))
}

Expand Down
8 changes: 7 additions & 1 deletion src/os/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,13 @@ func (dir dirFS) ReadLink(name string) (string, error) {
if err != nil {
return "", &PathError{Op: "readlink", Path: name, Err: err}
}
return Readlink(fullname)
f, err := Readlink(fullname)
if err != nil {
// See comment in dirFS.Open.
err.(*PathError).Path = name
return "", err
}
return f, nil
}

// join returns the path for name in dir.
Expand Down
26 changes: 26 additions & 0 deletions src/os/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ func TestDirFSReadLink(t *testing.T) {
t.Errorf("fs.ReadLink(fsys, %q) = %q, %v; want %q, <nil>", name, got, err, want)
}
}

const nonesuch = "dir/nonesuch"
_, err := fs.ReadLink(fsys, nonesuch)
if err == nil {
t.Fatal("fs.ReadLink of nonexistent file succeeded")
}
pe, ok := err.(*PathError)
if !ok {
t.Fatalf("fs.ReadLink error type = %T; want *PathError", err)
}
if pe.Path != nonesuch {
t.Errorf("fs.ReadLink(%q) error path = %q; want %q", nonesuch, pe.Path, nonesuch)
}
}

func TestDirFSLstat(t *testing.T) {
Expand Down Expand Up @@ -78,6 +91,19 @@ func TestDirFSLstat(t *testing.T) {
t.Errorf("fs.Lstat(fsys, %q).Mode().Type() = %v, %v; want %v, <nil>", name, got, err, want)
}
}

const nonesuch = "dir/nonesuch"
_, err := fs.Lstat(fsys, nonesuch)
if err == nil {
t.Fatal("fs.Lstat of nonexistent file succeeded")
}
pe, ok := err.(*PathError)
if !ok {
t.Fatalf("fs.Lstat error type = %T; want *PathError", err)
}
if pe.Path != nonesuch {
t.Errorf("fs.Lstat(%q) error path = %q; want %q", nonesuch, pe.Path, nonesuch)
}
}

func TestDirFSWalkDir(t *testing.T) {
Expand Down
Loading