Skip to content

Commit b0f4dea

Browse files
dellis1972jonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] The "ConvertResourcesCases" task failed unexpectedly. (#1371)
Fixes? https://devdiv.visualstudio.com/DevDiv/_workitems/edit/571365 We have a report that `ConvertResourcesCases` is failing with the following error: error MSB4018: The "ConvertResourcesCases" task failed unexpectedly. Android/Xamarin.Android.Common.targets(1334,2): error MSB4018: System.IO.FileNotFoundException: <some path>/design_layout_snackbar.xml.tmp does not exist This is completely weird, especially as the Task is not being called concurrently (or so we hope & assume). What I suspect is happening is Visual Studio for Mac is running the `UpdateAndroidResources` target at the same time the user started a build. Because the name of the temp files was just `foo.xml.tmp` it is highly possible that one instance of the task is deleting the file while another instance is still running. This is difficult to replicate. Attempt to "fix" things by using `System.IO.Path.GetTempFileName()` instead of appending `.tmp` to the filename. This should avoid filename collisions. However, if our suspicion is correct that the `UpdateAndroidResources` target is concurrently executing alongside a Build, this patch will *not* fix the concurrent target execution scenario.
1 parent d8764e7 commit b0f4dea

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/ConvertResourcesCases.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ void FixupResources (ITaskItem item, Dictionary<string, string> acwMap)
5454
foreach (string file in xmls) {
5555
Log.LogDebugMessage (" Processing: {0}", file);
5656
var srcmodifiedDate = File.GetLastWriteTimeUtc (file);
57-
var tmpdest = file + ".tmp";
57+
var tmpdest = Path.GetTempFileName ();
5858
MonoAndroidHelper.CopyIfChanged (file, tmpdest);
5959
MonoAndroidHelper.SetWriteable (tmpdest);
6060
try {
61-
AndroidResource.UpdateXmlResource (tmpdest, acwMap,
61+
AndroidResource.UpdateXmlResource (resdir, tmpdest, acwMap,
6262
ResourceDirectories.Where (s => s != item).Select(s => s.ItemSpec));
6363

6464
// We strip away an eventual UTF-8 BOM from the XML file.

src/Xamarin.Android.Build.Tasks/Tasks/CopyAndConvertResources.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ public override bool Execute ()
8686
var destfilename = p.Value;
8787
var srcmodifiedDate = File.GetLastWriteTimeUtc (filename);
8888
var dstmodifiedDate = File.Exists (destfilename) ? File.GetLastAccessTimeUtc (destfilename) : DateTime.MinValue;
89-
var tmpdest = p.Value + ".tmp";
89+
var tmpdest = Path.GetTempFileName ();
90+
var res = Path.Combine (Path.GetDirectoryName (filename), "..");
9091
MonoAndroidHelper.CopyIfChanged (filename, tmpdest);
9192
MonoAndroidHelper.SetWriteable (tmpdest);
9293
try {
93-
AndroidResource.UpdateXmlResource (tmpdest, acw_map);
94+
AndroidResource.UpdateXmlResource (res, tmpdest, acw_map);
9495
if (MonoAndroidHelper.CopyIfChanged (tmpdest, destfilename)) {
9596
MonoAndroidHelper.SetWriteable (destfilename);
9697
MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (destfilename, srcmodifiedDate, Log);

src/Xamarin.Android.Build.Tasks/Utilities/AndroidResource.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
namespace Monodroid {
1010
static class AndroidResource {
1111

12-
public static void UpdateXmlResource (string filename, Dictionary<string, string> acwMap, IEnumerable<string> additionalDirectories = null)
12+
public static void UpdateXmlResource (string res, string filename, Dictionary<string, string> acwMap, IEnumerable<string> additionalDirectories = null)
1313
{
1414
// use a temporary file so we only update the real file if things actually changed
1515
string tmpfile = filename + ".bk";
1616
try {
1717
XDocument doc = XDocument.Load (filename, LoadOptions.SetLineInfo);
1818

19-
// The assumption here is that the file we're fixing up is in a directory below the
20-
// obj/${Configuration}/res/ directory and so appending ../gives us the actual path to
21-
// 'res/'
22-
UpdateXmlResource (Path.Combine (Path.GetDirectoryName (filename), ".."), doc.Root, acwMap, additionalDirectories);
19+
UpdateXmlResource (res, doc.Root, acwMap, additionalDirectories);
2320
using (var stream = File.OpenWrite (tmpfile))
2421
using (var xw = new LinePreservedXmlWriter (new StreamWriter (stream)))
2522
xw.WriteNode (doc.CreateNavigator (), false);

0 commit comments

Comments
 (0)