Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
Add better errors, fix help output text, and add 'dotnet watch --list…
Browse files Browse the repository at this point in the history
…' to help us diagnose issues

Fixes #252 - help output shown twice in dotnet-watch
Fixes #250 - add dotnet-watch --list. Prints a list of all files discovered
Fixes #249 - better error message when GenerateWatchList fails
  • Loading branch information
Nate McMaster committed Jan 13, 2017
1 parent 7ee45af commit b7abeea
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 58 deletions.
28 changes: 28 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; EditorConfig to support per-solution formatting.
; Use the EditorConfig VS add-in to make this work.
; http://editorconfig.org/

; This is the default for the codeline.
root = true

[*]
indent_style = space
charset = utf-8
trim_trailing_whitespace = true

[*.{cs}]
indent_size = 4
insert_final_newline = true

; All XML-based file formats
[*.{config,csproj,nuspec,props,resx,targets,xml}]
indent_size = 2

[*.{json}]
indent_size = 2

[*.{ps1}]
indent_size = 4

[*.{sh}]
indent_size = 4
13 changes: 7 additions & 6 deletions DotNetTools.sln
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.25807.0
VisualStudioVersion = 15.0.26020.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{66517987-2A5A-4330-B130-207039378FD4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Watcher.Tools", "src\Microsoft.DotNet.Watcher.Tools\Microsoft.DotNet.Watcher.Tools.csproj", "{8A8CEABC-AC47-43FF-A5DF-69224F7E1F46}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8321E0D1-9A47-4D2F-AED8-3AE636D44E35}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
NuGet.Config = NuGet.Config
EndProjectSection
EndProject
Expand Down Expand Up @@ -43,10 +44,6 @@ Global
{8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|Any CPU.Build.0 = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Release|Any CPU.Build.0 = Debug|Any CPU
{7B331122-83B1-4F08-A119-DC846959844C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B331122-83B1-4F08-A119-DC846959844C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B331122-83B1-4F08-A119-DC846959844C}.Release|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -59,6 +56,10 @@ Global
{53F3B53D-303A-4DAA-9C38-4F55195FA5B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53F3B53D-303A-4DAA-9C38-4F55195FA5B9}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{53F3B53D-303A-4DAA-9C38-4F55195FA5B9}.Release|Any CPU.Build.0 = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD}.Release|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -67,9 +68,9 @@ Global
{8A8CEABC-AC47-43FF-A5DF-69224F7E1F46} = {66517987-2A5A-4330-B130-207039378FD4}
{16BADE2F-1184-4518-8A70-B68A19D0805B} = {F5B382BC-258F-46E1-AC3D-10E5CCD55134}
{8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E} = {66517987-2A5A-4330-B130-207039378FD4}
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD} = {F5B382BC-258F-46E1-AC3D-10E5CCD55134}
{7B331122-83B1-4F08-A119-DC846959844C} = {F5B382BC-258F-46E1-AC3D-10E5CCD55134}
{8A2E6961-6B12-4A8E-8215-3E7301D52EAC} = {F5B382BC-258F-46E1-AC3D-10E5CCD55134}
{53F3B53D-303A-4DAA-9C38-4F55195FA5B9} = {66517987-2A5A-4330-B130-207039378FD4}
{9295E811-FF0F-E40A-2F9A-0B2C4EBC22AD} = {F5B382BC-258F-46E1-AC3D-10E5CCD55134}
EndGlobalSection
EndGlobal
13 changes: 10 additions & 3 deletions src/Microsoft.DotNet.Watcher.Tools/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class CommandLineOptions
public bool IsQuiet { get; private set; }
public bool IsVerbose { get; private set; }
public IList<string> RemainingArguments { get; private set; }
public bool ListFiles { get; private set; }

public static CommandLineOptions Parse(string[] args, IConsole console)
{
Expand Down Expand Up @@ -63,6 +64,9 @@ public static CommandLineOptions Parse(string[] args, IConsole console)
CommandOptionType.NoValue);
var optVerbose = app.VerboseOption();

var optList = app.Option("--list", "Lists all discovered files without starting the watcher",
CommandOptionType.NoValue);

app.VersionOptionFromAssemblyAttributes(typeof(Program).GetTypeInfo().Assembly);

if (app.Execute(args) != 0)
Expand All @@ -75,7 +79,9 @@ public static CommandLineOptions Parse(string[] args, IConsole console)
throw new CommandParsingException(app, Resources.Error_QuietAndVerboseSpecified);
}

if (app.RemainingArguments.Count == 0)
if (app.RemainingArguments.Count == 0
&& !app.IsShowingInformation
&& !optList.HasValue())
{
app.ShowHelp();
}
Expand All @@ -86,8 +92,9 @@ public static CommandLineOptions Parse(string[] args, IConsole console)
IsQuiet = optQuiet.HasValue(),
IsVerbose = optVerbose.HasValue(),
RemainingArguments = app.RemainingArguments,
IsHelp = app.IsShowingInformation
IsHelp = app.IsShowingInformation,
ListFiles = optList.HasValue(),
};
}
}
}
}
9 changes: 8 additions & 1 deletion src/Microsoft.DotNet.Watcher.Tools/DotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public DotNetWatcher(IReporter reporter)
while (true)
{
var fileSet = await fileSetFactory.CreateAsync(cancellationToken);

if (fileSet == null)
{
_reporter.Error("Failed to find a list of files to watch");
return;
}

if (cancellationToken.IsCancellationRequested)
{
return;
Expand Down Expand Up @@ -91,4 +98,4 @@ public DotNetWatcher(IReporter reporter)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ namespace Microsoft.DotNet.Watcher.Internal
public class MsBuildFileSetFactory : IFileSetFactory
{
private const string TargetName = "GenerateWatchList";
private const string ProjectExtensionFileExtension = ".dotnetwatch.targets";
private const string ProjectExtensionFileExtension = ".dotnetwatch.g.targets";
private const string WatchTargetsFileName = "DotNetWatchCommon.targets";
private readonly IReporter _reporter;
private readonly string _projectFile;
private readonly string _watchTargetsDir;
private readonly OutputSink _outputSink;
private readonly ProcessRunner _processRunner;
private readonly bool _waitOnError;

public MsBuildFileSetFactory(IReporter reporter, string projectFile)
public MsBuildFileSetFactory(IReporter reporter, string projectFile, bool waitOnError)
: this(reporter, projectFile, new OutputSink())
{
_waitOnError = waitOnError;
}

// output sink is for testing
Expand Down Expand Up @@ -86,7 +88,7 @@ public async Task<IFileSet> CreateAsync(CancellationToken cancellationToken)

var exitCode = await _processRunner.RunAsync(processSpec, cancellationToken);

if (exitCode == 0)
if (exitCode == 0 && File.Exists(watchList))
{
var fileset = new FileSet(
File.ReadAllLines(watchList)
Expand All @@ -109,28 +111,41 @@ public async Task<IFileSet> CreateAsync(CancellationToken cancellationToken)

_reporter.Error($"Error(s) finding watch items project file '{Path.GetFileName(_projectFile)}'");

_reporter.Output($"MSBuild output from target '{TargetName}'");
_reporter.Output($"MSBuild output from target '{TargetName}':");
_reporter.Output(string.Empty);

foreach (var line in capture.Lines)
{
_reporter.Output($" [MSBUILD] : {line}");
_reporter.Output($" {line}");
}

_reporter.Warn("Fix the error to continue or press Ctrl+C to exit.");
_reporter.Output(string.Empty);

var fileSet = new FileSet(new[] {_projectFile});

using (var watcher = new FileSetWatcher(fileSet))
if (!_waitOnError)
{
return null;
}
else
{
await watcher.GetChangedFileAsync(cancellationToken);
_reporter.Warn("Fix the error to continue or press Ctrl+C to exit.");

_reporter.Output($"File changed: {_projectFile}");
var fileSet = new FileSet(new[] { _projectFile });

using (var watcher = new FileSetWatcher(fileSet))
{
await watcher.GetChangedFileAsync(cancellationToken);

_reporter.Output($"File changed: {_projectFile}");
}
}
}
}
finally
{
File.Delete(watchList);
if (File.Exists(watchList))
{
File.Delete(watchList);
}
}
}

Expand Down Expand Up @@ -173,4 +188,4 @@ private string FindWatchTargetsDir()
return Path.GetDirectoryName(targetPath);
}
}
}
}
53 changes: 49 additions & 4 deletions src/Microsoft.DotNet.Watcher.Tools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public async Task<int> RunAsync(string[] args)

try
{
return await MainInternalAsync(reporter, options.Project, options.RemainingArguments, ctrlCTokenSource.Token);
if (options.ListFiles)
{
return await ListFilesAsync(reporter,
options.Project,
ctrlCTokenSource.Token);
}
else
{

return await MainInternalAsync(reporter,
options.Project,
options.RemainingArguments,
ctrlCTokenSource.Token);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -115,8 +128,7 @@ public async Task<int> RunAsync(string[] args)
return 1;
}

var fileSetFactory = new MsBuildFileSetFactory(reporter, projectFile);

var fileSetFactory = new MsBuildFileSetFactory(reporter, projectFile, waitOnError: true);
var processInfo = new ProcessSpec
{
Executable = DotNetMuxer.MuxerPathOrDefault(),
Expand All @@ -130,6 +142,39 @@ public async Task<int> RunAsync(string[] args)
return 0;
}

private async Task<int> ListFilesAsync(
IReporter reporter,
string project,
CancellationToken cancellationToken)
{
// TODO multiple projects should be easy enough to add here
string projectFile;
try
{
projectFile = MsBuildProjectFinder.FindMsBuildProject(_workingDir, project);
}
catch (FileNotFoundException ex)
{
reporter.Error(ex.Message);
return 1;
}

var fileSetFactory = new MsBuildFileSetFactory(reporter, projectFile, waitOnError: false);
var files = await fileSetFactory.CreateAsync(cancellationToken);

if (files == null)
{
return 1;
}

foreach(var file in files)
{
_console.Out.WriteLine(file);
}

return 0;
}

private static IReporter CreateReporter(bool verbose, bool quiet, IConsole console)
{
const string prefix = "watch : ";
Expand Down Expand Up @@ -178,4 +223,4 @@ private static IReporter CreateReporter(bool verbose, bool quiet, IConsole conso
.Build();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ private void EnsureProjectExtensionTargetsExist(string projectFile)
using (var stream = new FileStream(projectExtensionsPath, FileMode.Create))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("<!-- Auto-generated by dotnet-user-secrets. This file can be deleted and should not be commited to source control. -->");
resource.CopyTo(stream);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ public IEnumerable<string> GetDotnetWatchArguments()
// this launches a new .NET Core process using the runtime of the current test app
// and the version of dotnet-watch that this test app is compiled against
var thisAssembly = Path.GetFileNameWithoutExtension(GetType().GetTypeInfo().Assembly.Location);
var args = new List<string>();
args.Add("exec");

args.Add("--depsfile");
args.Add(Path.Combine(AppContext.BaseDirectory, thisAssembly + ".deps.json"));

args.Add("--runtimeconfig");
args.Add(Path.Combine(AppContext.BaseDirectory, thisAssembly + ".runtimeconfig.json"));
var args = new List<string>
{
"exec",
"--depsfile",
Path.Combine(AppContext.BaseDirectory, thisAssembly + ".deps.json"),
"--runtimeconfig",
Path.Combine(AppContext.BaseDirectory, thisAssembly + ".runtimeconfig.json")
};

var currentFxVersion = AppContext.GetData("FX_DEPS_FILE") as string;
if (currentFxVersion != null)
if (AppContext.GetData("FX_DEPS_FILE") is string currentFxVersion)
{
// This overrides the version of shared fx in the runtimeconfig.json file.
// Tests do this to ensure dotnet-watch is executing on the version of Microsoft.NETCore.App
Expand Down
Loading

0 comments on commit b7abeea

Please sign in to comment.