@@ -32,6 +32,7 @@ final class CliServerManager {
3232 private static final Logger LOG = Logger .getLogger (CliServerManager .class .getName ());
3333
3434 private final CopilotClientOptions options ;
35+ private final StringBuilder stderrBuffer = new StringBuilder ();
3536
3637 CliServerManager (CopilotClientOptions options ) {
3738 this .options = options ;
@@ -47,6 +48,8 @@ final class CliServerManager {
4748 * if interrupted while waiting for port detection
4849 */
4950 ProcessInfo startCliServer () throws IOException , InterruptedException {
51+ clearStderrBuffer ();
52+
5053 String cliPath = options .getCliPath () != null ? options .getCliPath () : "copilot" ;
5154 var args = new ArrayList <String >();
5255
@@ -152,6 +155,9 @@ private void startStderrReader(Process process) {
152155 new InputStreamReader (process .getErrorStream (), StandardCharsets .UTF_8 ))) {
153156 String line ;
154157 while ((line = reader .readLine ()) != null ) {
158+ synchronized (stderrBuffer ) {
159+ stderrBuffer .append (line ).append ('\n' );
160+ }
155161 LOG .fine ("[CLI] " + line );
156162 }
157163 } catch (IOException e ) {
@@ -171,6 +177,10 @@ private Integer waitForPortAnnouncement(Process process) throws IOException {
171177 while (System .currentTimeMillis () < deadline ) {
172178 String line = reader .readLine ();
173179 if (line == null ) {
180+ String stderr = getStderrOutput ();
181+ if (!stderr .isEmpty ()) {
182+ throw new IOException ("CLI process exited unexpectedly. stderr: " + stderr );
183+ }
174184 throw new IOException ("CLI process exited unexpectedly" );
175185 }
176186
@@ -185,6 +195,18 @@ private Integer waitForPortAnnouncement(Process process) throws IOException {
185195 }
186196 }
187197
198+ String getStderrOutput () {
199+ synchronized (stderrBuffer ) {
200+ return stderrBuffer .toString ().trim ();
201+ }
202+ }
203+
204+ private void clearStderrBuffer () {
205+ synchronized (stderrBuffer ) {
206+ stderrBuffer .setLength (0 );
207+ }
208+ }
209+
188210 private List <String > resolveCliCommand (String cliPath , List <String > args ) {
189211 boolean isJsFile = cliPath .toLowerCase ().endsWith (".js" );
190212
0 commit comments