From aa33b92c7a2e6b699a11532056485143b0edf4a3 Mon Sep 17 00:00:00 2001 From: Bob Arnson Date: Thu, 27 Sep 2018 20:10:30 -0400 Subject: Report preprocessor exceptions as errors. Fixes wixtoolset/issues#5881. --- src/WixToolset.Core/CommandLine/BuildCommand.cs | 12 +++++++++- src/WixToolset.Core/CommandLine/CompileCommand.cs | 25 ++++++++++++++++++--- .../PreprocessorFixture.cs | 26 ++++++++++++++++++++++ .../TestData/BadIf/Package.en-us.wxl | 11 +++++++++ .../TestData/BadIf/Package.wxs | 26 ++++++++++++++++++++++ .../TestData/BadIf/PackageComponents.wxs | 12 ++++++++++ .../TestData/BadIf/data/test.txt | 1 + .../WixToolsetTest.CoreIntegration.csproj | 4 ++++ 8 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.en-us.wxl create mode 100644 src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.wxs create mode 100644 src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/PackageComponents.wxs create mode 100644 src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/data/test.txt (limited to 'src') diff --git a/src/WixToolset.Core/CommandLine/BuildCommand.cs b/src/WixToolset.Core/CommandLine/BuildCommand.cs index b460e48f..16c98c83 100644 --- a/src/WixToolset.Core/CommandLine/BuildCommand.cs +++ b/src/WixToolset.Core/CommandLine/BuildCommand.cs @@ -6,6 +6,7 @@ namespace WixToolset.Core.CommandLine using System.Collections.Generic; using System.IO; using System.Linq; + using System.Xml.Linq; using WixToolset.Data; using WixToolset.Extensibility.Data; using WixToolset.Extensibility.Services; @@ -173,7 +174,16 @@ namespace WixToolset.Core.CommandLine preprocessor.Platform = Platform.X86; // TODO: set this correctly preprocessor.SourcePath = sourceFile.SourcePath; preprocessor.Variables = this.PreprocessorVariables; - var document = preprocessor.Execute(); + + XDocument document = null; + try + { + document = preprocessor.Execute(); + } + catch (WixException e) + { + this.Messaging.Write(e.Error); + } if (this.Messaging.EncounteredError) { diff --git a/src/WixToolset.Core/CommandLine/CompileCommand.cs b/src/WixToolset.Core/CommandLine/CompileCommand.cs index 6bd0f25a..ec1ce602 100644 --- a/src/WixToolset.Core/CommandLine/CompileCommand.cs +++ b/src/WixToolset.Core/CommandLine/CompileCommand.cs @@ -4,26 +4,31 @@ namespace WixToolset.Core.CommandLine { using System; using System.Collections.Generic; + using System.Xml.Linq; using WixToolset.Data; using WixToolset.Extensibility.Data; + using WixToolset.Extensibility.Services; internal class CompileCommand : ICommandLineCommand { public CompileCommand(IServiceProvider serviceProvider, IEnumerable sources, IDictionary preprocessorVariables) { - this.PreprocessorVariables = preprocessorVariables; this.ServiceProvider = serviceProvider; + this.Messaging = serviceProvider.GetService(); this.SourceFiles = sources; + this.PreprocessorVariables = preprocessorVariables; } private IServiceProvider ServiceProvider { get; } - public IEnumerable IncludeSearchPaths { get; } + public IMessaging Messaging { get; } private IEnumerable SourceFiles { get; } private IDictionary PreprocessorVariables { get; } + public IEnumerable IncludeSearchPaths { get; } + public int Execute() { foreach (var sourceFile in this.SourceFiles) @@ -33,7 +38,21 @@ namespace WixToolset.Core.CommandLine preprocessor.Platform = Platform.X86; // TODO: set this correctly preprocessor.SourcePath = sourceFile.SourcePath; preprocessor.Variables = new Dictionary(this.PreprocessorVariables); - var document = preprocessor.Execute(); + + XDocument document = null; + try + { + document = preprocessor.Execute(); + } + catch (WixException e) + { + this.Messaging.Write(e.Error); + } + + if (this.Messaging.EncounteredError) + { + continue; + } var compiler = new Compiler(this.ServiceProvider); compiler.OutputPath = sourceFile.OutputPath; diff --git a/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs b/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs index 9c60c902..ebc713ed 100644 --- a/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs @@ -63,6 +63,32 @@ namespace WixToolsetTest.CoreIntegration Assert.Equal(0, result); } } + + [Fact] + public void NonterminatedPreprocessorInstructionShowsSourceLineNumber() + { + var folder = TestData.Get(@"TestData\BadIf"); + + using (var fs = new DisposableFileSystem()) + { + var baseFolder = fs.GetFolder(); + var intermediateFolder = Path.Combine(baseFolder, "obj"); + + var result = WixRunner.Execute(new[] + { + "build", + Path.Combine(folder, "Package.wxs"), + Path.Combine(folder, "PackageComponents.wxs"), + "-loc", Path.Combine(folder, "Package.en-us.wxl"), + "-bindpath", Path.Combine(folder, "data"), + "-intermediateFolder", intermediateFolder, + "-o", Path.Combine(baseFolder, @"bin\test.msi") + }, out var messages); + + Assert.Equal(147, result); + Assert.StartsWith("Found a ", messages.Single().ToString()); + } + } } } diff --git a/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.en-us.wxl b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.en-us.wxl new file mode 100644 index 00000000..38c12ac1 --- /dev/null +++ b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.en-us.wxl @@ -0,0 +1,11 @@ + + + + + + A newer version of [ProductName] is already installed. + MsiPackage + + diff --git a/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.wxs b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.wxs new file mode 100644 index 00000000..73577ce5 --- /dev/null +++ b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.wxs @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/PackageComponents.wxs b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/PackageComponents.wxs new file mode 100644 index 00000000..2a75e3d7 --- /dev/null +++ b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/PackageComponents.wxs @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/data/test.txt b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/data/test.txt new file mode 100644 index 00000000..cd0db0e1 --- /dev/null +++ b/src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/data/test.txt @@ -0,0 +1 @@ +This is test.txt. \ No newline at end of file diff --git a/src/test/WixToolsetTest.CoreIntegration/WixToolsetTest.CoreIntegration.csproj b/src/test/WixToolsetTest.CoreIntegration/WixToolsetTest.CoreIntegration.csproj index 7c4578d9..38b7dc81 100644 --- a/src/test/WixToolsetTest.CoreIntegration/WixToolsetTest.CoreIntegration.csproj +++ b/src/test/WixToolsetTest.CoreIntegration/WixToolsetTest.CoreIntegration.csproj @@ -28,6 +28,10 @@ + + + + -- cgit v1.2.3-55-g6feb