Skip to content

os: RemoveAll no longer returns *os.PathError #30491

@colincross

Description

@colincross

What version of Go are you using (go version)?

$ go version
go version go1.12 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"

What did you do?

In go 1.11, os.RemoveAll returned an *os.PathError when it failed to remove something due to a permissions issue. While not documented in the godoc for os.RemoveAll, this behavior was useful to implement a retry loop that attempted to chown the failing path and then call os.RemoveAll again (for example, https://android.googlesource.com/platform/build/soong/+/ef36053829238e24088c578eeac08a1693177757/ui/build/util.go#74). In go 1.12 os.RemoveAll returns a syscall.Errno instead, which doesn't tell the caller which path failed.

This test passes on go 1.11 but fails on go 1.12:

package main

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

func TestRemoveAllPathError(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		err := os.RemoveAll(tmpDir)
		if err != nil {
			t.Errorf("Error removing tmpDir: %v", err)
		}
	}()

	a := filepath.Join(tmpDir, "a")
	b := filepath.Join(a, "b")

	os.Mkdir(a, 0755)
	os.Mkdir(b, 0755)
	os.Chmod(a, 0555)
	defer os.Chmod(a, 0755)

	err = os.RemoveAll(a)
	if pathErr, ok := err.(*os.PathError); ok {
		if pathErr.Path != b {
			t.Errorf("expected pathErr.Path %q, was %q", b, pathErr.Path)
		}
	} else {
		t.Errorf("expected os.PathError, got %T", err)
	}
}

On go 1.11 this test passes, but fails on go 1.12:

--- FAIL: TestRemoveAllPathError (0.00s)
    removeall_test.go:36: expected os.PathError, got syscall.Errno

Should os.RemoveAll always return a *os.PathError?

Metadata

Metadata

Assignees

No one assigned

    Labels

    CherryPickCandidateUsed during the release process for point releasesFrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions