apache/answer · error

create directory fail %s

Error message

create directory fail %s

What it means

InstallConfigFile returns this when dir.CreateDirIfNotExist(path.ConfigFileDir) fails while installing the default config file. The library cannot create the directory that will hold the config file, so installation aborts.

Source

Thrown at internal/cli/install.go:56

	InstallI18nBundle(false)
	fmt.Println("install all initial environment done")
}

func InstallConfigFile(configFilePath string) error {
	if len(configFilePath) == 0 {
		configFilePath = filepath.Join(path.ConfigFileDir, path.DefaultConfigFileName)
	}
	fmt.Println("[config-file] try to create at ", configFilePath)

	// if config file already exists do nothing.
	if CheckConfigFile(configFilePath) {
		fmt.Printf("[config-file] %s already exists\n", configFilePath)
		return nil
	}

	if err := dir.CreateDirIfNotExist(path.ConfigFileDir); err != nil {
		fmt.Printf("[config-file] create directory fail %s\n", err.Error())
		return fmt.Errorf("create directory fail %s", err.Error())
	}
	fmt.Printf("[config-file] create directory success, config file is %s\n", configFilePath)

	if err := writer.WriteFile(configFilePath, string(configs.Config)); err != nil {
		fmt.Printf("[config-file] install fail %s\n", err.Error())
		return fmt.Errorf("write file failed %s", err)
	}
	fmt.Printf("[config-file] install success\n")
	return nil
}

func installUploadDir() {
	fmt.Println("[upload-dir] try to install...")
	if err := dir.CreateDirIfNotExist(path.UploadFilePath); err != nil {
		fmt.Printf("[upload-dir] install fail %s\n", err.Error())
	} else {
		fmt.Printf("[upload-dir] install success, upload directory is %s\n", path.UploadFilePath)
	}

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Check and fix filesystem permissions on the parent of path.ConfigFileDir.
  2. Ensure the configured data dir path is a directory, not an existing file.
  3. Run with sufficient privileges or point the data dir to a writable location.

Example fix

// before
if err := dir.CreateDirIfNotExist(path.ConfigFileDir); err != nil {
    return fmt.Errorf("create directory fail %s", err.Error())
}
// after (pre-check by caller)
if _, statErr := os.Stat(dataDirPath); os.IsNotExist(statErr) {
    os.MkdirAll(dataDirPath, 0o755)
}
if err := dir.CreateDirIfNotExist(path.ConfigFileDir); err != nil {
    return fmt.Errorf("create directory fail %s", err.Error())
}
Defensive patterns

Strategy: validation

Validate before calling

if info, err := os.Stat(dataDirPath); err == nil && !info.IsDir() {
    return fmt.Errorf("%s is not a directory", dataDirPath)
}
if err := os.MkdirAll(path.ConfigFileDir, 0o755); err != nil {
    return fmt.Errorf("cannot create config dir: %w", err)
}

Prevention

When it happens

Trigger: InitEnvironment -> InstallConfigFile runs and the OS refuses to create path.ConfigFileDir (permission denied, path is a file, read-only filesystem).

Common situations: Running CLI as a user without write access to the data directory, dataDirPath pointing at an existing regular file, container volume mounted read-only.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


AI-assisted analysis of apache/answer@3b9f137061 (2026-09-05). Data as JSON: /api/errors/7afac2b9f3e07b1c. Report an issue: GitHub.