do not run project on failed build

This commit is contained in:
Matt Parker
2026-01-18 17:00:47 +10:00
parent fc9e6ef3ec
commit 56784ecfd3
2 changed files with 24 additions and 7 deletions

View File

@@ -17,6 +17,8 @@ public enum BuildType
Restore
}
public enum BuildStartedFlags { UserFacing = 0, Internal }
public enum SharpIdeBuildResult { Success = 0, Failure }
public class BuildService(ILogger<BuildService> logger)
{
private readonly ILogger<BuildService> _logger = logger;
@@ -25,7 +27,7 @@ public class BuildService(ILogger<BuildService> logger)
public EventWrapper<Task> BuildFinished { get; } = new(() => Task.CompletedTask);
public ChannelTextWriter BuildTextWriter { get; } = new ChannelTextWriter();
private CancellationTokenSource? _cancellationTokenSource;
public async Task MsBuildAsync(string solutionOrProjectFilePath, BuildType buildType = BuildType.Build, BuildStartedFlags buildStartedFlags = BuildStartedFlags.UserFacing, CancellationToken cancellationToken = default)
public async Task<SharpIdeBuildResult> MsBuildAsync(string solutionOrProjectFilePath, BuildType buildType = BuildType.Build, BuildStartedFlags buildStartedFlags = BuildStartedFlags.UserFacing, CancellationToken cancellationToken = default)
{
if (_cancellationTokenSource is not null) throw new InvalidOperationException("A build is already in progress.");
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
@@ -63,6 +65,13 @@ public class BuildService(ILogger<BuildService> logger)
BuildFinished.InvokeParallelFireAndForget();
_cancellationTokenSource = null;
_logger.LogInformation(buildResult.Exception, "Build result: {BuildResult} in {ElapsedMilliseconds}ms", buildResult.OverallResult, timer.ElapsedMilliseconds);
var mappedResult = buildResult.OverallResult switch
{
BuildResultCode.Success => SharpIdeBuildResult.Success,
BuildResultCode.Failure => SharpIdeBuildResult.Failure,
_ => throw new ArgumentOutOfRangeException()
};
return mappedResult;
}
public async Task CancelBuildAsync()

View File

@@ -33,15 +33,23 @@ public partial class RunService(ILogger<RunService> logger, RoslynAnalysis rosly
var semaphoreSlim = _projectLocks.GetOrAdd(project, new SemaphoreSlim(1, 1));
var waitResult = await semaphoreSlim.WaitAsync(0).ConfigureAwait(false);
if (waitResult is false) throw new InvalidOperationException($"Project {project.Name} is already running.");
if (project.RunningCancellationTokenSource is not null) throw new InvalidOperationException($"Project {project.Name} is already running with a cancellation token source.");
project.RunningCancellationTokenSource = new CancellationTokenSource();
await _buildService.MsBuildAsync(project.FilePath);
var launchProfiles = await LaunchSettingsParser.GetLaunchSettingsProfiles(project);
var launchProfile = launchProfiles.FirstOrDefault();
try
{
if (project.RunningCancellationTokenSource is not null) throw new InvalidOperationException($"Project {project.Name} is already running with a cancellation token source.");
var buildResult = await _buildService.MsBuildAsync(project.FilePath);
if (buildResult is not SharpIdeBuildResult.Success)
{
_logger.LogInformation("Build failed for project {ProjectName}. Aborting run/debug.", project.Name);
return;
}
project.RunningCancellationTokenSource = new CancellationTokenSource();
var launchProfiles = await LaunchSettingsParser.GetLaunchSettingsProfiles(project);
var launchProfile = launchProfiles.FirstOrDefault();
var fileName = launchProfile?.CommandName switch
{
"Executable" => launchProfile.ExecutablePath,