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

fs::File.read_to_string(...) appends newline character #6943

Closed
MatthiasvB opened this issue Oct 29, 2024 · 5 comments
Closed

fs::File.read_to_string(...) appends newline character #6943

MatthiasvB opened this issue Oct 29, 2024 · 5 comments
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-fs Module: tokio/fs

Comments

@MatthiasvB
Copy link

Version
List the versions of all tokio crates you are using. The easiest way to get
this information is using cargo tree subcommand:

cargo tree | grep tokio
1.41.0

Platform
The output of uname -a (UNIX), or version and 32 or 64-bit (Windows)
Linux nixos 6.6.32 #1-NixOS SMP PREEMPT_DYNAMIC Sat May 25 14:22:56 UTC 2024 x86_64 GNU/Linux

Description
Enter your issue details here.
One way to structure the description:

[short summary of the bug]
I'm reading a file using

let master_key_file = env::var("MEILI_MASTER_KEY_FILE").expect("MEILI_MASTER_KEY_FILE must be set");
let mut master_key = String::new();
File::open(master_key_file).await.unwrap().read_to_string(&mut master_key).await.unwrap();

Now, master_key has an extra newline at the end, which breaks downstream code

I expected to see this happen: master_key should contain exactly the contents of the file

Instead, this happened: it contains an extra newline at the end

@MatthiasvB MatthiasvB added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Oct 29, 2024
@Darksonn Darksonn added the M-fs Module: tokio/fs label Oct 29, 2024
@Darksonn
Copy link
Contributor

I suspect your file probably contains an extra newline character :)

As an aside, tokio::fs::read is better suited for your purposes.

@MatthiasvB
Copy link
Author

I did check for that :). It does not

@mox692
Copy link
Member

mox692 commented Oct 29, 2024

@Darksonn
Copy link
Contributor

Have you tried with std::fs?

There really is nowhere for Tokio to insert a spurious newline character in the code you shared. I still think your file has a newline character, and that you're doing something wrong when checking it. I'm going to close this. If you want this issue reopened, I'm going to need really really compelling evidence that your file really does not have a newline character at the end.

Be aware that text editors usually don't show the last newline character as an additional empty line. Please see this SO question.

@Darksonn Darksonn closed this as not planned Won't fix, can't repro, duplicate, stale Oct 29, 2024
@MatthiasvB
Copy link
Author

You were right, I'm sorry. It's just surprisingly hard to get this right. I did not expect neovim to do nasty things like inserting invisible newlines (it only shows line numbers where content is, and there is no line number on the line after my content, but sure enough, there's a newline character at the end of the first line).

It gets worse, as it's even harder to test if that newline is there.

echo "newlinetest" > testnewline;

echo "/$(cat testnewline)/"
# -> /newlinetest/

The newline is there in the example above, but cat seems to omit it. Tokio, rightly so, does not.

Best way I found so far to not have it there is

echo -n "whatever you want" > "wherever_you_want"

or perhaps even better

printf '%s' "whatever you want" > "wherever you want"

as taken from here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-fs Module: tokio/fs
Projects
None yet
Development

No branches or pull requests

3 participants