crowdsecurity/crowdsec · error

while creating directories for %s: %w

Error message

while creating directories for %s: %w

What it means

After marshaling, DumpContextConfigFile creates the parent directory of ConsoleContextPath with os.MkdirAll (mode 0700). Failure (wrapped as 'while creating directories for %s: %w') means the directory tree could not be created, typically a permission problem.

Source

Thrown at pkg/csconfig/crowdsec_service.go:173

		c.Crowdsec.OutputRoutinesCount = 1
	}

	if err = c.LoadAPIClient(); err != nil {
		return fmt.Errorf("loading api client: %w", err)
	}

	return nil
}

func (c *CrowdsecServiceCfg) DumpContextConfigFile() error {
	// XXX: MakeDirs
	out, err := yaml.Marshal(c.ContextToSend)
	if err != nil {
		return fmt.Errorf("while serializing ConsoleConfig (for %s): %w", c.ConsoleContextPath, err)
	}

	if err = os.MkdirAll(filepath.Dir(c.ConsoleContextPath), 0o700); err != nil {
		return fmt.Errorf("while creating directories for %s: %w", c.ConsoleContextPath, err)
	}

	if err := os.WriteFile(c.ConsoleContextPath, out, 0o600); err != nil {
		return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleContextPath, err)
	}

	log.Infof("%s file saved", c.ConsoleContextPath)

	return nil
}

View on GitHub (pinned to 909b515798)

Solutions

  1. Check ownership/permissions of the target directory tree; run crowdsec as the user that owns it, or chown the config dir
  2. Ensure no regular file exists at any prefix of ConsoleContextPath's directory path
  3. Set a ConsoleContextPath in a writable location

Example fix

// before
sudo -u nobody ./crowdsec -c /root/dev.yaml  # mkdir denied
// after
chown -R crowdsec:crowdsec /etc/crowdsec && systemctl start crowdsec
Defensive patterns

Strategy: try-catch

Validate before calling

dir := filepath.Dir(consoleContextPath); if info, err := os.Stat(dir); err == nil && !info.IsDir() { log.Fatalf("%s exists and is not a directory", dir) }

Try / catch

if err := cfg.Crowdsec.DumpContextConfigFile(); err != nil { if strings.Contains(err.Error(), "creating directories") { log.Fatalf("cannot create context dir (permissions?): %v", err) } }

Prevention

When it happens

Trigger: ConsoleContextPath's parent directory is inside a non-writable location (e.g. /etc/crowdsec owned by root while running as unprivileged user), or a component of the path exists as a regular file.

Common situations: Running crowdsec manually as non-root against a root-owned config dir; Docker volume permission mismatch; a file where a directory is expected (e.g. /etc/crowdsec/console/context.d is a file).

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 crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/751d006625fcfeb6. Report an issue: GitHub.