Remove WixFileNotFoundException, report checked paths and improve bind path command-line parsing
Rob Mensching committed
Jul 27, 2018 at 00:35 UTC
c8c73ccddedcb64f9989e3d5a9f15240b476b551
7 files changed
+80
-22
src/WixToolset.Core.WindowsInstaller/Bind/GenerateDatabaseCommand.cs
+2
-2
@@ -334,10 +334,10 @@ namespace WixToolset.Core.WindowsInstaller.Bind
334
db.Commit();
335
}
336
}
337
- catch (IOException)
337
+ catch (IOException e)
338
{
339
// TODO: this error message doesn't seem specific enough
340
- throw new WixFileNotFoundException(new SourceLineNumber(this.OutputPath), this.OutputPath);
340
+ throw new WixException(ErrorMessages.FileNotFound(new SourceLineNumber(this.OutputPath), this.OutputPath), e);
341
}
342
}
343
src/WixToolset.Core/Bind/FileResolver.cs
+26
-10
@@ -43,17 +43,24 @@ namespace WixToolset.Core.Bind
43
44
public string Resolve(SourceLineNumber sourceLineNumbers, IntermediateTupleDefinition tupleDefinition, string source)
45
{
46
+ var checkedPaths = new List<string>();
47
+
48
foreach (var extension in this.LibrarianExtensions)
49
{
48
- var resolved = extension.Resolve(sourceLineNumbers, tupleDefinition, source);
50
+ var resolved = extension.ResolveFile(sourceLineNumbers, tupleDefinition, source);
51
+
52
+ if (resolved?.CheckedPaths != null)
53
+ {
54
+ checkedPaths.AddRange(resolved.CheckedPaths);
55
+ }
56
50
- if (null != resolved)
57
+ if (!String.IsNullOrEmpty(resolved?.Path))
58
{
52
- return resolved;
59
+ return resolved?.Path;
60
}
61
}
62
56
- return this.ResolveUsingBindPaths(source, tupleDefinition, sourceLineNumbers, BindStage.Normal);
63
+ return this.MustResolveUsingBindPaths(source, tupleDefinition, sourceLineNumbers, BindStage.Normal, checkedPaths);
64
}
65
66
/// <summary>
@@ -66,24 +73,32 @@ namespace WixToolset.Core.Bind
73
/// <returns>Should return a valid path for the stream to be imported.</returns>
74
public string ResolveFile(string source, IntermediateTupleDefinition tupleDefinition, SourceLineNumber sourceLineNumbers, BindStage bindStage)
75
{
76
+ var checkedPaths = new List<string>();
77
+
78
foreach (var extension in this.ResolverExtensions)
79
{
80
var resolved = extension.ResolveFile(source, tupleDefinition, sourceLineNumbers, bindStage);
81
73
- if (null != resolved)
82
+ if (resolved?.CheckedPaths != null)
83
+ {
84
+ checkedPaths.AddRange(resolved.CheckedPaths);
85
+ }
86
+
87
+ if (!String.IsNullOrEmpty(resolved?.Path))
88
{
75
- return resolved;
89
+ return resolved?.Path;
90
}
91
}
92
79
- return this.ResolveUsingBindPaths(source, tupleDefinition, sourceLineNumbers, bindStage);
93
+ return this.MustResolveUsingBindPaths(source, tupleDefinition, sourceLineNumbers, bindStage, checkedPaths);
94
}
95
82
- private string ResolveUsingBindPaths(string source, IntermediateTupleDefinition tupleDefinition, SourceLineNumber sourceLineNumbers, BindStage bindStage)
96
+ private string MustResolveUsingBindPaths(string source, IntermediateTupleDefinition tupleDefinition, SourceLineNumber sourceLineNumbers, BindStage bindStage, List<string> checkedPaths)
97
{
98
string resolved = null;
99
100
// If the file exists, we're good to go.
101
+ checkedPaths.Add(source);
102
if (CheckFileExists(source))
103
{
104
resolved = source;
@@ -121,6 +136,7 @@ namespace WixToolset.Core.Bind
136
{
137
var filePath = Path.Combine(bindPath.Path, pathWithoutSourceDir);
138
139
+ checkedPaths.Add(filePath);
140
if (CheckFileExists(filePath))
141
{
142
resolved = filePath;
@@ -131,6 +147,7 @@ namespace WixToolset.Core.Bind
147
{
148
var filePath = Path.Combine(bindPath.Path, path);
149
150
+ checkedPaths.Add(filePath);
151
if (CheckFileExists(filePath))
152
{
153
resolved = filePath;
@@ -141,10 +158,9 @@ namespace WixToolset.Core.Bind
158
159
if (null == resolved)
160
{
144
- throw new WixFileNotFoundException(sourceLineNumbers, source, tupleDefinition.Name);
161
+ throw new WixException(ErrorMessages.FileNotFound(sourceLineNumbers, source, tupleDefinition.Name, checkedPaths));
162
}
163
147
- // Didn't find the file.
164
return resolved;
165
}
166
src/WixToolset.Core/Bind/ResolveFieldsCommand.cs
+2
-3
@@ -152,10 +152,9 @@ namespace WixToolset.Core.Bind
152
}
153
#endif
154
}
155
- catch (WixFileNotFoundException)
155
+ catch (WixException e)
156
{
157
- // display the error with source line information
158
- this.Messaging.Write(ErrorMessages.FileNotFound(row.SourceLineNumbers, objectField.Path));
157
+ this.Messaging.Write(e.Error);
158
}
159
}
160
src/WixToolset.Core/Bind/TransferFilesCommand.cs
+1
-1
@@ -63,7 +63,7 @@ namespace WixToolset.Core.Bind
63
}
64
catch (FileNotFoundException e)
65
{
66
- throw new WixFileNotFoundException(fileTransfer.SourceLineNumbers, e.FileName);
66
+ throw new WixException(ErrorMessages.FileNotFound(fileTransfer.SourceLineNumbers, e.FileName));
67
}
68
catch (DirectoryNotFoundException)
69
{
src/WixToolset.Core/CommandLine/CommandLineParser.cs
+13
-5
@@ -22,6 +22,8 @@ namespace WixToolset.Core.CommandLine
22
23
internal class CommandLineParser : ICommandLineParser
24
{
25
+ private static readonly char[] BindPathSplit = { '=' };
26
+
27
public CommandLineParser(IServiceProvider serviceProvider)
28
{
29
this.ServiceProvider = serviceProvider;
@@ -380,15 +382,15 @@ namespace WixToolset.Core.CommandLine
382
383
foreach (var bindPath in bindPaths)
384
{
383
- var bp = BindPath.Parse(bindPath);
385
+ var bp = ParseBindPath(bindPath);
386
385
- if (Directory.Exists(bp.Path))
387
+ if (File.Exists(bp.Path))
388
{
387
- result.Add(bp);
389
+ this.Messaging.Write(ErrorMessages.ExpectedDirectoryGotFile("-bindpath", bp.Path));
390
}
389
- else if (File.Exists(bp.Path))
391
+ else
392
{
391
- this.Messaging.Write(ErrorMessages.ExpectedDirectoryGotFile("-bindpath", bp.Path));
393
+ result.Add(bp);
394
}
395
}
396
@@ -407,5 +409,11 @@ namespace WixToolset.Core.CommandLine
409
410
return false;
411
}
412
+
413
+ public static BindPath ParseBindPath(string bindPath)
414
+ {
415
+ string[] namedPath = bindPath.Split(BindPathSplit, 2);
416
+ return (1 == namedPath.Length) ? new BindPath(namedPath[0]) : new BindPath(namedPath[0], namedPath[1]);
417
+ }
418
}
419
}
src/WixToolset.Core/Preprocessor.cs
+1
-1
@@ -649,7 +649,7 @@ namespace WixToolset.Core
649
650
if (null == includeFile)
651
{
652
- throw new WixFileNotFoundException(sourceLineNumbers, includePath, "include");
652
+ throw new WixException(ErrorMessages.FileNotFound(sourceLineNumbers, includePath, "include"));
653
}
654
655
using (XmlReader reader = XmlReader.Create(includeFile, DocumentXmlReaderSettings))
src/test/WixToolsetTest.CoreIntegration/MsiFixture.cs
+35
@@ -170,6 +170,41 @@ namespace WixToolsetTest.CoreIntegration
170
}
171
}
172
173
+ [Fact]
174
+ public void CanFailBuildMissingFile()
175
+ {
176
+ var folder = TestData.Get(@"TestData\SingleFile");
177
+
178
+ using (var fs = new DisposableFileSystem())
179
+ {
180
+ var baseFolder = fs.GetFolder();
181
+ var intermediateFolder = Path.Combine(baseFolder, "obj");
182
+
183
+ var result = WixRunner.Execute(new[]
184
+ {
185
+ "build",
186
+ Path.Combine(folder, "Package.wxs"),
187
+ Path.Combine(folder, "PackageComponents.wxs"),
188
+ "-loc", Path.Combine(folder, "Package.en-us.wxl"),
189
+ "-bindpath", Path.Combine(folder, "does-not-exist"),
190
+ "-bindpath", Path.Combine(folder, "also-does-not-exist"),
191
+ "-intermediateFolder", intermediateFolder,
192
+ "-o", Path.Combine(baseFolder, @"bin\test.msi")
193
+ }, out var messages);
194
+ Assert.Equal(103, result);
195
+
196
+ var error = messages.Single(m => m.Level == MessageLevel.Error);
197
+ var errorMessage = error.ToString();
198
+ var checkedPaths = errorMessage.Substring(errorMessage.IndexOf(':') + 1).Split(new[] { ',' }).Select(s => s.Trim()).ToArray();
199
+ Assert.Equal(new[]
200
+ {
201
+ "test.txt",
202
+ Path.Combine(folder, "does-not-exist", "test.txt"),
203
+ Path.Combine(folder, "also-does-not-exist", "test.txt"),
204
+ }, checkedPaths);
205
+ }
206
+ }
207
+
208
[Fact]
209
public void CanLoadPdbGeneratedByBuild()
210
{