[Microsoft.Build.Engine] Ignore import with empty project attribute when condition...
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Wed, 18 Feb 2015 05:22:29 +0000 (06:22 +0100)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Wed, 18 Feb 2015 06:38:41 +0000 (07:38 +0100)
<Import Project="$(Variable)" Condition="false" /> causes an exception otherwise when Variable is not set.
Also added a test for condition referencing extension path after discussion in https://github.com/akoeplinger/mono/commit/f13d9cb5dacbb5c9f758d6dd8b9aacf46b5508b6

mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Import.cs
mcs/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ImportTest.cs

index 14d026ff973124ead2e06fd5e98318941fb93a78..764330f8f5086259381a3b8e13d5fd670ddf587a 100644 (file)
@@ -152,6 +152,15 @@ namespace Microsoft.Build.BuildEngine {
                                                project_attribute.IndexOf ("$(MSBuildExtensionsPath32)") >= 0 ||
                                                project_attribute.IndexOf ("$(MSBuildExtensionsPath64)") >= 0;
 
+                       bool condn_has_extn_ref = condition_attribute.IndexOf ("$(MSBuildExtensionsPath)") >= 0 ||
+                                               condition_attribute.IndexOf ("$(MSBuildExtensionsPath32)") >= 0 ||
+                                               condition_attribute.IndexOf ("$(MSBuildExtensionsPath64)") >= 0;
+
+                       // we can skip the following logic in case the condition doesn't reference any extension paths
+                       // and it evaluates to false since nothing would change anyway
+                       if (!condn_has_extn_ref && !ConditionParser.ParseAndEvaluate (condition_attribute, project))
+                               return;
+
                        string importingFile = importingProject != null ? importingProject.FullFileName : project.FullFileName;
                        DirectoryInfo base_dir_info = null;
                        if (!String.IsNullOrEmpty (importingFile))
index 076db84e6c5f8c915d94ad9b5ebe7c976e86bd26..6edebb8461cc2e20ce3f5c6cd6d539155a0d981f 100644 (file)
@@ -209,7 +209,38 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                        project.LoadXml (documentString);
                }
 
-#if NET_4_0
+               [Test]
+               public void TestImportEmptyVariableWithConditionFalse ()
+               {
+                       string documentString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
+                               <Import Project='$(ImportPath)' Condition='false' />
+                               <Target Name='Build' />
+                       </Project>";
+
+                       engine = new Engine (Consts.BinPath);
+
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       Assert.IsTrue (project.Build ("Build"), "Build failed");
+               }
+
+               [Test]
+               public void TestImportProjectWithConditionReferencingExtensionPath ()
+               {
+                       string documentString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
+                               <Import Project='$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets' Condition=""Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets')""  />
+                               <Target Name='Build' />
+                       </Project>";
+
+                       engine = new Engine (Consts.BinPath);
+
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       Assert.IsTrue (project.Build ("Build"), "Build failed");
+               }
+
                [Test]
                public void TestImportWildcard ()
                {
@@ -264,7 +295,6 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                                File.Delete (second_project);
                        }
                }
-#endif
 
        }
 }