{"record":{"id":"2e0533f5a7b8c4e9","repo":"gofr-dev/gofr","slug":"failed-to-create-directory-q-w","errorCode":null,"errorMessage":"failed to create directory %q: %w","messagePattern":"failed to create directory %q: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/file/azure/storage_adapter.go","lineNumber":841,"sourceCode":"\treturn r.reader.Close()\n}\n\n// isDirectoryExistsError checks if the error indicates the directory already exists.\nfunc isDirectoryExistsError(err error) bool {\n\terrStr := err.Error()\n\n\treturn strings.Contains(errStr, \"already exists\") ||\n\t\tstrings.Contains(errStr, \"ShareAlreadyExists\") ||\n\t\tstrings.Contains(errStr, \"ResourceAlreadyExists\")\n}\n\n// createDirectoryLevel creates a single directory level and handles errors.\nfunc (s *storageAdapter) createDirectoryLevel(ctx context.Context, dirPath string) error {\n\tdirClient := s.shareClient.NewDirectoryClient(dirPath)\n\n\t_, err := dirClient.Create(ctx, nil)\n\tif err != nil && !isDirectoryExistsError(err) {\n\t\treturn fmt.Errorf(\"failed to create directory %q: %w\", dirPath, err)\n\t}\n\n\treturn nil\n}\n\n// ensureParentDirectories creates all parent directories for the given file path.\n// Azure File Storage requires explicit directory creation for directories to appear in listings.\n// This function ensures parent directories are created before file creation, matching\n// local filesystem behavior where os.MkdirAll is called automatically.\nfunc (s *storageAdapter) ensureParentDirectories(ctx context.Context, filePath string) error {\n\tif s.shareClient == nil {\n\t\treturn errAzureClientNotInitialized\n\t}\n\n\t// Extract parent directory path\n\tparentDir := getParentDir(filePath)\n\tif parentDir == \"\" {\n\t\treturn nil // File is in root, no parent directories needed","sourceCodeStart":823,"sourceCodeEnd":859,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/file/azure/storage_adapter.go#L823-L859","documentation":"A single directory level could not be created in the Azure File share, and the error was not the benign 'already exists' case (which isDirectoryExistsError filters out). It occurs during ensureParentDirectories when materializing parent folders for a file write.","triggerScenarios":"Uploading/writing a file whose parent directory path requires creation, and dirClient.Create fails with a real error: invalid directory name (illegal characters, too long, trailing spaces/dots), share missing, or insufficient permissions.","commonSituations":"Path components containing characters Azure Files rejects (e.g. \"dir\\\\sub\", \"a/b\" embedded in a level, names over 255 chars, reserved names like 'aux'); writing to a share that doesn't exist; SAS without create permission; conflicting file exists at the same path as the directory.","solutions":["Read the wrapped error's Azure code — ParentNotFound means the parent of this level doesn't exist (ensureParentDirectories should create levels in order; check path splitting).","Sanitize directory names: remove illegal characters (\\ / : * ? \" < > |), trailing dots/spaces, and reserved device names.","Verify the share exists (create it via shareClient.Create if needed) and credentials allow create.","Check for a file already existing at the same path as the intended directory (Conflict)."],"exampleFix":"// before\ndirPath := strings.Join(parts, \"/\") // \"a/b\" in one level\n// after\nfor _, part := range parts { // create one sanitized level at a time\n    part = sanitize(part)\n    if err := adapter.createDirectoryLevel(ctx, part); err != nil {\n        return err\n    }\n}","handlingStrategy":"try-catch","validationCode":"var invalidDirChars = regexp.MustCompile(`[\\\\/:*?\"<>|]`)\nfunc validDirName(name string) bool {\n    return name != \"\" && len(name) <= 255 &&\n        !invalidDirChars.MatchString(name) &&\n        !strings.HasSuffix(name, \".\") && !strings.HasSuffix(name, \" \") &&\n        !strings.EqualFold(name, \"aux\")\n}","typeGuard":null,"tryCatchPattern":"if err := store.Create(ctx, dir + \"/file.txt\"); err != nil {\n    if strings.Contains(err.Error(), \"failed to create directory\") {\n        // sanitize path levels or create share, then retry\n    }\n    return err\n}","preventionTips":["Sanitize every path segment (no \\ / : * ? \" < > |, trailing dots/spaces, reserved names).","Ensure the share exists before writing files with deep paths.","Create parent levels in order and tolerate 'already exists' responses (the adapter already does).","Check that no file occupies the same path as an intended directory."],"tags":["azure","storage","directory","mkdir"],"backgroundTag":"azure-create-directory-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}