cargo: `cargo run` fails with `Access is denied. (os error 5)` on Windows 10

Problem

Following the instructions on https://www.rust-lang.org/tools/install I used rustup-init.exe to install Rust on my Windows 10 machine.

I created a new Rust project by running cargo new playground in PowerShell, changed to the new directory with cd \.playground\, and ran cargo run. The error I get is as follows:

PS C:\Users\myname\playground> cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target\debug\playground.exe`
error: could not execute process `target\debug\playground.exe` (never executed)

Caused by:
  Access is denied. (os error 5)

However, if I run the binary directly in PowerShell (after cargo build or attempting cargo run), it works:

PS C:\Users\myname\playground> .\target\debug\playground.exe
Hello, world!

I looked up several similar issues to this, but these didn’t lead to any solutions:

Things I have tried so far which don’t fix the problem:

  • Re-running the rustup-init.exe installer
  • Restarting my machine
  • Compiling the source code directly with rustc, copying the binary into target\debug\playground.exe and then running cargo run
  • cargo run with RUST_BACKTRACE=1 and CARGO_LOG=trace environment variables to try and diagnose the issue - no extra useful output
  • Checking the file/folder permissions on the binary and the target directory - permissions look fine to me
  • Re-doing the above steps but using Command Prompt instead of PowerShell
    • This was interesting because executing the binary directly produces an Access is denied. error, whereas this approach works in PowerShell
  • Re-running the rustup-init.exe installer as administrator (without uninstalling as a regular user first)

One thing I have not tried yet is disabling my antivirus, because this is managed by my company. However, I have seen people with a similar issue to myself (see links above) and they mention that disabling antivirus did not fix the issue for them.

Steps

  1. Install Rust via https://www.rust-lang.org/tools/install on a Windows 10 machine
  2. Open PowerShell
  3. Create a new Rust project with cargo new playground
  4. In the new project directory run cargo run (should produce the error)
  5. Run the binary directly with .\target\debug\playground.exe (should work successfully)
  6. Open Command Prompt
  7. In the project directory run cargo run (should produce the error)
  8. Run the binary directly with .\target\debug\playground.exe` (should produce the error)

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.66.0 (d65d197ad 2022-11-15)
release: 1.66.0
commit-hash: d65d197ad5c6c09234369f219f943e291d4f04b9
commit-date: 2022-11-15
host: x86_64-pc-windows-msvc
libgit2: 1.5.0 (sys:0.15.0 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:Schannel)
os: Windows 10.0.19045 (Windows 10 Pro) [64-bit]

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 17 (4 by maintainers)

Most upvoted comments

I found a workaround for this after some more searching: https://stackoverflow.com/a/74170163

Adding the Rust workspace folder to Microsoft Defender’s exclusion list prevents the problem from happening. image

I’m not sure there is anything Cargo can do in this situation, it’s more related to OS security policies than the program itself, so I am happy if this thread stays closed. Thanks for everyone’s help.

Just add the project directory to Windows Security program exclusion list and it should be fine.

image

Hey. Thank you for helping the investigation. I am going to close this issue to make the way too long issue backlog less scary. If you find anything interesting or any improvement Cargo can do for this situation, feel free to comment below or open a new issue. Thank you again!

In my case, the program was already running in Visual Studio Code’s terminal and I was trying cargo run in powershell

Just add the project directory to Windows Security program exclusion list and it should be fine.

image

This fixed it for me, thanks

in my case, it was by ESET antivirus. disabled it to solved