From 4442f9473ac50ae8337ea053e4810ddcb128cbf2 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Tue, 4 Apr 2023 01:15:04 -0700 Subject: Push TARGETDIR down to child Components Fixes 7340 --- src/wix/WixToolset.Converters/WixConverter.cs | 17 ++++- .../WixToolsetTest.Converters/DirectoryFixture.cs | 78 +++++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/wix/WixToolset.Converters/WixConverter.cs b/src/wix/WixToolset.Converters/WixConverter.cs index 109a1eb8..84748d70 100644 --- a/src/wix/WixToolset.Converters/WixConverter.cs +++ b/src/wix/WixToolset.Converters/WixConverter.cs @@ -1118,6 +1118,8 @@ namespace WixToolset.Converters { if (this.OnInformation(ConverterTestType.TargetDirDeprecated, element, "The TARGETDIR directory should no longer be explicitly defined. Remove the Directory element with Id attribute 'TARGETDIR'.")) { + AddTargetDirDirectoryAttributeToComponents(element); + RemoveElementKeepChildren(element); } } @@ -1143,9 +1145,11 @@ namespace WixToolset.Converters { if (this.OnInformation(ConverterTestType.StandardDirectoryRefDeprecated, element, "The {0} directory should no longer be explicitly referenced. Remove the DirectoryRef element with Id attribute '{0}'.", id)) { + AddTargetDirDirectoryAttributeToComponents(element); + RemoveElementKeepChildren(element); - this.OnError(ConverterTestType.TargetDirRefRemoved, element, "A reference to the TARGETDIR Directory was removed. This may cause the Fragment that defined TARGETDIR to not be included in the final output. If this happens, reference a different element in the Fragment to replace the old reference to TARGEDIR."); + this.OnError(ConverterTestType.TargetDirRefRemoved, element, "A reference to the TARGETDIR Directory was removed. This can cause unintended side effects. See the conversion FAQ for more information: https://wixtoolset.org/docs/fourthree/faqs/#converting-packages"); } } else if (this.OnInformation(ConverterTestType.StandardDirectoryRefDeprecated, element, "The standard directory '{0}' should no longer be directly referenced. Use the StandardDirectory element instead.", id)) @@ -2810,6 +2814,15 @@ namespace WixToolset.Converters return value; } + private static void AddTargetDirDirectoryAttributeToComponents(XElement element) + { + // Move the TARGETDIR reference to the Component Directory attribute + foreach (var xComponent in element.Elements(ComponentElementName).Where(x => x.Attribute("Directory") is null)) + { + xComponent.Add(new XAttribute("Directory", "TARGETDIR")); + } + } + private static void RemoveElementKeepChildren(XElement element) { var parentElement = element.Parent; @@ -3317,7 +3330,7 @@ namespace WixToolset.Converters WixMbaPrereqPackageIdDeprecated, /// - /// A reference to the TARGETDIR Directory was removed. This may cause the Fragment that defined TARGETDIR to not be included in the final output. If this happens, reference a different element in the Fragment to replace the old reference to TARGEDIR. + /// A reference to the TARGETDIR Directory was removed. This can cause unintended side effects. See the conversion FAQ for more information: https://wixtoolset.org/docs/fourthree/faqs/#converting-packages /// TargetDirRefRemoved, diff --git a/src/wix/test/WixToolsetTest.Converters/DirectoryFixture.cs b/src/wix/test/WixToolsetTest.Converters/DirectoryFixture.cs index c938b9ad..f4cb3b6d 100644 --- a/src/wix/test/WixToolsetTest.Converters/DirectoryFixture.cs +++ b/src/wix/test/WixToolsetTest.Converters/DirectoryFixture.cs @@ -94,7 +94,7 @@ namespace WixToolsetTest.Converters "[Converted] This file contains an XML declaration on the first line. (DeclarationPresent)", "[Converted] The namespace 'http://schemas.microsoft.com/wix/2006/wi' is out of date. It must be 'http://wixtoolset.org/schemas/v4/wxs'. (XmlnsValueWrong)", "[Converted] The TARGETDIR directory should no longer be explicitly referenced. Remove the DirectoryRef element with Id attribute 'TARGETDIR'. (StandardDirectoryRefDeprecated)", - "A reference to the TARGETDIR Directory was removed. This may cause the Fragment that defined TARGETDIR to not be included in the final output. If this happens, reference a different element in the Fragment to replace the old reference to TARGEDIR. (TargetDirRefRemoved)", + "A reference to the TARGETDIR Directory was removed. This can cause unintended side effects. See the conversion FAQ for more information: https://wixtoolset.org/docs/fourthree/faqs/#converting-packages (TargetDirRefRemoved)", }, messaging.Messages.Select(m => m.ToString()).ToArray()); Assert.Equal(4, errors); } @@ -211,6 +211,82 @@ namespace WixToolsetTest.Converters Assert.Equal(5, errors); } + [Fact] + public void RemoveTargetDirWithComponents() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + ""); + + var expected = new[] + { + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + "" + }; + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new MockMessaging(); + var converter = new WixConverter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actualLines = UnformattedDocumentLines(document); + WixAssert.CompareLineByLine(expected, actualLines); + + WixAssert.CompareLineByLine(new[] + { + "[Converted] This file contains an XML declaration on the first line. (DeclarationPresent)", + "[Converted] The namespace 'http://schemas.microsoft.com/wix/2006/wi' is out of date. It must be 'http://wixtoolset.org/schemas/v4/wxs'. (XmlnsValueWrong)", + "[Converted] The TARGETDIR directory should no longer be explicitly referenced. Remove the DirectoryRef element with Id attribute 'TARGETDIR'. (StandardDirectoryRefDeprecated)", + "A reference to the TARGETDIR Directory was removed. This can cause unintended side effects. See the conversion FAQ for more information: https://wixtoolset.org/docs/fourthree/faqs/#converting-packages (TargetDirRefRemoved)", + "[Converted] The file id is being updated to 'c1.txt' to ensure it remains the same as the v3 default (AssignAnonymousFileId)", + "[Converted] The file id is being updated to 'c2.txt' to ensure it remains the same as the v3 default (AssignAnonymousFileId)", + "[Converted] The TARGETDIR directory should no longer be explicitly defined. Remove the Directory element with Id attribute 'TARGETDIR'. (TargetDirDeprecated)", + "[Converted] The file id is being updated to 'c3.txt' to ensure it remains the same as the v3 default (AssignAnonymousFileId)", + "[Converted] The file id is being updated to 'c4.txt' to ensure it remains the same as the v3 default (AssignAnonymousFileId)" + }, messaging.Messages.Select(m => m.ToString()).ToArray()); + + Assert.Equal(9, errors); + } + [Fact] public void ErrorOnEmptyStandardDirectoryRef() { -- cgit v1.2.3-55-g6feb