{"record":{"id":"751d006625fcfeb6","repo":"crowdsecurity/crowdsec","slug":"while-creating-directories-for-s-w","errorCode":null,"errorMessage":"while creating directories for %s: %w","messagePattern":"while creating directories for (.+?): %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/csconfig/crowdsec_service.go","lineNumber":173,"sourceCode":"\t\tc.Crowdsec.OutputRoutinesCount = 1\n\t}\n\n\tif err = c.LoadAPIClient(); err != nil {\n\t\treturn fmt.Errorf(\"loading api client: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *CrowdsecServiceCfg) DumpContextConfigFile() error {\n\t// XXX: MakeDirs\n\tout, err := yaml.Marshal(c.ContextToSend)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"while serializing ConsoleConfig (for %s): %w\", c.ConsoleContextPath, err)\n\t}\n\n\tif err = os.MkdirAll(filepath.Dir(c.ConsoleContextPath), 0o700); err != nil {\n\t\treturn fmt.Errorf(\"while creating directories for %s: %w\", c.ConsoleContextPath, err)\n\t}\n\n\tif err := os.WriteFile(c.ConsoleContextPath, out, 0o600); err != nil {\n\t\treturn fmt.Errorf(\"while dumping console config to %s: %w\", c.ConsoleContextPath, err)\n\t}\n\n\tlog.Infof(\"%s file saved\", c.ConsoleContextPath)\n\n\treturn nil\n}\n","sourceCodeStart":155,"sourceCodeEnd":184,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/csconfig/crowdsec_service.go#L155-L184","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Check ownership/permissions of the target directory tree; run crowdsec as the user that owns it, or chown the config dir","Ensure no regular file exists at any prefix of ConsoleContextPath's directory path","Set a ConsoleContextPath in a writable location"],"exampleFix":"// before\nsudo -u nobody ./crowdsec -c /root/dev.yaml  # mkdir denied\n// after\nchown -R crowdsec:crowdsec /etc/crowdsec && systemctl start crowdsec","handlingStrategy":"try-catch","validationCode":"dir := filepath.Dir(consoleContextPath); if info, err := os.Stat(dir); err == nil && !info.IsDir() { log.Fatalf(\"%s exists and is not a directory\", dir) }","typeGuard":null,"tryCatchPattern":"if err := cfg.Crowdsec.DumpContextConfigFile(); err != nil { if strings.Contains(err.Error(), \"creating directories\") { log.Fatalf(\"cannot create context dir (permissions?): %v\", err) } }","preventionTips":["Ensure the crowdsec service user owns /etc/crowdsec (or its console subdirectory)","Don't place ConsoleContextPath under root-only paths when testing as non-root","Check for files shadowing expected directory names in the path"],"tags":["filesystem","permissions","mkdir"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}