[Fixed] Rust Cargo Build Error. Access is Denied. OS Error 5 – Rust

by
Ali Hasan
rust-analyzer rust-cargo

Quick Fix: In PowerShell, ensure the execution policy is set to "Remote Signed" or "Unrestricted" to allow script execution. Use "Get-ExecutionPolicy" to check the current policy and "Set-ExecutionPolicy" to modify it.

The Problem:

While trying to build a Rust project using Cargo, the user encounters an "Access is denied. (os error 5)" error, despite reinstalling Rust with antivirus disabled. The issue seems to be related to the project’s location on the D drive and not being in the System32 directory. However, running the "cargo build" command from PowerShell works fine.

The Solutions:

Solution 1: Check and Modify PowerShell Execution Policy

Title: Adjusting PowerShell Execution Policy to Resolve Cargo Build Error

Solution:

  1. Examine Current Execution Policy:

    Begin by verifying your current PowerShell execution policy using the command:

    Get-ExecutionPolicy
    

    The expected output should be "Restricted".

  2. Adjust Execution Policy:

    To alleviate the cargo build error, you need to grant the necessary permissions by modifying the execution policy.

    a. Open PowerShell as Administrator:

    Ensure you are running PowerShell with administrative privileges.

    b. Set Execution Policy:

    Use one of the following commands to set the execution policy to either "Remote Signed" or "Unrestricted":

    Option 1: Remote Signed:

    Set-ExecutionPolicy RemoteSigned
    

    Option 2: Unrestricted:

    Set-ExecutionPolicy Unrestricted
    
  3. Confirm Policy Change:

    Rerun the "Get-ExecutionPolicy" command to verify that the policy has been successfully changed.

  4. Test Cargo Build:

    Return to the Cargo build command and attempt to compile your project again.

    cargo build
    

    You should no longer encounter the "Access is denied" error.

This solution addresses the root cause of the error, which is the restrictive PowerShell execution policy. By modifying the policy, you grant the necessary permissions for the cargo build process to proceed smoothly.

Solution 2: Give your user read and write permission

In case you encounter the “Access is denied. (os error 5)” error while running Cargo commands from Visual Studio Code, the issue may lie with file permissions. To resolve this, follow these steps:

  1. Navigate to the folder containing your Rust project in File Explorer.

  2. Right-click on the folder and select "Properties".

  3. Click on the "Security" tab.

  4. Under "Group or user names", select your user account.

  5. Ensure that the "Read & execute" and "Write" permissions are checked.

  6. Click "Apply" and then "OK" to save the changes.

  7. Return to Visual Studio Code and try running the Cargo command again.

This should resolve the "Access is denied. (os error 5)" error and allow you to build and run your Rust project from within VS Code.

Q&A

Why I am getting, Cargo Build error: Access is Denied. OS Error 5, on VS Code?

You might need to change your permission grants on the PowerShell

What is the command to check execution policy in Powershell?

Get-ExecutionPolicy

What should be the execution policy set to?

Remote Signed or Unrestricted

Video Explanation:

The following video, titled "2024 Fix Error 5: Access is denied when installing software - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Try Fortect to fix and optimize your PC like a Pro: ...