Report preprocessor exceptions as errors. Fixes wixtoolset/issues#5881.
Bob Arnson committed
Sep 27, 2018 at 20:10 UTC
aa33b92c7a2e6b699a11532056485143b0edf4a3
8 files changed
+113
-4
src/WixToolset.Core/CommandLine/BuildCommand.cs
+11
-1
@@ -6,6 +6,7 @@ namespace WixToolset.Core.CommandLine
6
using System.Collections.Generic;
7
using System.IO;
8
using System.Linq;
9
+ using System.Xml.Linq;
10
using WixToolset.Data;
11
using WixToolset.Extensibility.Data;
12
using WixToolset.Extensibility.Services;
@@ -173,7 +174,16 @@ namespace WixToolset.Core.CommandLine
174
preprocessor.Platform = Platform.X86; // TODO: set this correctly
175
preprocessor.SourcePath = sourceFile.SourcePath;
176
preprocessor.Variables = this.PreprocessorVariables;
176
- var document = preprocessor.Execute();
177
+
178
+ XDocument document = null;
179
+ try
180
+ {
181
+ document = preprocessor.Execute();
182
+ }
183
+ catch (WixException e)
184
+ {
185
+ this.Messaging.Write(e.Error);
186
+ }
187
188
if (this.Messaging.EncounteredError)
189
{
src/WixToolset.Core/CommandLine/CompileCommand.cs
+22
-3
@@ -4,26 +4,31 @@ namespace WixToolset.Core.CommandLine
4
{
5
using System;
6
using System.Collections.Generic;
7
+ using System.Xml.Linq;
8
using WixToolset.Data;
9
using WixToolset.Extensibility.Data;
10
+ using WixToolset.Extensibility.Services;
11
12
internal class CompileCommand : ICommandLineCommand
13
{
14
public CompileCommand(IServiceProvider serviceProvider, IEnumerable<SourceFile> sources, IDictionary<string, string> preprocessorVariables)
15
{
14
- this.PreprocessorVariables = preprocessorVariables;
16
this.ServiceProvider = serviceProvider;
17
+ this.Messaging = serviceProvider.GetService<IMessaging>();
18
this.SourceFiles = sources;
19
+ this.PreprocessorVariables = preprocessorVariables;
20
}
21
22
private IServiceProvider ServiceProvider { get; }
23
21
- public IEnumerable<string> IncludeSearchPaths { get; }
24
+ public IMessaging Messaging { get; }
25
26
private IEnumerable<SourceFile> SourceFiles { get; }
27
28
private IDictionary<string, string> PreprocessorVariables { get; }
29
30
+ public IEnumerable<string> IncludeSearchPaths { get; }
31
+
32
public int Execute()
33
{
34
foreach (var sourceFile in this.SourceFiles)
@@ -33,7 +38,21 @@ namespace WixToolset.Core.CommandLine
38
preprocessor.Platform = Platform.X86; // TODO: set this correctly
39
preprocessor.SourcePath = sourceFile.SourcePath;
40
preprocessor.Variables = new Dictionary<string, string>(this.PreprocessorVariables);
36
- var document = preprocessor.Execute();
41
+
42
+ XDocument document = null;
43
+ try
44
+ {
45
+ document = preprocessor.Execute();
46
+ }
47
+ catch (WixException e)
48
+ {
49
+ this.Messaging.Write(e.Error);
50
+ }
51
+
52
+ if (this.Messaging.EncounteredError)
53
+ {
54
+ continue;
55
+ }
56
57
var compiler = new Compiler(this.ServiceProvider);
58
compiler.OutputPath = sourceFile.OutputPath;
src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs
+26
@@ -63,6 +63,32 @@ namespace WixToolsetTest.CoreIntegration
63
Assert.Equal(0, result);
64
}
65
}
66
+
67
+ [Fact]
68
+ public void NonterminatedPreprocessorInstructionShowsSourceLineNumber()
69
+ {
70
+ var folder = TestData.Get(@"TestData\BadIf");
71
+
72
+ using (var fs = new DisposableFileSystem())
73
+ {
74
+ var baseFolder = fs.GetFolder();
75
+ var intermediateFolder = Path.Combine(baseFolder, "obj");
76
+
77
+ var result = WixRunner.Execute(new[]
78
+ {
79
+ "build",
80
+ Path.Combine(folder, "Package.wxs"),
81
+ Path.Combine(folder, "PackageComponents.wxs"),
82
+ "-loc", Path.Combine(folder, "Package.en-us.wxl"),
83
+ "-bindpath", Path.Combine(folder, "data"),
84
+ "-intermediateFolder", intermediateFolder,
85
+ "-o", Path.Combine(baseFolder, @"bin\test.msi")
86
+ }, out var messages);
87
+
88
+ Assert.Equal(147, result);
89
+ Assert.StartsWith("Found a <?if?>", messages.Single().ToString());
90
+ }
91
+ }
92
}
93
}
94
src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.en-us.wxl
new
+11
@@ -0,0 +1,11 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+
3
+<!--
4
+This file contains the declaration of all the localizable strings.
5
+-->
6
+<WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
7
+
8
+ <String Id="DowngradeError">A newer version of [ProductName] is already installed.</String>
9
+ <String Id="FeatureTitle">MsiPackage</String>
10
+
11
+</WixLocalization>
src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/Package.wxs
new
+26
@@ -0,0 +1,26 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
3
+ <Product Id="*" Name="MsiPackage" Language="1033" Version="1.0" Manufacturer="Example Corporation" UpgradeCode="047730a5-30fe-4a62-a520-da9381b8226a">
4
+ <Package InstallerVersion="200" Compressed="no" InstallScope="perMachine" />
5
+
6
+ <MajorUpgrade DowngradeErrorMessage="!(loc.DowngradeError)" />
7
+ <MediaTemplate />
8
+
9
+ <Feature Id="ProductFeature" Title="!(loc.FeatureTitle)">
10
+ <ComponentGroupRef Id="ProductComponents.x86" />
11
+ <!-- <?if x64 == $(sys.BUILDARCH) ?> -->
12
+ <!-- <?if arm==$(sys.BUILDARCH) ?> -->
13
+ <?if $(sys.BUILDARCH) = "x64" ?>
14
+ <ComponentGroupRef Id="ProductComponents.x64" />
15
+ <ComponentGroupRef Id="ProductComponents.arm" />
16
+ </Feature>
17
+ </Product>
18
+
19
+ <Fragment>
20
+ <Directory Id="TARGETDIR" Name="SourceDir">
21
+ <Directory Id="ProgramFilesFolder">
22
+ <Directory Id="INSTALLFOLDER" Name="MsiPackage" />
23
+ </Directory>
24
+ </Directory>
25
+ </Fragment>
26
+</Wix>
src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/PackageComponents.wxs
new
+12
@@ -0,0 +1,12 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
3
+ <?foreach ComponentPlatform in x86;x64;arm ?>
4
+ <Fragment>
5
+ <ComponentGroup Id="ProductComponents.$(var.ComponentPlatform)" Directory="INSTALLFOLDER">
6
+ <Component>
7
+ <File Name="$(var.ComponentPlatform).dll" Source="test.txt" />
8
+ </Component>
9
+ </ComponentGroup>
10
+ </Fragment>
11
+ <?endforeach?>
12
+</Wix>
src/test/WixToolsetTest.CoreIntegration/TestData/BadIf/data/test.txt
new
+1
@@ -0,0 +1 @@
1
+This is test.txt.
\ No newline at end of file
src/test/WixToolsetTest.CoreIntegration/WixToolsetTest.CoreIntegration.csproj
+4
@@ -28,6 +28,10 @@
28
<Content Include="TestData\ForEach\Package.en-us.wxl" CopyToOutputDirectory="PreserveNewest" />
29
<Content Include="TestData\ForEach\Package.wxs" CopyToOutputDirectory="PreserveNewest" />
30
<Content Include="TestData\ForEach\PackageComponents.wxs" CopyToOutputDirectory="PreserveNewest" />
31
+ <Content Include="TestData\BadIf\data\test.txt" CopyToOutputDirectory="PreserveNewest" />
32
+ <Content Include="TestData\BadIf\Package.en-us.wxl" CopyToOutputDirectory="PreserveNewest" />
33
+ <Content Include="TestData\BadIf\Package.wxs" CopyToOutputDirectory="PreserveNewest" />
34
+ <Content Include="TestData\BadIf\PackageComponents.wxs" CopyToOutputDirectory="PreserveNewest" />
35
<Content Include="TestData\SingleFile\data\test.txt" CopyToOutputDirectory="PreserveNewest" />
36
<Content Include="TestData\SingleFile\Package.en-us.wxl" CopyToOutputDirectory="PreserveNewest" />
37
<Content Include="TestData\SingleFile\Package.wxs" CopyToOutputDirectory="PreserveNewest" />