Skip to content

Stdout not printing until after reading stdin when using terminal #2164

@mpfaff

Description

@mpfaff

I've noticed that stdout output and stdin input don't seem to be happening in the correct order if I set "console": "terminal" in my launch config. I'm guessing that maybe Dart-Code is caching the stdout output for some reason.

import 'dart:convert';
import 'dart:io';

import 'package:io/io.dart';

final Stream<String> stdinUtf8 = sharedStdIn.transform(utf8.decoder).asBroadcastStream();
final Stream<String> stdinLined = stdinUtf8.transform(LineSplitter());

Future<T> prompt<T>(String question, {T fallback, T def, String hint, T Function(String) parse}) async {
  assert(T is String || parse != null, '`parse` can only be null if T is String. T is $T');
  stdout.write('$question${hint != null || def != null ? ' (${hint ?? def})' : ''} >>> ');
  final ret = await stdinLined.first;
  print('[OUTPUT = $ret]'); // for debugging
  if (ret.isEmpty && fallback != null) return def;
  else if (parse != null) {
    try {
      return parse(ret);
    } on FormatException {
      return fallback;
    }
  }

  return ret as T;
}

Example:

Future<void> main() async {
  final name = await prompt<String>('What is your name?');
}

Expected Output (works in non-debug terminal):

$ dart bin/main.dart
What is your name? >>> <some user input>
[OUTPUT = <some user input>]

Actual Output:

$ dart bin/main.dart
<some user input>
What is your name? >>> [OUTPUT = <some user input>]

Metadata

Metadata

Assignees

No one assigned

    Labels

    in dartRelates to running Dart programsin debuggingRelates to the debug adapter or process of running debug sessionsis bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions