{"record":{"id":"aa557f8b2c8053fb","repo":"microsoft/garnet","slug":"filepath-is-required-for-disk-backed-trees","errorCode":null,"errorMessage":"filePath is required for disk-backed trees.","messagePattern":"filePath is required for disk-backed trees\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"libs/native/bftree-garnet/BfTreeService.cs","lineNumber":161,"sourceCode":"        /// <param name=\"cbSizeByte\">Circular buffer size in bytes (hot-data cache for Disk; total capacity for Memory).</param>\n        /// <param name=\"cbMinRecordSize\">Minimum record size.</param>\n        /// <param name=\"cbMaxRecordSize\">Maximum record size.</param>\n        /// <param name=\"cbMaxKeyLen\">Maximum key length.</param>\n        /// <param name=\"leafPageSize\">Leaf page size.</param>\n        public BfTreeService(\n            StorageBackendType storageBackend = StorageBackendType.Disk,\n            string filePath = null,\n            bool enableSnapshots = false,\n            ulong cbSizeByte = 0,\n            uint cbMinRecordSize = 0,\n            uint cbMaxRecordSize = 0,\n            uint cbMaxKeyLen = 0,\n            uint leafPageSize = 0)\n        {\n            _storageBackend = storageBackend;\n            _filePath = filePath;\n            if (storageBackend == StorageBackendType.Disk && string.IsNullOrEmpty(filePath))\n                throw new ArgumentException(\"filePath is required for disk-backed trees.\", nameof(filePath));\n            byte[] pathBytes = filePath != null ? Encoding.UTF8.GetBytes(filePath) : null;\n            byte useSnapshot = (byte)(enableSnapshots ? 1 : 0);\n            fixed (byte* pp = pathBytes)\n            {\n                _tree = NativeBfTreeMethods.bftree_create(\n                    cbSizeByte, cbMinRecordSize, cbMaxRecordSize, cbMaxKeyLen, leafPageSize,\n                    (byte)storageBackend, pp, pathBytes?.Length ?? 0,\n                    useSnapshot);\n            }\n            if (_tree == 0)\n                throw new InvalidOperationException(\"Failed to create BfTree instance.\");\n        }\n\n        /// <summary>\n        /// Creates a BfTreeService wrapping an existing native tree pointer (e.g. from snapshot restore).\n        /// Takes ownership of the pointer.\n        /// </summary>\n        internal BfTreeService(nint treePtr, StorageBackendType storageBackend, string filePath = null)","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/native/bftree-garnet/BfTreeService.cs#L143-L179","documentation":"Thrown by the BfTreeService constructor when StorageBackendType.Disk is selected but no filePath is provided. Disk-backed BfTree instances require a file path to persist data; without one, the native layer cannot initialize the storage backend. The ArgumentException includes the parameter name 'filePath' for diagnostics.","triggerScenarios":"Creating a BfTreeService with storageBackend=StorageBackendType.Disk and filePath=null or empty string. The check is at BfTreeService.cs:160.","commonSituations":"Programmatically constructing a BfTreeService without specifying a file path when disk storage is intended; config or code path that defaults filePath to null but sets the backend to Disk; forgetting to pass the filePath argument when calling the constructor.","solutions":["Provide a valid file path when using StorageBackendType.Disk.","If you don't need disk persistence, use StorageBackendType.Memory or the appropriate non-disk backend.","Validate the filePath parameter before constructing the BfTreeService."],"exampleFix":"// before\nvar svc = new BfTreeService(\n    storageBackend: StorageBackendType.Disk,\n    filePath: null  // missing\n);\n\n// after\nvar svc = new BfTreeService(\n    storageBackend: StorageBackendType.Disk,\n    filePath: \"/var/lib/garnet/bftree.dat\"\n);","handlingStrategy":"validation","validationCode":"if (storageBackend == StorageBackendType.Disk && string.IsNullOrWhiteSpace(filePath))\n    throw new ArgumentException(\"A file path is required when using disk-backed BfTree storage.\", nameof(filePath));","typeGuard":"static bool IsBfTreeConfigValid(StorageBackendType backend, string filePath) =>\n    backend != StorageBackendType.Disk || !string.IsNullOrWhiteSpace(filePath);","tryCatchPattern":null,"preventionTips":["Always provide a non-empty filePath when using StorageBackendType.Disk.","Validate constructor arguments before calling BfTreeService.","Use a factory method that enforces the filePath requirement for disk backends."],"tags":["bftree","native-interop","storage","argument-validation"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}