Go version

go version go1.23.0 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/user/Library/Caches/go-build'
GOENV='/Users/user/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT='rangefunc'
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/user/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/user/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/4v/g652j2zn37lg22crrdwmcqdm0000gn/T/go-build1911013595=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

package main

import (
    "fmt"
    "os"
)

func main() {
    os.Mkdir("src", 0o777)
    os.WriteFile("src/file", ([]byte)("src"), 0o666)
    os.Mkdir("dest", 0o777)
    os.WriteFile("dest/file", ([]byte)("dest"), 0o666)
    os.CopyFS("dest", os.DirFS("src")) // dest/file should not be overwritten.
    file, _ := os.ReadFile("dest/file")
    fmt.Printf("%s", file)
}

The doc of os.CopyFS says: CopyFS will not overwrite existing files, and returns an error if a file name in fsys already exists in the destination.. So dest/file should not be overwritten.

What did you see happen?

src

What did you expect to see?

dest

Comment From: gabyhelp

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Comment From: seankhliao

cc @panjf2000

Comment From: seankhliao

https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/os/dir.go;l=179;drc=559c77592f182a2f77f2d70328cb649609517bd3

should O_TRUNC instead be O_EXCL ?

Comment From: panjf2000

See also https://github.com/golang/go/issues/62484#issuecomment-2246240691

Comment From: panjf2000

I missed that discussion at the time, but now as far as I'm concerned we should replace O_TRUNC with O_EXCL in order to match the new function comment. @neild @ianlancetaylor @rsc

Comment From: gopherbot

Change https://go.dev/cl/606095 mentions this issue: os: use O_EXCL instead of O_TRUNC in CopyFS to disallow rewriting existing files

Comment From: panjf2000

@gopherbot please create a backport to 1.23, it's a mismatch of doc and actual behavior.

Comment From: gopherbot

Backport issue(s) opened: #68907 (for 1.23).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.