Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readLines always yield empty "" at EOF? #986

Closed
t829702 opened this issue Jun 27, 2021 · 0 comments · Fixed by #990
Closed

readLines always yield empty "" at EOF? #986

t829702 opened this issue Jun 27, 2021 · 0 comments · Fixed by #990
Labels
bug Something isn't working needs triage

Comments

@t829702
Copy link

t829702 commented Jun 27, 2021

Describe the bug A clear and concise description of what the bug is.

it seems Deno's readLines always yield empty "" at EOF, while all other known Programming languages' similar line scanning interfaces don't do this

import { readLines } from "https://deno.land/[email protected]/io/mod.ts";

for await (const line of readLines(Deno.stdin)) {
  console.log(new Date, 'got line:', { line });
}

To Reproduce Steps to reproduce the behavior:

$ seq 3 | deno run readlines-deno.js
2021-06-27T10:23:08.520Z got line: { line: "1" }
2021-06-27T10:23:08.522Z got line: { line: "2" }
2021-06-27T10:23:08.522Z got line: { line: "3" }
2021-06-27T10:23:08.522Z got line: { line: "" }

Expected behavior A clear and concise description of what you expected to
happen.

Node's builtin readline ::

const rl = readline.createInterface({
  input: process.stdin,
});

(async function () {
  for await (const line of rl) {
    console.log(new Date, 'got line:', { line });
  }
})()
$ seq 3 | node ./readline-node.js
2021-06-27T10:26:11.352Z got line: { line: '1' }
2021-06-27T10:26:11.357Z got line: { line: '2' }
2021-06-27T10:26:11.358Z got line: { line: '3' }

Go's example scanner lines https://golang.org/pkg/bufio/#example_Scanner_lines

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		fmt.Println(scanner.Text()) // Println will add back the final '\n'
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
	}
}
$ seq 3 | ./readlines-go
1
2
3

and Python:

$ seq 3 | python -c 'import sys;
for line in sys.stdin:
    print("line:", line)' 
('line:', '1\n')
('line:', '2\n')
('line:', '3\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant