{"record":{"id":"fffcdf283370091d","repo":"microsoft/aspire","slug":"cannot-derive-a-kubernetes-namespace-from-resource-name","errorCode":null,"errorMessage":"Cannot derive a Kubernetes namespace from resource name '{chart.Name}'. Set an explicit namespace via WithNamespace(...). {ex.Message}","messagePattern":"Cannot derive a Kubernetes namespace from resource name '(.+?)'\\. Set an explicit namespace via WithNamespace\\(\\.\\.\\.\\)\\. (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Kubernetes/KubernetesHelmChartExtensions.cs","lineNumber":498,"sourceCode":"            }\n            catch (ArgumentException ex)\n            {\n                throw new InvalidOperationException(\n                    $\"Cannot derive a Helm release name from resource name '{chart.Name}'. \" +\n                    $\"Set an explicit release name via WithReleaseName(...). {ex.Message}\",\n                    ex);\n            }\n        }\n\n        if (chart.Namespace is null)\n        {\n            try\n            {\n                HelmChartOptions.ValidateNamespace(@namespace, nameof(chart.Namespace));\n            }\n            catch (ArgumentException ex)\n            {\n                throw new InvalidOperationException(\n                    $\"Cannot derive a Kubernetes namespace from resource name '{chart.Name}'. \" +\n                    $\"Set an explicit namespace via WithNamespace(...). {ex.Message}\",\n                    ex);\n            }\n        }\n\n        return (releaseName, @namespace);\n    }\n\n    private static string GetStateSectionName(KubernetesEnvironmentResource environment, KubernetesHelmChartResource chart)\n        => $\"HelmChart:{environment.Name}:{chart.Name}\";\n\n    // Allowlist for Helm chart references. Covers OCI URLs (oci://host/path), HTTP/HTTPS URLs,\n    // local paths, plain chart names (\"repo/chart\"), and packaged chart filenames. Rejects anything\n    // that could break helm argument tokenization (whitespace, quotes, control chars).\n    [GeneratedRegex(@\"^[A-Za-z0-9_./:@+~\\-]+$\")]\n    private static partial Regex ChartReferencePattern();\n","sourceCodeStart":480,"sourceCodeEnd":516,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Kubernetes/KubernetesHelmChartExtensions.cs#L480-L516","documentation":"When no explicit namespace is set, the chart's resource name is used as the Kubernetes namespace and validated with HelmChartOptions.ValidateNamespace. If it fails namespace rules (RFC 1123 label: lowercase alphanumerics and '-', max 63 chars), the ArgumentException is wrapped in this InvalidOperationException advising WithNamespace.","triggerScenarios":"Calling AddHelmChart with a resource name that is not a valid Kubernetes namespace (uppercase, underscores, dots, or over 63 characters) without calling WithNamespace.","commonSituations":"CamelCase or underscored C# resource names used directly as namespaces; resource names containing dots.","solutions":["Set an explicit namespace via WithNamespace(...) with an RFC 1123-compliant label","Rename the resource to lowercase alphanumerics/hyphens within 63 characters","Read the embedded ex.Message for the exact rule violated"],"exampleFix":"// before\nenv.AddHelmChart(\"MyChart_v2\", ...); // name not a valid namespace\n// after\nenv.AddHelmChart(\"MyChart_v2\", ...)\n   .WithNamespace(\"mychart\");","handlingStrategy":"validation","validationCode":"// RFC 1123 label check (max 63 chars)\nbool IsValidNamespace(string name) =>\n    name.Length <= 63 && Regex.IsMatch(name, \"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$\");","typeGuard":null,"tryCatchPattern":"try { env.AddHelmChart(chartName, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot derive a Kubernetes namespace\"))\n{ logger.LogError(ex, \"Set WithNamespace(...) for resource '{Name}'.\", chartName); }","preventionTips":["Call WithNamespace explicitly instead of relying on the resource name","Use lowercase alphanumeric/hyphen names of 63 chars or fewer","Avoid dots, underscores, and uppercase letters in resource names used as namespaces"],"tags":["kubernetes","helm","naming","namespace"],"backgroundTag":"invalid-identifier-format","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}