{"record":{"id":"890f0240ddbd48a1","repo":"microsoft/aspire","slug":"the-resource-builder-resource-name-does-not-have-a","errorCode":null,"errorMessage":"The resource '{builder.Resource.Name}' does not have a Dockerfile build annotation. Call WithDockerfile before calling WithBuildArg.","messagePattern":"The resource '(.+?)' does not have a Dockerfile build annotation\\. Call WithDockerfile before calling WithBuildArg\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ContainerResourceBuilderExtensions.cs","lineNumber":1155,"sourceCode":"    ///        .WithDockerfile(\"../mycontainer\")\n    ///        .WithBuildArg(\"CUSTOM_BRANDING\", \"/app/static/branding/custom\");\n    ///\n    /// builder.Build().Run();\n    /// </code>\n    /// </example>\n    /// </remarks>\n    /// <remarks>This method is not available in polyglot app hosts. Use the ATS dispatcher overload instead.</remarks>\n    [AspireExportIgnore(Reason = \"Uses object parameter which is not ATS-compatible.\")]\n    public static IResourceBuilder<T> WithBuildArg<T>(this IResourceBuilder<T> builder, string name, object? value) where T : ContainerResource\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(name);\n\n        var annotation = builder.Resource.Annotations.OfType<DockerfileBuildAnnotation>().SingleOrDefault();\n\n        if (annotation is null)\n        {\n            throw new InvalidOperationException($\"The resource '{builder.Resource.Name}' does not have a Dockerfile build annotation. Call WithDockerfile before calling WithBuildArg.\");\n        }\n\n        annotation.BuildArguments[name] = value;\n\n        return builder;\n    }\n\n    /// <summary>\n    /// Adds a build argument when the container is built from a Dockerfile.\n    /// </summary>\n    /// <typeparam name=\"T\">The type of container resource.</typeparam>\n    /// <param name=\"builder\">The resource builder for the container resource.</param>\n    /// <param name=\"name\">The name of the build argument.</param>\n    /// <param name=\"value\">The resource builder for a parameter resource.</param>\n    /// <returns>The <see cref=\"IResourceBuilder{T}\"/>.</returns>\n    /// <exception cref=\"InvalidOperationException\">\n    /// Thrown when <see cref=\"ContainerResourceBuilderExtensions.WithBuildArg{T}(IResourceBuilder{T}, string, IResourceBuilder{ParameterResource})\"/> is\n    /// called before <see cref=\"ContainerResourceBuilderExtensions.WithDockerfile{T}(IResourceBuilder{T}, string, string?, string?)\"/>.","sourceCodeStart":1137,"sourceCodeEnd":1173,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ContainerResourceBuilderExtensions.cs#L1137-L1173","documentation":"WithBuildArg adds a build argument to a resource's Dockerfile build annotation. If the resource has no DockerfileBuildAnnotation — i.e. WithDockerfile was never called — the library throws this InvalidOperationException telling you to configure the Dockerfile build first.","triggerScenarios":"Calling WithBuildArg (or WithBuildArg<T>) on a container resource that was created with WithImage only, or without WithDockerfile.","commonSituations":"Adding build args to a pre-built image resource; copy-pasting builder configuration between Dockerfile-built and image-pull resources; ordering mistakes in shared builder helpers.","solutions":["Call WithDockerfile(\"<contextPath>\") (optionally with a Dockerfile name) before WithBuildArg.","Remove WithBuildArg calls for resources that run a prebuilt image — build args only apply to builds.","For secrets during build, use WithBuildSecret instead of WithBuildArg after adding WithDockerfile.","exampleFix placeholder"],"exampleFix":"// before\nbuilder.AddContainer(\"app\", \"image\")\n    .WithBuildArg(\"CONFIG\", value);\n// after\nbuilder.AddContainer(\"app\", \"image\")\n    .WithDockerfile(\"./MyApp\")\n    .WithBuildArg(\"CONFIG\", value);","handlingStrategy":"validation","validationCode":"bool HasDockerfile<T>(IResourceBuilder<T> builder) where T : ContainerResource =>\n    builder.Resource.Annotations.OfType<DockerfileBuildAnnotation>().Any();\nif (!HasDockerfile(builder)) throw new InvalidOperationException(\"Call WithDockerfile before WithBuildArg.\");","typeGuard":"bool HasDockerfile<T>(IResourceBuilder<T> b) where T : ContainerResource =>\n    b.Resource.Annotations.OfType<DockerfileBuildAnnotation>().Any();","tryCatchPattern":"try\n{\n    resource.WithBuildArg(\"CONFIG\", value);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Dockerfile build annotation\"))\n{\n    // Call WithDockerfile first and retry\n}","preventionTips":["Only add build args to resources created with WithDockerfile.","Keep build-argument configuration adjacent to the WithDockerfile call in builder chains.","For image-pull resources, remove build args — they have no effect."],"tags":["dockerfile","build-args","aspire"],"backgroundTag":"invalid-state-transition","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}