Skip to content

Commit 6d86445

Browse files
committed
Surface unprefixed child stderr in bundle discovery failures
CreateDiscoveryFailure claimed to fall back to raw stderr for unprefixed failures, but both the stdout and stderr scans went through ExtractErrorMessage, which only keeps Reporter.ErrorPrefix lines. Host-level failures (framework roll-forward, missing runtime, unhandled exceptions) that write unprefixed text to stderr were dropped, leaving the user with the generic BundleContextDiscoveryFailed message. Add a real raw-stderr fallback (JoinRawErrorLines) as the last resort, keeping prefixed EF guidance (e.g. the multi-context --context hint) as the priority. Fix the stale comments that described the missing fallback.
1 parent 07e06ee commit 6d86445

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎src/ef/Commands/MigrationsBundleCommand.Discovery.cs‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,49 @@ internal static IReadOnlyList<string> BuildDiscoveryArguments(
9292

9393
// Builds the failure for a non-zero discovery child. Under --prefix-output the child writes its
9494
// prefixed error lines to STDOUT (Reporter.WriteError -> WriteStdErr -> WriteLine -> stdout), so
95-
// the actionable text is in the captured stdout, not stderr; scan stdout first and fall back to
96-
// the raw stderr capture for unprefixed failures. See https://github.com/dotnet/efcore/issues/25555.
95+
// the actionable text is in the captured stdout, not stderr; scan stdout first, then stderr for
96+
// prefixed lines, and finally fall back to the raw stderr capture for unprefixed failures.
97+
// See https://github.com/dotnet/efcore/issues/25555.
9798
internal static CommandException CreateDiscoveryFailure(string output, IEnumerable<string?> errorOutput)
9899
{
100+
var errorLines = errorOutput as IReadOnlyCollection<string?> ?? errorOutput.ToList();
101+
102+
// Prefer prefixed error lines (actionable EF guidance like the multi-context "--context" hint):
103+
// scan the child's stdout first (where --prefix-output routes its error: lines), then its stderr.
104+
// As a last resort fall back to the raw stderr text, so host-level failures that carry no
105+
// Reporter.ErrorPrefix (framework roll-forward, missing runtime, an unhandled exception) still
106+
// reach the user instead of collapsing into the generic message.
99107
var detail = ExtractErrorMessage(output.Split('\n').Select(l => l.TrimEnd('\r')))
100-
?? ExtractErrorMessage(errorOutput);
108+
?? ExtractErrorMessage(errorLines)
109+
?? JoinRawErrorLines(errorLines);
101110

102111
return new CommandException(
103112
detail != null
104113
? Resources.BundleContextDiscoveryFailedWithError(detail)
105114
: Resources.BundleContextDiscoveryFailed);
106115
}
107116

117+
// Joins the raw captured stderr lines verbatim, used as a last-resort fallback when the child
118+
// produced no Reporter.ErrorPrefix-prefixed error text.
119+
internal static string? JoinRawErrorLines(IEnumerable<string?> errorLines)
120+
{
121+
var builder = new StringBuilder();
122+
foreach (var line in errorLines)
123+
{
124+
if (!string.IsNullOrEmpty(line))
125+
{
126+
if (builder.Length > 0)
127+
{
128+
builder.Append(Environment.NewLine);
129+
}
130+
131+
builder.Append(line);
132+
}
133+
}
134+
135+
return builder.Length > 0 ? builder.ToString() : null;
136+
}
137+
108138
// Pulls the human-readable error text out of prefixed output lines (those beginning with
109139
// Reporter.ErrorPrefix), so actionable discovery errors (e.g. the multi-context "--context"
110140
// guidance) are surfaced instead of collapsed into a generic failure.

‎test/ef.Tests/MigrationsBundleDiscoveryTest.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,29 @@ public void CreateDiscoveryFailure_is_generic_when_no_error_lines_anywhere()
205205

206206
Assert.Equal(Properties.Resources.BundleContextDiscoveryFailed, ex.Message);
207207
}
208+
209+
[Fact]
210+
public void CreateDiscoveryFailure_falls_back_to_raw_stderr_when_unprefixed()
211+
{
212+
// Host-level failures (framework roll-forward, missing runtime, unhandled exceptions) write
213+
// unprefixed text to stderr. It must still reach the user rather than collapsing into the
214+
// generic message.
215+
var ex = MigrationsBundleCommand.CreateDiscoveryFailure(
216+
Reporter.InfoPrefix + "no error here",
217+
new[] { "A fatal error was encountered.", "The required framework was not found." });
218+
219+
Assert.Contains("A fatal error was encountered.", ex.Message);
220+
Assert.Contains("The required framework was not found.", ex.Message);
221+
}
222+
223+
[Fact]
224+
public void CreateDiscoveryFailure_prefers_prefixed_stderr_over_raw()
225+
{
226+
var ex = MigrationsBundleCommand.CreateDiscoveryFailure(
227+
Reporter.InfoPrefix + "no error here",
228+
new[] { "noise line", Reporter.ErrorPrefix + "Actionable error." });
229+
230+
Assert.Contains("Actionable error.", ex.Message);
231+
Assert.DoesNotContain("noise line", ex.Message);
232+
}
208233
}

0 commit comments

Comments
 (0)