Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try/catch around FindWorkspace calls to handle FileNotFound #1387

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Commands/FormatAnalyzersCommand.cs
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.CommandLine;
using System.CommandLine.IO;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -36,7 +37,16 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
var formatOptions = parseResult.ParseVerbosityOption(FormatOptions.Instance);
var logger = new SystemConsole().SetupLogging(minimalLogLevel: formatOptions.LogLevel, minimalErrorLevel: LogLevel.Warning);
formatOptions = parseResult.ParseCommonOptions(formatOptions, logger);
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);

try
{
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);
}
catch (FileNotFoundException fex)
{
logger.LogError(fex.Message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we show a message saying we couldn't find the file instead of logging an error? Doesn't this still show a stack trace to the user?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We log the message because the workspace finder throws this exception with different messages based on how it fails. This does not log the stack trace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rework this to not rely on exception handling but wanted to address the regression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is in main I am ok with waiting for a full re-work. do we want this to go into the 6.x branch instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out wrong base branch. Updated to release/6.x

Copy link
Contributor

@jmarolf jmarolf Oct 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, I'll email .NET Tactics to see if they want to take this for .NET 6

return UnhandledExceptionExitCode;
}

if (parseResult.GetResult(SeverityOption) is not null &&
parseResult.GetValue(SeverityOption) is string { Length: > 0 } analyzerSeverity)
Expand Down
12 changes: 11 additions & 1 deletion src/Commands/FormatStyleCommand.cs
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.CommandLine;
using System.CommandLine.IO;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -36,7 +37,16 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
var formatOptions = parseResult.ParseVerbosityOption(FormatOptions.Instance);
var logger = new SystemConsole().SetupLogging(minimalLogLevel: formatOptions.LogLevel, minimalErrorLevel: LogLevel.Warning);
formatOptions = parseResult.ParseCommonOptions(formatOptions, logger);
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);

try
{
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);
}
catch (FileNotFoundException fex)
{
logger.LogError(fex.Message);
return UnhandledExceptionExitCode;
}

if (parseResult.GetResult(SeverityOption) is not null &&
parseResult.GetValue(SeverityOption) is string { Length: > 0 } styleSeverity)
Expand Down
12 changes: 11 additions & 1 deletion src/Commands/FormatWhitespaceCommand.cs
Expand Up @@ -3,6 +3,7 @@
using System.CommandLine;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -71,7 +72,16 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
var formatOptions = parseResult.ParseVerbosityOption(FormatOptions.Instance);
var logger = new SystemConsole().SetupLogging(minimalLogLevel: formatOptions.LogLevel, minimalErrorLevel: LogLevel.Warning);
formatOptions = parseResult.ParseCommonOptions(formatOptions, logger);
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);

try
{
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);
}
catch (FileNotFoundException fex)
{
logger.LogError(fex.Message);
return UnhandledExceptionExitCode;
}

formatOptions = formatOptions with { FixCategory = FixCategory.Whitespace };

Expand Down
12 changes: 11 additions & 1 deletion src/Commands/RootFormatCommand.cs
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.CommandLine;
using System.CommandLine.IO;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -39,7 +40,16 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
var formatOptions = parseResult.ParseVerbosityOption(FormatOptions.Instance);
var logger = new SystemConsole().SetupLogging(minimalLogLevel: formatOptions.LogLevel, minimalErrorLevel: LogLevel.Warning);
formatOptions = parseResult.ParseCommonOptions(formatOptions, logger);
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);

try
{
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);
}
catch (FileNotFoundException fex)
{
logger.LogError(fex.Message);
return UnhandledExceptionExitCode;
}

if (parseResult.GetResult(SeverityOption) is not null &&
parseResult.GetValue(SeverityOption) is string { Length: > 0 } defaultSeverity)
Expand Down