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

When importing txt files, "\n"s in the txt file are converted to newline #9552

Open
lirc571 opened this issue Mar 22, 2024 · 4 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@lirc571
Copy link

lirc571 commented Mar 22, 2024

What version of Bun is running?

1.0.33+9e91e137f

What platform is your computer?

Linux 5.15.133.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

text.txt:

printf("Hello, world\n");

index.ts:

import text from "./text.txt";

console.log(text);

bun run index.ts

What is the expected behavior?

The following should be printed to the console:

printf("Hello, world\n");

What do you see instead?

printf("Hello, world
");

Additional information

txt files should be loaded as is.

@lirc571 lirc571 added the bug Something isn't working label Mar 22, 2024
@jdalton
Copy link
Contributor

jdalton commented Mar 29, 2024

Hi @lirc571. Thanks for raising this.

I don't believe this is a bug. If imported text is a string with the content as is. When you pass the text as is to console.log it will interpret the \n as such. What you may be wanting is to escape newlines before passing it to console.log. Something like console.log(text.replaceAll("\n", "\\n")).

@jdalton jdalton closed this as completed Mar 29, 2024
@lirc571
Copy link
Author

lirc571 commented Mar 29, 2024

Hi @jdalton , the problem is not with console.log. The text file should be read as "printf(\"Hello, world\\n\");", but Bun instead changed the \n to a newline character.

I believe a file reading operation should treat the file's content as is. The 2 snippets below should produce the same result:

import textFromImport from "./text.txt";

vs

const file = Bun.file("./text.txt");
const textFromFile = await file.text();

But they are different.

@lirc571
Copy link
Author

lirc571 commented Mar 29, 2024

@jdalton jdalton reopened this Mar 29, 2024
@jdalton
Copy link
Contributor

jdalton commented Mar 29, 2024

Thank you @lirc571!

I also verified the difference in Node:

import fs from "fs";
const file = fs.readFileSync("./text.txt", "utf-8");
console.log(file); // logs printf("Hello, world\n");

@jdalton jdalton self-assigned this Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants