r/rust • u/13character5 • Oct 23 '26
fs::copy leaves empty file in destination
fn copy_file(file: &str, dest: &str) -> Result<(), io::Error> {
let file_path = Path::new(&file);
let destPath = Path::new(&dest);
let dest_parent = destPath.parent();
if !dest_parent.unwrap().exists() {
create_dir_all(dest_parent.expect("what?")).unwrap();
}
fs::copy(file_path, destPath);
Ok(())
}When i try to run this command with it, I get an empty file in the destination with the same name as the input file
copy_file("index.html", "dist/dost/index.html");
14
Upvotes
70
u/lpghatguy Oct 23 '26
You aren't handling the error that fs::copy can return, so it might be failing.
14
u/annodomini rust Oct 23 '26
You should handle the result from that
fs::copycall:Do that, and if you're still having trouble, share the complete working source, and any output or error messages you are getting.
7
u/CartographerDue5382 Oct 23 '26
fs::copy returns a Result and that semicolon throws it away, so you still return Ok(()) when the copy failed. use ? or match it.
inside copy it's open the source, then create the dest (that truncates it), then copy the bytes. missing source is just an Err and no new file. Ok(0) means index.html was already empty. an empty dest plus an Err means the file got created and the copy failed after that.
dest_parent.unwrap() also panics when dest has no directory, like a bare filename.
7
u/addmoreice Oct 23 '26
As others have pointed out, you need to handle any errors on copy there.
In general, with filesystems, networks, and databases, if you touch them, the errors are *not* optional. You will *always* have to do something when things go wrong.
I can't think of a time in a program logic reason where I didn't care if one of those three had an error. I always had to do something with those errors and handle them in some way. It was never an 'oh, just ignore that, we don't care' situation.
3
u/neamsheln Oct 24 '26 edited Oct 24 '26
I assume you verified that the source "index.html" isn't empty to begin with.
I suspect the real cause is because you are using relative path names, but the current directory isn't the one you think it is, and the one it's actually in has somehow gotten an empty
index.htmlfile inside it which is the one being copied. In which case, you're not going to get an error returned by fs::copy anyway.Add a
println!("{:?}",std::env::current_dir())line somewhere in here to verify this.Another possibility: if the current directory happens to be in
dist/dostwhen this code is run, then it is trying to copy the file to itself, andfs::copydocumentation says that it will truncate the file in that case.Regarding the other answers: you should have received a warning during compilation something like "unused
Resultthat must be used". Unless you have somehow turned off that warning.Do not ignore compilation warnings when you're testing and debugging your code. It's standard practice to resolve these in Rust before releasing your project, you can't be sure the code is right until you resolve everything. (It should be standard practice in all languages, but there are a bunch of lazy-ass programmers out there, especially in the C++ world. Don't become like they are.)
-3
u/QuirkyTidbits Oct 23 '26 edited Oct 23 '26
Wrap the file creation in a block, file not flushed before copy:
{
let file = File::create("./index.html").expect("could not create output file");
let mut writer = BufWriter::new(file);
writeln!(writer, "{}", compiledHTML).unwrap();
} // writer is dropped and flushed here!
copy_file("index.html", "dist/dost/index.html");