|
5 | 5 | using System.Text.Json; |
6 | 6 | #endif |
7 | 7 | using System.Text; |
| 8 | +using Microsoft.EntityFrameworkCore.Design; |
8 | 9 | using Microsoft.EntityFrameworkCore.Tools.Properties; |
9 | 10 |
|
10 | 11 | namespace Microsoft.EntityFrameworkCore.Tools.Commands; |
@@ -90,141 +91,134 @@ internal static IReadOnlyList<string> BuildDiscoveryArguments( |
90 | 91 | return args; |
91 | 92 | } |
92 | 93 |
|
93 | | - // Builds the failure for a non-zero discovery child. Under --prefix-output the child writes its |
94 | | - // 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, then stderr for |
96 | | - // prefixed lines, and finally fall back to the raw stderr capture for unprefixed failures. |
| 94 | + // Routes one line of the discovery child's output as it arrives. The child runs with |
| 95 | + // --prefix-output, so everything ef.dll itself reports (error/warn/info/verbose, and the data: |
| 96 | + // lines carrying the result) is tagged, and all of it is written to stdout. Data payloads are |
| 97 | + // collected for ParseContextType; tagged diagnostics go to the matching report method, so the |
| 98 | + // user sees actionable guidance (e.g. the multi-context "--context" hint) immediately, without |
| 99 | + // waiting for the child to exit. Untagged lines are output from the user's application or the |
| 100 | + // runtime host (an unhandled exception, a framework roll-forward failure, startup noise): they are |
| 101 | + // collected in 'other' so the caller can show them as errors if the child fails, or as verbose |
| 102 | + // output if it succeeds. Callbacks for stdout and stderr arrive on different threads, so the |
| 103 | + // collections are guarded by a lock on 'data'. |
97 | 104 | // See https://github.com/dotnet/efcore/issues/25555. |
98 | | - internal static CommandException CreateDiscoveryFailure(string output, IEnumerable<string?> errorOutput) |
| 105 | + internal static void HandleDiscoveryOutput( |
| 106 | + string? line, |
| 107 | + StringBuilder data, |
| 108 | + List<string> other, |
| 109 | + Action<string> onError, |
| 110 | + Action<string> onWarning, |
| 111 | + Action<string> onInformation, |
| 112 | + Action<string> onVerbose) |
99 | 113 | { |
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. |
107 | | - var detail = ExtractErrorMessage(output.Split('\n').Select(l => l.TrimEnd('\r'))) |
108 | | - ?? ExtractErrorMessage(errorLines) |
109 | | - ?? JoinRawErrorLines(errorLines); |
110 | | - |
111 | | - return new CommandException( |
112 | | - detail != null |
113 | | - ? Resources.BundleContextDiscoveryFailedWithError(detail) |
114 | | - : Resources.BundleContextDiscoveryFailed); |
115 | | - } |
| 114 | + line = line?.TrimEnd('\r'); |
| 115 | + if (line == null || line.Length == 0) |
| 116 | + { |
| 117 | + return; |
| 118 | + } |
116 | 119 |
|
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) |
| 120 | + if (line.StartsWith(Reporter.DataPrefix, StringComparison.Ordinal)) |
123 | 121 | { |
124 | | - if (!string.IsNullOrEmpty(line)) |
| 122 | + lock (data) |
125 | 123 | { |
126 | | - if (builder.Length > 0) |
127 | | - { |
128 | | - builder.Append(Environment.NewLine); |
129 | | - } |
130 | | - |
131 | | - builder.Append(line); |
| 124 | + data.Append(line, Reporter.DataPrefix.Length, line.Length - Reporter.DataPrefix.Length); |
132 | 125 | } |
133 | 126 | } |
134 | | - |
135 | | - return builder.Length > 0 ? builder.ToString() : null; |
136 | | - } |
137 | | - |
138 | | - // Pulls the human-readable error text out of prefixed output lines (those beginning with |
139 | | - // Reporter.ErrorPrefix), so actionable discovery errors (e.g. the multi-context "--context" |
140 | | - // guidance) are surfaced instead of collapsed into a generic failure. |
141 | | - internal static string? ExtractErrorMessage(IEnumerable<string?> errorLines) |
142 | | - { |
143 | | - var builder = new StringBuilder(); |
144 | | - foreach (var line in errorLines) |
| 127 | + else if (line.StartsWith(Reporter.ErrorPrefix, StringComparison.Ordinal)) |
145 | 128 | { |
146 | | - if (line != null |
147 | | - && line.StartsWith(Reporter.ErrorPrefix, StringComparison.Ordinal)) |
| 129 | + onError(line.Substring(Reporter.ErrorPrefix.Length)); |
| 130 | + } |
| 131 | + else if (line.StartsWith(Reporter.WarningPrefix, StringComparison.Ordinal)) |
| 132 | + { |
| 133 | + onWarning(line.Substring(Reporter.WarningPrefix.Length)); |
| 134 | + } |
| 135 | + else if (line.StartsWith(Reporter.InfoPrefix, StringComparison.Ordinal)) |
| 136 | + { |
| 137 | + onInformation(line.Substring(Reporter.InfoPrefix.Length)); |
| 138 | + } |
| 139 | + else if (line.StartsWith(Reporter.VerbosePrefix, StringComparison.Ordinal)) |
| 140 | + { |
| 141 | + onVerbose(line.Substring(Reporter.VerbosePrefix.Length)); |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + lock (data) |
148 | 146 | { |
149 | | - if (builder.Length > 0) |
150 | | - { |
151 | | - builder.Append(Environment.NewLine); |
152 | | - } |
153 | | - |
154 | | - builder.Append(line.Substring(Reporter.ErrorPrefix.Length)); |
| 147 | + other.Add(line); |
155 | 148 | } |
156 | 149 | } |
157 | | - |
158 | | - return builder.Length > 0 ? builder.ToString() : null; |
159 | 150 | } |
160 | 151 |
|
161 | 152 | private string DiscoverContextType(IReadOnlyList<string> applicationArgs) |
| 153 | + => DiscoverContextType( |
| 154 | + applicationArgs, |
| 155 | + (args, handleLine) => Exe.Run("dotnet", args, handleOutput: handleLine, handleError: handleLine), |
| 156 | + Reporter.WriteError, |
| 157 | + Reporter.WriteWarning, |
| 158 | + Reporter.WriteInformation, |
| 159 | + Reporter.WriteVerbose); |
| 160 | + |
| 161 | + // 'run' launches the child with the given arguments, passing each stdout/stderr line to the supplied |
| 162 | + // callback, and returns its exit code. Split out from the Exe.Run call so it can be tested. |
| 163 | + internal string DiscoverContextType( |
| 164 | + IReadOnlyList<string> applicationArgs, |
| 165 | + Func<IReadOnlyList<string>, Action<string?>, int> run, |
| 166 | + Action<string> onError, |
| 167 | + Action<string> onWarning, |
| 168 | + Action<string> onInformation, |
| 169 | + Action<string> onVerbose) |
162 | 170 | { |
163 | 171 | #if NET |
164 | 172 | var efAssemblyPath = typeof(Program).Assembly.Location; |
165 | 173 | var discoveryArgs = BuildDiscoveryArguments( |
166 | 174 | efAssemblyPath, |
167 | | - DepsFile!.Value(), |
168 | | - RuntimeConfig!.Value(), |
169 | | - FxVersion!.Value(), |
170 | | - AdditionalProbingPaths!.Values.Where(v => v != null).Cast<string>().ToList(), |
| 175 | + _depsFile!.Value(), |
| 176 | + _runtimeConfig!.Value(), |
| 177 | + _fxVersion!.Value(), |
| 178 | + _additionalProbingPaths!.Values.Where(v => v != null).Cast<string>().ToList(), |
171 | 179 | GetCommonProjectArgs(), |
172 | 180 | Context!.Value(), |
173 | 181 | Reporter.IsVerbose, |
174 | 182 | applicationArgs); |
175 | 183 |
|
176 | | - var output = new StringBuilder(); |
177 | | - var errorOutput = new List<string?>(); |
178 | | - var exitCode = Exe.Run( |
179 | | - "dotnet", |
| 184 | + var data = new StringBuilder(); |
| 185 | + var other = new List<string>(); |
| 186 | + |
| 187 | + var exitCode = run( |
180 | 188 | discoveryArgs, |
181 | | - handleOutput: line => |
182 | | - { |
183 | | - if (!string.IsNullOrEmpty(line)) |
184 | | - { |
185 | | - output.AppendLine(line); |
186 | | - } |
187 | | - }, |
188 | | - handleError: line => |
189 | | - { |
190 | | - if (!string.IsNullOrEmpty(line)) |
191 | | - { |
192 | | - errorOutput.Add(line); |
193 | | - Reporter.WriteVerbose(line); |
194 | | - } |
195 | | - }); |
| 189 | + line => HandleDiscoveryOutput(line, data, other, onError, onWarning, onInformation, onVerbose)); |
196 | 190 |
|
197 | 191 | if (exitCode != 0) |
198 | 192 | { |
199 | | - throw CreateDiscoveryFailure(output.ToString(), errorOutput); |
| 193 | + // The tagged diagnostics have already been reported. Untagged output is most likely the |
| 194 | + // reason for a failure the child could not report itself (unhandled exception, missing |
| 195 | + // framework), so show it now. |
| 196 | + foreach (var line in other) |
| 197 | + { |
| 198 | + onError(line); |
| 199 | + } |
| 200 | + |
| 201 | + throw new CommandException(Resources.BundleContextDiscoveryFailed); |
| 202 | + } |
| 203 | + |
| 204 | + foreach (var line in other) |
| 205 | + { |
| 206 | + onVerbose(line); |
200 | 207 | } |
201 | 208 |
|
202 | | - return ParseContextType(output.ToString()); |
| 209 | + return ParseContextType(data.ToString()); |
203 | 210 | #else |
204 | 211 | // The .NET Framework build of ef.dll is a build-only reference (see ef.csproj) and is never |
205 | 212 | // executed by dotnet-ef, which always runs tools/net/ef.dll. Bundling is a .NET-only path. |
206 | 213 | throw new NotSupportedException(); |
207 | 214 | #endif |
208 | 215 | } |
209 | 216 |
|
210 | | - // Extracts the "type" field from the JSON emitted by `dbcontext info --json`. The child runs with |
211 | | - // --prefix-output, so its structured data lines start with Reporter.DataPrefix; only those lines |
212 | | - // are considered, so arbitrary stdout the user's app prints during discovery cannot corrupt the |
213 | | - // JSON. See https://github.com/dotnet/efcore/issues/25555. |
214 | | - internal static string ParseContextType(string output) |
| 217 | + // Extracts the "type" field from the JSON that `dbcontext info --json` emitted, as collected from |
| 218 | + // the child's data: lines by HandleDiscoveryOutput. See https://github.com/dotnet/efcore/issues/25555. |
| 219 | + internal static string ParseContextType(string json) |
215 | 220 | { |
216 | 221 | #if NET |
217 | | - var builder = new StringBuilder(); |
218 | | - foreach (var rawLine in output.Split('\n')) |
219 | | - { |
220 | | - var line = rawLine.TrimEnd('\r'); |
221 | | - if (line.StartsWith(Reporter.DataPrefix, StringComparison.Ordinal)) |
222 | | - { |
223 | | - builder.Append(line.Substring(Reporter.DataPrefix.Length)); |
224 | | - } |
225 | | - } |
226 | | - |
227 | | - var json = builder.ToString(); |
228 | 222 | var start = json.IndexOf('{'); |
229 | 223 | var end = json.LastIndexOf('}'); |
230 | 224 | if (start >= 0 && end > start) |
|
0 commit comments