Skip to content

Commit

Permalink
Return false if there are no existent inputs to GenerateResource (dot…
Browse files Browse the repository at this point in the history
…net#9208)

Fixes dotnet#9199 by avoiding an unhandled exception:

```
"C:\temp\testgenres\project.proj" (default target) (1) ->
  MSBUILD : error : This is an unhandled exception in MSBuild -- PLEASE UPVOTE AN EXISTING ISSUE OR FILE A NEW ONE AT https://aka.ms/msbuild/unhandled [C:\temp\testgenres\project.proj]
MSBUILD : error :     Microsoft.Build.Framework.InternalErrorException: MSB0001: Internal MSBuild Error: There should be resource files to process [C:\temp\testgenres\project.proj]
MSBUILD : error :    at Microsoft.Build.Shared.ErrorUtilities.ThrowInternalError(String message, Exception innerException, Object[] args) [C:\temp\testgenres\project.proj]
MSBUILD : error :    at Microsoft.Build.Tasks.GenerateResource.TransformResourceFilesUsingResGen(List`1 inputsToProcess, List`1 outputsToProcess) [C:\temp\testgenres\project.proj]
MSBUILD : error :    at Microsoft.Build.Tasks.GenerateResource.GenerateResourcesUsingResGen(List`1 inputsToProcess, List`1 outputsToProcess) [C:\temp\testgenres\project.proj]
MSBUILD : error :    at Microsoft.Build.Tasks.GenerateResource.Execute() [C:\temp\testgenres\project.proj]
```
  • Loading branch information
JaynieBai authored Sep 27, 2023
1 parent 26a4fc6 commit ba2883e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
23 changes: 19 additions & 4 deletions src/Tasks.UnitTests/ResourceHandling/GenerateResource_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Tasks;
using Microsoft.Build.UnitTests.Shared;
using Microsoft.Build.Utilities;
using Shouldly;
using Xunit;
Expand Down Expand Up @@ -338,7 +339,7 @@ public void ForceOutOfDate()
/* Unmerged change from project 'Microsoft.Build.Tasks.UnitTests (net7.0)'
Before:
Utilities.AssertLogContainsResource(t, "GenerateResource.OutputDoesntExist", t.OutputResources[0].ItemSpec);
Utilities.AssertStateFileWasWritten(t);
After:
Utilities.AssertLogContainsResource(t, "GenerateResource.OutputDoesntExist", t.OutputResources[0].ItemSpec);
Expand Down Expand Up @@ -1718,7 +1719,7 @@ public void FailedResXReader()
/* Unmerged change from project 'Microsoft.Build.Tasks.UnitTests (net7.0)'
Before:
Assert.False(success);
Utilities.AssertStateFileWasWritten(t);
After:
Assert.False(success);
Expand Down Expand Up @@ -1797,7 +1798,7 @@ public void FailedResXReaderWithAllOutputResourcesSpecified()
/* Unmerged change from project 'Microsoft.Build.Tasks.UnitTests (net7.0)'
Before:
Assert.False(success);
Utilities.AssertStateFileWasWritten(t);
After:
Assert.False(success);
Expand Down Expand Up @@ -2317,7 +2318,7 @@ public void OutputFilesPartialInputs()
/* Unmerged change from project 'Microsoft.Build.Tasks.UnitTests (net7.0)'
Before:
Assert.Equal(t.FilesWritten[2].ItemSpec, Path.ChangeExtension(t.Sources[3].ItemSpec, ".resources"));
Utilities.AssertStateFileWasWritten(t);
After:
Assert.Equal(t.FilesWritten[2].ItemSpec, Path.ChangeExtension(t.Sources[3].ItemSpec, ".resources"));
Expand Down Expand Up @@ -3653,6 +3654,20 @@ GenerateResource ExecuteTask()
Utilities.FileUpdated(resourcesFile, initialWriteTime).ShouldBeFalse();
}
}

/// <summary>
/// https://github.com/dotnet/msbuild/issues/9199
/// </summary>
[Fact]
public void NotValidSources()
{
GenerateResource t = new GenerateResource { BuildEngine = new MockEngine(_output) };
t.Sources = new ITaskItem[] { new TaskItem("non-existent") };
t.OutputResources = new ITaskItem[] { new TaskItem("out") };
Assert.False(t.Execute());
((MockEngine)t.BuildEngine).AssertLogContains("MSB3552");
Assert.Equal(1, ((MockEngine)t.BuildEngine).Errors);
}
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/Tasks/GenerateResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,22 @@ public override bool Execute()

GetResourcesToProcess(out inputsToProcess, out outputsToProcess, out cachedOutputFiles);

if (inputsToProcess.Count == 0 && !Log.HasLoggedErrors)
if (inputsToProcess.Count == 0)
{
if (cachedOutputFiles.Count > 0)
if (!Log.HasLoggedErrors)
{
OutputResources = cachedOutputFiles.ToArray();
}
if (cachedOutputFiles.Count > 0)
{
OutputResources = cachedOutputFiles.ToArray();
}

Log.LogMessageFromResources("GenerateResource.NothingOutOfDate");
Log.LogMessageFromResources("GenerateResource.NothingOutOfDate");
}
else
{
// No valid sources found--failures should have been logged in GetResourcesToProcess
return false;
}
}
else if (FailIfNotIncremental)
{
Expand Down

0 comments on commit ba2883e

Please sign in to comment.