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

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

Closed
SamCNexor opened this issue Jan 5, 2023 · 20 comments
Closed

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

SamCNexor opened this issue Jan 5, 2023 · 20 comments
Labels
C-bug Category: bug O-windows OS: Windows

Comments

@SamCNexor
Copy link

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]
@SamCNexor SamCNexor added the C-bug Category: bug label Jan 5, 2023
@SamCNexor
Copy link
Author

SamCNexor commented Jan 5, 2023

For reference, the output of cargo run with the RUST_BACKTRACE=1 and CARGO_LOG=trace environment variables set:

Show...
PS C:\Users\myname\playground> cargo run
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_ALIAS_RUN", parts: [("alias", 5), ("run", 11)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TERM", parts: [("term", 5)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_NET", parts: [("net", 5)] }  
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_HTTP", parts: [("http", 5)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_BUILD", parts: [("build", 5)] }
[2023-01-05T14:09:10Z TRACE cargo::util::toml] read_manifest; path=C:\Users\myname\playground\Cargo.toml; source-id=C:\Users\myname\playground
[2023-01-05T14:09:10Z DEBUG cargo::core::workspace] find_members - only me as a member
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TERM", parts: [("term", 5)] }
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] adding rustup info to rustc fingerprint
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] reusing existing rustc info cache
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] rustc info cache hit
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("rustflags", 35)] }      
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] rustc info cache hit
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] rustc info cache hit
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("rustflags", 35)] }      
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET", parts: [("target", 5)] }
[2023-01-05T14:09:10Z DEBUG cargo::util::config::target] Got all targets {}
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTDOCFLAGS", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("rustdocflags", 35)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("runner", 35)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("rustflags", 35)] }      
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("linker", 35)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("runner", 35)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("rustflags", 35)] }      
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("linker", 35)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_REGISTRY_INDEX", parts: [("registry", 5), ("index", 14)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_REGISTRIES_CRATES_IO_PROTOCOL", parts: [("registries", 5), ("crates-io", 16), ("protocol", 26)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_SOURCE", parts: [("source", 5)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_PATCH", parts: [("patch", 5)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_PATCH", parts: [("patch", 5)] }
[2023-01-05T14:09:10Z DEBUG cargo::ops::resolve] avoid_patch_ids={}
[2023-01-05T14:09:10Z TRACE cargo::ops::resolve] previous: graph: Graph {
      - playground v0.1.0 (C:\Users\myname\playground)
    }

    features: {
    }
[2023-01-05T14:09:10Z TRACE cargo::core::registry] clear_lock
[2023-01-05T14:09:10Z TRACE cargo::core::registry] register_lock: playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z DEBUG cargo::ops::resolve] attempting to prefer playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z DEBUG cargo::core::registry] load/locked   C:\Users\myname\playground
[2023-01-05T14:09:10Z TRACE cargo::core::registry] locking summary of playground v0.1.0 (C:\Users\myname\playground) 
[2023-01-05T14:09:10Z DEBUG cargo::core::resolver] initial activation: playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z TRACE cargo::core::resolver] activating playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z TRACE cargo::core::resolver] resolved: graph: Graph {
      - playground v0.1.0 (C:\Users\myname\playground)
    }

    features: {
    }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_PATHS", parts: [("paths", 5)] }
[2023-01-05T14:09:10Z DEBUG cargo::ops::resolve] avoid_patch_ids={}
[2023-01-05T14:09:10Z TRACE cargo::ops::resolve] previous: graph: Graph {
      - playground v0.1.0 (C:\Users\myname\playground)
    }

    features: {
    }
[2023-01-05T14:09:10Z TRACE cargo::core::registry] clear_lock
[2023-01-05T14:09:10Z TRACE cargo::core::registry] register_lock: playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z DEBUG cargo::ops::resolve] attempting to prefer playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z DEBUG cargo::core::registry] load/locked   C:\Users\myname\playground
[2023-01-05T14:09:10Z TRACE cargo::core::registry] locking summary of playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z DEBUG cargo::core::resolver] initial activation: playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z TRACE cargo::core::resolver] activating playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z TRACE cargo::core::resolver] resolved: graph: Graph {
      - playground v0.1.0 (C:\Users\myname\playground)
    }

    features: {
    }
[2023-01-05T14:09:10Z TRACE cargo::core::registry] getting packages; sources=1
[2023-01-05T14:09:10Z TRACE cargo::sources::path] getting packages; id=playground v0.1.0 (C:\Users\myname\playground)
[2023-01-05T14:09:10Z DEBUG cargo::core::package] playground v0.1.0 (C:\Users\myname\playground) doesn't need a download
[2023-01-05T14:09:10Z TRACE cargo::core::resolver::features] activate_pkg playground
[2023-01-05T14:09:10Z DEBUG cargo::core::resolver::features] features={
        (
            PackageId {
                name: "playground",
                version: "0.1.0",
                source: "C:\\Users\\myname\\playground",
            },
            NormalOrDev,
        ): {},
    }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_PROFILE_RELEASE", parts: [("profile", 5), ("release", 13)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_PROFILE_BENCH", parts: [("profile", 5), ("bench", 13)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_PROFILE_TEST", parts: [("profile", 5), ("test", 13)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_PROFILE_DEV", parts: [("profile", 5), ("dev", 13)] }
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] adding rustup info to rustc fingerprint
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] reusing existing rustc info cache
[2023-01-05T14:09:10Z DEBUG cargo::util::rustc] rustc info cache hit
[2023-01-05T14:09:10Z TRACE cargo::core::compiler::unit_dependencies] ALL UNIT DEPENDENCIES {
        Unit {
            pkg: Package {
                id: PackageId {
                    name: "playground",
                    version: "0.1.0",
                    source: "C:\\Users\\myname\\playground",
                },
                ..: "..",
            },
            target: TargetInner {
                name: "playground",
                doc: true,
                ..: with_path("C:\\Users\\myname\\playground\\src\\main.rs", Edition2021),
            },
            profile: Profile {
                ..: default_dev(),
            },
            kind: Target(
                CompileTarget {
                    name: "x86_64-pc-windows-msvc",
                },
            ),
            mode: Build,
            features: [],
            artifact: false,
            is_std: false,
            dep_hash: 0,
        }: [],
    }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("runner", 35)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER", parts: [("target", 5), ("x86_64-pc-windows-msvc", 12), ("runner", 35)] }
[2023-01-05T14:09:10Z DEBUG cargo::core::compiler::context::compilation_files] Target filenames: [OutputFile { path: "C:\\Users\\myname\\playground\\target\\debug\\deps\\playground.exe", hardlink: Some("C:\\Users\\myname\\playground\\target\\debug\\playground.exe"), export_path: None, flavor: Normal }, OutputFile { path: "C:\\Users\\myname\\playground\\target\\debug\\deps\\playground.pdb", hardlink: Some("C:\\Users\\myname\\playground\\target\\debug\\playground.pdb"), export_path: None, flavor: DebugInfo }]
[2023-01-05T14:09:10Z DEBUG cargo::core::compiler::fingerprint] fingerprint at: C:\Users\myname\playground\target\debug\.fingerprint\playground-39e3816e8516d9a9\bin-playground
[2023-01-05T14:09:10Z DEBUG cargo::core::compiler::fingerprint] max output mtime for "C:\\Users\\myname\\playground" is "C:\\Users\\myname\\playground\\target\\debug\\deps\\playground.exe" 13317400805.652277300s
[2023-01-05T14:09:10Z DEBUG cargo::core::compiler::fingerprint] all paths up-to-date relative to "C:\\Users\\myname\\playground\\target\\debug\\.fingerprint\\playground-39e3816e8516d9a9\\dep-bin-playground" mtime=13317400805.399922800s
[2023-01-05T14:09:10Z DEBUG cargo::core::compiler::fingerprint] filesystem up-to-date "C:\\Users\\myname\\playground"
[2023-01-05T14:09:10Z TRACE cargo::core::compiler::job_queue] queue: DependencyQueue {
        dep_map: {
            Unit {
                pkg: Package {
                    id: PackageId {
                        name: "playground",
                        version: "0.1.0",
                        source: "C:\\Users\\myname\\playground",
                    },
                    ..: "..",
                },
                target: TargetInner {
                    name: "playground",
                    doc: true,
                    ..: with_path("C:\\Users\\myname\\playground\\src\\main.rs", Edition2021),
                },
                profile: Profile {
                    ..: default_dev(),
                },
                kind: Host,
                mode: Build,
                features: [],
                artifact: false,
                is_std: false,
                dep_hash: 2202906307356721367,
            }: (
                {},
                Job { ... },
            ),
        },
        reverse_dep_map: {},
        priority: {
            Unit {
                pkg: Package {
                    id: PackageId {
                        name: "playground",
                        version: "0.1.0",
                        source: "C:\\Users\\myname\\playground",
                    },
                    ..: "..",
                },
                target: TargetInner {
                    name: "playground",
                    doc: true,
                    ..: with_path("C:\\Users\\myname\\playground\\src\\main.rs", Edition2021),
                },
                profile: Profile {
                    ..: default_dev(),
                },
                kind: Host,
                mode: Build,
                features: [],
                artifact: false,
                is_std: false,
                dep_hash: 2202906307356721367,
            }: 200,
        },
        cost: {
            Unit {
                pkg: Package {
                    id: PackageId {
                        name: "playground",
                        version: "0.1.0",
                        source: "C:\\Users\\myname\\playground",
                    },
                    ..: "..",
                },
                target: TargetInner {
                    name: "playground",
                    doc: true,
                    ..: with_path("C:\\Users\\myname\\playground\\src\\main.rs", Edition2021),
                },
                profile: Profile {
                    ..: default_dev(),
                },
                kind: Host,
                mode: Build,
                features: [],
                artifact: false,
                is_std: false,
                dep_hash: 2202906307356721367,
            }: 100,
        },
    }
[2023-01-05T14:09:10Z DEBUG cargo::core::compiler::job_queue] start 0: Unit { pkg: Package { id: PackageId { name: "playground", version: "0.1.0", source: "C:\\Users\\myname\\playground" }, ..: ".." }, target: TargetInner { name: "playground", doc: true, ..: with_path("C:\\Users\\myname\\playground\\src\\main.rs", Edition2021) }, profile: Profile { ..: default_dev() }, kind: Host, mode: Build, features: [], artifact: false, is_std: false, dep_hash: 2202906307356721367 }
[2023-01-05T14:09:10Z DEBUG cargo_util::paths] linking C:\Users\myname\playground\target\debug\deps\playground.exe to C:\Users\myname\playground\target\debug\playground.exe
[2023-01-05T14:09:10Z DEBUG cargo_util::paths] linking C:\Users\myname\playground\target\debug\deps\playground.pdb to C:\Users\myname\playground\target\debug\playground.pdb
[2023-01-05T14:09:10Z TRACE cargo::core::compiler::job_queue] tokens in use: 0, rustc_tokens: [], waiting_rustcs: [] (events this tick: 1)
[2023-01-05T14:09:10Z TRACE cargo::core::compiler::job_queue] end: JobId(0)
[2023-01-05T14:09:10Z DEBUG cargo::core::compiler::job_queue] end (Unit { pkg: Package { id: PackageId { name: "playground", version: "0.1.0", source: "C:\\Users\\myname\\playground" }, ..: ".." }, target: TargetInner { name: "playground", doc: true, ..: with_path("C:\\Users\\myname\\playground\\src\\main.rs", Edition2021) }, profile: Profile { ..: default_dev() }, kind: Host, mode: Build, features: [], artifact: false, is_std: false, dep_hash: 2202906307356721367 }): Ok(())
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_FUTURE_INCOMPAT_REPORT", parts: [("future-incompat-report", 5)] }
[2023-01-05T14:09:10Z TRACE cargo::util::config] get cv ConfigKey { env: "CARGO_ENV", parts: [("env", 5)] }
     Running `target\debug\playground.exe`
[2023-01-05T14:09:11Z DEBUG cargo] exit_with_error; err=CliError { error: Some(could not execute process `target\debug\playground.exe` (never executed)

    Caused by:
        Access is denied. (os error 5)), exit_code: 101 }
[2023-01-05T14:09:11Z DEBUG cargo] display_error; err=could not execute process `target\debug\playground.exe` (never executed)

    Caused by:
        Access is denied. (os error 5)
error: could not execute process `target\debug\playground.exe` (never executed)

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

@ehuss
Copy link
Contributor

ehuss commented Jan 5, 2023

Hm, that's quite strange! Some questions:

  • Can you say which anti-virus product (and version) you are using?
  • Which powershell are you using (type $PSVersionTable)?
  • What Is the execution policy (type Get-ExecutionPolicy -List)?
  • Are there any non-default system policies?

@SamCNexor
Copy link
Author

Our anti-virus is Windows Defender Antivirus. Versions here:

Antimalware Client Version: 4.18.2211.5
Engine Version: 1.1.19900.2
Antivirus Version: 1.381.1761.0
Anti-spyware Version: 1.381.1761.0

PowerShell version:

PS C:\Users\myname\playground> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.2364
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.2364
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Execution policy:

PS C:\Users\myname\playground> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy    RemoteSigned
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined

Yes, I think there are non-default system policies, but I don't know them in detail I'm afraid.

From your questions it sounds like this might just be my environment not allowing me access - I will talk to the policy issuers and see if they can give any more insight.

@ehuss
Copy link
Contributor

ehuss commented Jan 5, 2023

Hm. RemoteSigned is not the default, but I tested on my local machine and it didn't seem to make a difference.

Another random thought is to check if there are any AppLocker policies. I think those can be listed with Get-AppLockerPolicy -Effective.

Another place to check is the Event Viewer and scrounge around to see if there are any events logged when the Access is Denied error is raised.

Otherwise, I'm not familiar with Windows policies or anti-virus controls, so I probably won't be able to help much more. Perhaps there are some Windows experts at one of the forums like https://users.rust-lang.org/?

@SamCNexor
Copy link
Author

AppLocker policies:

PS C:\Users\myname\playground> Get-AppLockerPolicy -Effective

Version RuleCollections RuleCollectionTypes
------- --------------- -------------------
      1 {}              {}

I will investigate the Event Viewer and update this thread if I find anything interesting.

It seems right now that my problem is being caused by a policy on my machine causing the issue, not necessarily a problem with cargo itself. I'll continue investigating. Thanks for your help so far.

@weihanglo
Copy link
Member

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!

@linsaftw
Copy link

I have the same error. I am new to this programming language. I am using Windows 11.

@SamCNexor
Copy link
Author

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.

@linsaftw
Copy link

In my case it was avast hardened mode.

@muratozgul
Copy link

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

@zhongdongy
Copy link

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

image

@waki285
Copy link

waki285 commented Sep 17, 2023

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

@askor
Copy link

askor commented Oct 6, 2023

On Windows 10 I had to click

Virus & threat protection > Virus & threat protection: Manage settings > Exclusions: Add or remove exclusions > + Add an exclusion

@carsonkrueger
Copy link

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

image

This fixed it for me, thanks

@oisyn
Copy link

oisyn commented Oct 18, 2023

Windows Security was the culprit for me as well. I hooked up ProcMon to see what exactly gave the error, and it was a call to SetRenameInformationFile on file egui_node_graph\egui_node_graph_example\target\wasm32-unknown-unknown3X35gS
context:

ReplaceIfExists: True,
FileName: egui_node_graph\egui_node_graph_example\target\wasm32-unknown-unknown

Note that I was running trunk serve which in turn called cargo, don't know whether that matters.

@ChrisDenton
Copy link
Member

ChrisDenton commented Oct 18, 2023

Hm, my guess is that happens because the directory wasm32-unknown-unknown unexpectedly still exists. The documentation for Rust's std::fs::rename notes that it will fail if a directory already exists at the destination.

Edit: actually a better guess is the security software has a lock on something in the source directory. This would also prevent renaming the ancestor directories. A quick fix would be to keep retrying the operation with a back-off until either it succeeds or some arbitrary limit is reached (whichever comes first).

@Traxpoint
Copy link

Seems it comes with last MS or similar updates. New projects won't work like described above. Older ones are working for me. Cargo run then does it without OS error 5.

@Mergpijp
Copy link

I did a cargo clean and the deleted cargo.lock it seems fine now

@ojob
Copy link

ojob commented Dec 9, 2024

I did a cargo clean and the deleted cargo.lock it seems fine now

It seemed it would work for me, but alas it failed with same OS error. After some trials, it does not always stop at the same point thought.

Last week, my company installed an upgrade of the antivirus on my development computer, and it really seems to be the root cause.

@ChrisDenton
Copy link
Member

If you're able to use dev drive on your developer computer then that might improve the situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug O-windows OS: Windows
Projects
None yet
Development

No branches or pull requests