Do not write generated files to disk if the input has parser errors

Fixes #2169
This commit is contained in:
Pranav K 2018-03-19 13:48:14 -07:00
parent c934bd08ac
commit 2bc3466e3d
3 changed files with 47 additions and 8 deletions

View File

@ -181,9 +181,12 @@ namespace Microsoft.AspNetCore.Razor.Tools
}
}
}
var outputFilePath = result.InputItem.OutputPath;
File.WriteAllText(outputFilePath, result.CSharpDocument.GeneratedCode);
else
{
// Only output the file if we generated it without errors.
var outputFilePath = result.InputItem.OutputPath;
File.WriteAllText(outputFilePath, result.CSharpDocument.GeneratedCode);
}
}
return success ? 0 : -1;

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;
@ -90,5 +91,43 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.NotEqual(fileThumbPrint, newThumbPrint);
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_ErrorInGeneratedCode_ReportsMSBuildError_OnIncrementalBuild()
{
// Introducing a Razor semantic error
ReplaceContent("@{ // Unterminated code block", "Views", "Home", "Index.cshtml");
// Regular build
await VerifyError();
// Incremental build
await VerifyError();
async Task VerifyError()
{
var result = await DotnetMSBuild("Build");
Assert.BuildFailed(result);
// This needs to be relative path. Tracked by https://github.com/aspnet/Razor/issues/2187.
var filePath = Path.Combine(Project.DirectoryPath, "Views", "Home", "Index.cshtml");
var location = filePath + "(1,2)";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
// Absolute paths on OSX don't work well.
location = null;
}
Assert.BuildError(result, "RZ1006", location: location);
// Compilation failed without creating the views assembly
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.dll");
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
// File with error does not get written to disk.
Assert.FileDoesNotExist(result, IntermediateOutputPath, "Razor", "Views", "Home", "Index.cshtml.g.cs");
}
}
}
}

View File

@ -69,8 +69,8 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.dll");
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
// The file should still be generated even if we had a Razor syntax error.
Assert.FileExists(result, RazorIntermediateOutputPath, "Views", "Home", "Index.g.cshtml.cs");
// If there's a parser error, the generated file contents is likely incorrect. The file should not be written to disk.
Assert.FileDoesNotExist(result, RazorIntermediateOutputPath, "Views", "Home", "Index.g.cshtml.cs");
}
[Fact]
@ -90,9 +90,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
// RazorGenerate should compile the assembly, but not the views.
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.dll");
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
// The file should still be generated even if we had a Razor syntax error.
Assert.FileExists(result, RazorIntermediateOutputPath, "Views", "Home", "Index.g.cshtml.cs");
}
[ConditionalFact]