{"record":{"id":"1efb40e95375c366","repo":"microsoft/aspire","slug":"object-reference-not-passed-for-parameter-resource","errorCode":null,"errorMessage":"Object reference not passed for parameter 'resource' (ArgumentNullException: resource)","messagePattern":"Object reference not passed for parameter 'resource' \\(ArgumentNullException: resource\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Rust/RustCargoArgsCallbackAnnotation.cs","lineNumber":56,"sourceCode":"/// <param name=\"args\">The command-line arguments collection.</param>\n/// <param name=\"cancellationToken\">The cancellation token associated with this callback context.</param>\n/// <remarks>\n/// Unlike program arguments, cargo arguments are plain strings rather than <see cref=\"object\"/>.\n/// They select build behaviour (<c>--release</c>, <c>--features</c>, <c>--bin</c>) before the program\n/// starts, so there is nothing for a deferred value such as an endpoint reference to resolve against;\n/// those belong after the <c>--</c> separator and are added with <c>WithArgs</c>.\n/// </remarks>\npublic sealed class RustCargoArgsCallbackContext(RustAppResource resource, IList<string> args, CancellationToken cancellationToken = default)\n{\n    /// <summary>\n    /// Gets the Rust application resource whose cargo arguments are being built.\n    /// </summary>\n    /// <remarks>\n    /// The same callbacks run for both the local <c>cargo run</c> command line and the generated\n    /// Dockerfile, so a callback that needs to know which resource it is configuring — or that needs to\n    /// read annotations placed on it — reads them from here rather than capturing the resource itself.\n    /// </remarks>\n    public RustAppResource Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));\n\n    /// <summary>\n    /// Gets the list of command-line arguments.\n    /// </summary>\n    public IList<string> Args { get; } = args ?? throw new ArgumentNullException(nameof(args));\n\n    /// <summary>\n    /// Gets the cancellation token associated with the callback context.\n    /// </summary>\n    public CancellationToken CancellationToken { get; } = cancellationToken;\n}\n","sourceCodeStart":38,"sourceCodeEnd":68,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Rust/RustCargoArgsCallbackAnnotation.cs#L38-L68","documentation":"RustCargoArgsCallbackContext's constructor guards its 'resource' parameter with 'resource ?? throw new ArgumentNullException(nameof(resource))'. The context must always reference the RustAppResource whose cargo arguments are being built, since callbacks read resource annotations and identity from it. A null resource indicates a programming error in whoever constructs the context, so the library fails fast at construction.","triggerScenarios":"Constructing RustCargoArgsCallbackContext directly (e.g. in a custom argument pipeline or a unit test harness) and passing null for the resource parameter, such as an uninitialized RustAppResource field or a lookup that returned null.","commonSituations":"Test code building callback contexts manually; a custom resource builder returning null on a failed lookup; copying sample code where the resource variable was renamed and lost its assignment.","solutions":["Pass the actual RustAppResource instance being configured into the context constructor","Fix the preceding lookup/builder so it returns a real resource instead of null","Assert the resource is non-null before constructing the context in test harnesses"],"exampleFix":"// before\nvar context = new RustCargoArgsCallbackContext(resource: null, args, cancellationToken);\n// after\nvar resource = builder.Resource as RustAppResource ?? throw new InvalidOperationException(\"Not a Rust app resource\");\nvar context = new RustCargoArgsCallbackContext(resource, args, cancellationToken);","handlingStrategy":"type-guard","validationCode":"if (resource is null) throw new InvalidOperationException(\"Callback context requires a non-null RustAppResource.\");","typeGuard":"bool IsRustAppResource(object? r) => r is RustAppResource;","tryCatchPattern":"try { var ctx = new RustCargoArgsCallbackContext(resource, args, ct); } catch (ArgumentNullException ex) when (ex.ParamName == \"resource\") { log.LogError(ex, \"Null resource for callback context\"); }","preventionTips":["Cast and check builder.Resource before constructing the context","In tests, build the RustAppResource fixture before the context","Fix lookup helpers to throw meaningful errors instead of returning null"],"tags":["argument-null","rust","callback-context"],"backgroundTag":"null-argument","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"}