Skip to content

Fix NullReferenceException in ValidateFile when bundles contain null files - #218

Draft
Shazwazza with Copilot wants to merge 4 commits into
developfrom
copilot/fix-nullreferenceexception-validatefile
Draft

Fix NullReferenceException in ValidateFile when bundles contain null files#218
Shazwazza with Copilot wants to merge 4 commits into
developfrom
copilot/fix-nullreferenceexception-validatefile

Conversation

Copilot AI commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

ValidateFile throws NullReferenceException when accessing file.Order if bundles contain null file references. This occurs intermittently, likely when application pool idles and bundle state becomes corrupted.

Changes

BundleFileSetGenerator.cs

  • Added null guard in ValidateFile before accessing file properties

BundleManager.cs

  • Filter null files in all Create() method overloads before bundle construction
  • Added null checks in AddToBundle() methods with warning logs
  • Optimized to avoid double enumeration using cached filtered lists

Tests

  • Added 8 tests covering null file handling in bundle creation and addition
  • Covers both standard and pipeline-enabled bundle creation paths

Approach

Three-layer defense:

  1. Filter nulls at bundle creation (preventive)
  2. Log warnings and skip nulls in AddToBundle (diagnostic)
  3. Throw ArgumentNullException in ValidateFile (fail-safe)

Example of the fix in action:

// Before: NullReferenceException when file is null
bundles.Create("my-bundle", file1, null, file2);  // Crashes later in ValidateFile

// After: Null files filtered, logged, and rejected gracefully
bundles.Create("my-bundle", file1, null, file2);  // Bundle created with 2 files, null logged as warning
Original prompt

This section details on the original issue you should resolve

<issue_title>NullReferenceException in ValidateFile</issue_title>
<issue_description>We had an issue this morning where our site stopped working with a NullReferenceException in the ValidateFile function. It was running yesterday and there have been no code / file changes since then (also no server updates or restarts). The ValidateFile function is pretty small so assuming that the file that it's passing in along the way is null. The bundle in question is a CSS bundle.

My gut is telling me this is something to do with the application pool going idle as a restart fixes the issue. Relevant stack trace extract below:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Smidge.BundleFileSetGenerator.ValidateFile(IWebFile file)
   at Smidge.BundleFileSetGenerator.GetOrderedFileSet(IEnumerable`1 files, PreProcessPipeline pipeline)
   at Smidge.BundleFileSetGenerator.GetOrderedFileSet(Bundle bundle, PreProcessPipeline pipeline)
   at Smidge.SmidgeHelper.GenerateBundleUrlsAsync(String bundleName, String fileExt, Boolean debug)
   at Smidge.TagHelpers.SmidgeLinkTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
   at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.<RunAsync>g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, Int32 i, Int32 count)

For reference, this is an Umbraco 9.5.0 site (.NET 5), running Smidge 4.0.3 hosted on Windows Server 2019, IIS 10.</issue_description>

Comments on the Issue (you are @copilot in this section)

@Shazwazza Is everyone reporting this issue occurring on your front-end or back office?

I'm trying to figure out how this could happen since it would mean that one of the file references for the bundle is null here

The only way this could happen is if that file reference is null when creating the bundle initially.

Can anyone provide an example of how they are creating the bundle that is causing this issue? It could also occur if using view based declarations and RequireCss/RequireJs is used and passing in a null file.

I can add some null checks to these methods to try to avoid this.</comment_new>
<comment_new>@Shazwazza
This is actually a duplicate of #205 </comment_new>
<comment_new>@Shazwazza
Have published version 4.5.0 https://github.com/Shazwazza/Smidge/releases/tag/v4.5.0 which eagerly adds null checks when creating a bundle with null files. Its not clear what the full underlying issue is but the file can only be null if it is registered as null when creating the bundle.</comment_new>
<comment_new>@Shazwazza
@benehudson are you using view based declarations?</comment_new>
<comment_new>@Shazwazza
@benehudson where do you use the taghelper? Is that within your TemplateMaster.cshtml or somewhere else? View based declarations can be problematic, see #198 (comment)</comment_new>
<comment_new>@Shazwazza
Yep, like this https://github.com/Shazwazza/Smidge?tab=readme-ov-file#pre-defined-bundles</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits January 29, 2026 17:03
Co-authored-by: Shazwazza <1742685+Shazwazza@users.noreply.github.com>
Co-authored-by: Shazwazza <1742685+Shazwazza@users.noreply.github.com>
Co-authored-by: Shazwazza <1742685+Shazwazza@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException in ValidateFile function Fix NullReferenceException in ValidateFile when bundles contain null files Jan 29, 2026
Copilot AI requested a review from Shazwazza January 29, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullReferenceException in ValidateFile

2 participants