Skip to content

os: document that OpenFile Create do not create file if directory do not exist #69836

@r4f4ss

Description

@r4f4ss

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

$ go version
go version go1.23.1 linux/amd64

Does this issue reproduce with the latest release?

I do not know

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

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='HOME/.cache/go-build'
GOENV='HOME/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='HOME/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='HOME/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='HOME/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='HOME/geth/goBug/go.mod'
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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build44258856=/tmp/go-build -gno-record-gcc-switches'
uname -sr: Linux 6.8.0-45-generic
Distributor ID:	Linuxmint
Description:	Linux Mint 22
Release:	22
Codename:	wilma
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Ubuntu GLIBC 2.39-0ubuntu8.3) stable release version 2.39.
gdb --version: GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git

What did you do?

Run the followeing two codes (./bugDir directory must do not exist):

$go run main.go
package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	dir := "./bugDir"
	fileName := "bugFile"
	fullPath := filepath.Join(dir, fileName)

	_, err := os.OpenFile(fullPath, os.O_CREATE|os.O_WRONLY, 0600)
	if err != nil {
		fmt.Println(err)
	}
}
package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	dir := "./bugDir"
	fileName := "bugFile"
	fullPath := filepath.Join(dir, fileName)

	_, err := os.Create(fullPath)
	if err != nil {
		fmt.Println(err)
	}
}

What did you expect to see?

The creation of the file bugFile, if the directory do not exist it should be created. This is my understanding of documentation described behavior (1,2), otherwise should be stated clearly in docs that the path must exist before file creation.

Note that the use of the flag O_CREATE suggest that the file will be created eitherways:

If the file does not exist, and the O_CREATE flag is passed, it is created with mode perm (before umask)

workarround: create the directory with os.MkdirAll(dir, os.ModePerm)

What did you see instead?

Error:

open bugDir/bugFile: no such file or directory

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.NeedsFixThe path to resolution is known, but the work has not been done.help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions