FindUnder task throws NullReferenceException

Feb 2, 2011 at 2:25 AM

Hi, I'm learning MSBuild, which is going ok, but I'm having trouble using the MSBuild.ExtensionPack.Filesystem.FindUnder task. Perhaps I haven't quite got the hang of properties and items yet so I'd like to ask for help please.

I'm trying to write a project that will clean up folders that have a certain naming pattern and are older than a few days. The FindUnder task helps me find them, but I get a NullReferenceException if I try to pass a value into the ModifiedBeforeDate parameter. If I provide a non-date string like "Pete" then the error message says (correctly) that this is an invalid value for ModifiedBeforeDate. If I provide an empty string then the task works as though I had not specified that parameter, which is also correct. If I supply a string with a valid date, then the code fails with the exception.

I'm using MSBuild 4.0, and MSBuild Extension Pack 4.0.2.0. Here's my code:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
         ToolsVersion="4.0" 
         DefaultTargets="CleanHotcopies">

   <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>
            
   <PropertyGroup>
      <SvnRepositoriesHotCopyFolder>C:\RepositoriesHotCopy\</SvnRepositoriesHotCopyFolder>
   </PropertyGroup>
   
   <UsingTask 
      TaskName="GetDateOffsetFromToday"
      TaskFactory="CodeTaskFactory"
      AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
      <ParameterGroup>
         <DaysOffset ParameterType="System.Int32" Required="true" />
         <OffsetDate ParameterType="System.DateTime" Output="true" />
      </ParameterGroup>
      <Task>
         <Code Type="Fragment" Language="cs">
            <![CDATA[
              OffsetDate = DateTime.Now.AddDays(DaysOffset);
           ]]>
         </Code>
      </Task>      
   </UsingTask>
   
   <!-- Cleans out folders with the HC-*-HC pattern, whose modified date is before the expiry date. -->
   <Target Name="CleanHotcopies">
      <PropertyGroup>
         <DaysExpiry>-3</DaysExpiry>
      </PropertyGroup>
      <GetDateOffsetFromToday DaysOffset="$(DaysExpiry)">
         <Output TaskParameter="OffsetDate" PropertyName="ExpiredDate"/>
      </GetDateOffsetFromToday>

      <Message Text="ExpiredDate=$(ExpiredDate)"/>
      <MSBuild.ExtensionPack.Filesystem.FindUnder 
         TaskAction="FindDirectories"
         Path="$(SvnRepositoriesHotCopyFolder)"
         SearchPattern="HC-*-HC"
         ModifiedBeforeDate="$(ExpiredDate)" >
         <Output TaskParameter="FoundItems" ItemName="ExpiredFolders"/>
      </MSBuild.ExtensionPack.Filesystem.FindUnder>
      
      <Message Text="Purging. Deleting these folders, expired after @(ExpiredDate): @(ExpiredFolders)"/>
	  <Message Text='To become task RemoveDir Directories="%(ExpiredFolders.FullPath)"'/> 
   </Target>
</Project>

Here's an example of the folders it is working on. If my code was working, I'd expect it to match the first two folders, but not the third, because that was modified more recently than 3 days ago.

C:\Temp\Try>dir C:\RepositoriesHotCopy\
 Volume in drive C has no label.
 Volume Serial Number is 901D-EE74

 Directory of C:\RepositoriesHotCopy

31/01/2011  04:09 p.m.    <DIR>          .
31/01/2011  04:09 p.m.    <DIR>          ..
21/01/2011  08:48 a.m.    <DIR>          HC-2011-01-21-HC
25/01/2011  03:45 p.m.    <DIR>          HC-2011-01-25-HC
31/01/2011  04:09 p.m.    <DIR>          HC-2011-01-31-16-09-HC
               0 File(s)              0 bytes
               5 Dir(s)  23,646,228,480 bytes free

C:\Temp\Try> 

I think my property ExpiredDate is receiving a valid value, but the FindUnder task fails. Here is the output:

 

C:\Temp\Try>msbuild CleanHotcopies2.msbuild.proj /v:d
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 2/02/2011 3:05:47 p.m..
Project "C:\Temp\Try\CleanHotcopies2.msbuild.proj" on node 1 (default targets).
Building with tools version "4.0".
Target "CleanHotcopies" in project "C:\Temp\Try\CleanHotcopies2.msbuild.proj" (entry point):
Initializing task factory "CodeTaskFactory" from assembly "c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Buil
d.Tasks.v4.0.dll".
Using "GetDateOffsetFromToday" task from the task factory "Code Task Factory".
Task "GetDateOffsetFromToday"
Done executing task "GetDateOffsetFromToday".
Using "Message" task from assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f
7f11d50a3a".
Task "Message"
  ExpiredDate=01/30/2011 15:05:47
Done executing task "Message".
Using "MSBuild.ExtensionPack.Filesystem.FindUnder" task from assembly "C:\Program Files (x86)\MSBuild\ExtensionPack\4.0
\MSBuild.ExtensionPack.dll".
Task "MSBuild.ExtensionPack.Filesystem.FindUnder"
  Searching under path [C:\RepositoriesHotCopy\]
C:\Temp\Try\CleanHotcopies2.msbuild.proj(39,7): error : NullReferenceException: Object reference not set to an instance
 of an object.\r
C:\Temp\Try\CleanHotcopies2.msbuild.proj(39,7): error :
Done executing task "MSBuild.ExtensionPack.Filesystem.FindUnder" -- FAILED.
Done building target "CleanHotcopies" in project "CleanHotcopies2.msbuild.proj" -- FAILED.
Done Building Project "C:\Temp\Try\CleanHotcopies2.msbuild.proj" (default targets) -- FAILED.


Build FAILED.

"C:\Temp\Try\CleanHotcopies2.msbuild.proj" (default target) (1) ->
(CleanHotcopies target) ->
  C:\Temp\Try\CleanHotcopies2.msbuild.proj(39,7): error : NullReferenceException: Object reference not set to an instan
ce of an object.\r
C:\Temp\Try\CleanHotcopies2.msbuild.proj(39,7): error :

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.56

 

What am I doing wrong?

Regards,
Pete

Coordinator
Feb 2, 2011 at 9:03 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Feb 2, 2011 at 9:11 AM

Hi Pete

Thanks for the detailed report. Unfortunately this is a bug. I've just uploaded a fix

You can download and compile the latest 4.0.3.0 code (See Developing with the latest source code) or use an inline task implementation.

Regards

Mike

Feb 3, 2011 at 4:17 AM

Thanks for your help Mike. My problem is solved. Thanks also for all the work you've put into the Extension Pack. It's really helpful.

Regards,
Pete