Skip to content

nightly cargo can no longer build anything due to error: failed to create directory ...\target Caused by: Incorrect function. (os error 1) #137499

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

Closed
nabijaczleweli opened this issue Feb 23, 2025 · 26 comments · Fixed by #137528 · May be fixed by #138133
Labels
C-bug Category: This is a bug. O-windows-gnu Toolchain: GNU, Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@nabijaczleweli
Copy link
Contributor

nabijaczleweli commented Feb 23, 2025

I tried this code:

cargo +nightly build

I expected to see this happen: the crate builds

Instead, this happened:

[some "Fetching"s happen on first run]
error: failed to create directory `s:\imgdiff-real\target`

Caused by:
  Incorrect function. (os error 1)

Happens to every crate any where I put it. I can obviously manually create this. Stable can build these too.

Meta

rustc +nightly --version --verbose:

rustc 1.87.0-nightly (46420c960 2025-02-22)
binary: rustc
commit-hash: 46420c96070b4c4bd8242f16d5806b8f26a57016
commit-date: 2025-02-22
host: x86_64-pc-windows-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Stable still works

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: x86_64-pc-windows-gnu
release: 1.84.1
LLVM version: 19.1.5
@nabijaczleweli nabijaczleweli added the C-bug Category: This is a bug. label Feb 23, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 23, 2025
@ChrisDenton
Copy link
Member

Is this on an imdisk ram drive?

@nabijaczleweli
Copy link
Contributor Author

nabijaczleweli commented Feb 23, 2025

Happens on real disk (local, SATA, NTFS) but can also repro on SoftPerfect RAM Disk.

@ChrisDenton
Copy link
Member

Hm, a usual reason for this error is std::fs::canonicalize failing on a drive that doesn't support it. However, Cargo uses fallbacks for that situation so I wondered if there was a regression there. Failing on normal drives would rule that out.

Failing to create a directory with that error is highly unusual. I can't yet repro it on my machine using the latest nightly.

Does it work if you manually create the target directory?

@nabijaczleweli
Copy link
Contributor Author

(SoftPerfect supports canonicalisation, I build crates there all the time; neither here nor there since this also happens on real disks)

It does for a bit until it tries to create another:

   Compiling crossbeam-utils v0.8.20
   Compiling log v0.4.22
   Compiling bumpalo v3.16.0
error: failed to write S:\imgdiff-real\target\debug\deps\libcfg_if-5e710c05facb79fe.rmeta: Incorrect function. (os error 1)

error: could not compile `cfg-if` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: failed to write S:\imgdiff-real\target\debug\deps\libunicode_ident-ae96588ddcd89e94.rmeta: Incorrect function. (os error 1)

error: could not compile `unicode-ident` (lib) due to 1 previous error
error: failed to write S:\imgdiff-real\target\debug\deps\liblog-f4025ca40747ecd5.rmeta: Incorrect function. (os error 1)

error: could not compile `log` (lib) due to 1 previous error
error: failed to write S:\imgdiff-real\target\debug\deps\libbumpalo-e9bda64ade8b9f52.rmeta: Incorrect function. (os error 1)

error: failed to write S:\imgdiff-real\target\debug\deps\libautocfg-ed612dc94a4d1095.rmeta: Incorrect function. (os error 1)

error: could not compile `bumpalo` (lib) due to 1 previous error
error: could not compile `autocfg` (lib) due to 1 previous error

@ChrisDenton
Copy link
Member

Do you have any security software that might be interfering with the build?

@nabijaczleweli
Copy link
Contributor Author

nabijaczleweli commented Feb 23, 2025

No, and the nightlies have always worked before (admittedly, I don't build on nightly all that often) and my setup hasn't changed. Is there something I can do to bisect this to some syscall or function call? Maybe a symbol to step through? Blindly attaching gdb and hoping when the compiler isn't even panicking/aborting/whatever seems like a fool's errand.

@ChrisDenton
Copy link
Member

ChrisDenton commented Feb 23, 2025

You could try cargo-bisect-rustc https://rust-lang.github.io/cargo-bisect-rustc/installation.html

To narrow down the nightly that started failing.

@ChrisDenton
Copy link
Member

Otherwise there's process monitor to look at the failing function calls https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

@nabijaczleweli
Copy link
Contributor Author

cargo bisect-rustc --access=github --start=2024-10-01 --end=2025-01-27 seems to have started well so far. Bisect running.

@saethlin saethlin added the O-windows-gnu Toolchain: GNU, Operating system: Windows label Feb 23, 2025
@nabijaczleweli
Copy link
Contributor Author

nabijaczleweli commented Feb 23, 2025

Reproed on plain cargo new foo, no additional arguments.

searched nightlies: from nightly-2024-10-01 to nightly-2025-01-27
regressed nightly: nightly-2024-12-23
searched commit range: 426d173...e108481
regressed commit: 00bf74d

bisected with cargo-bisect-rustc v0.6.9

Host triple: x86_64-pc-windows-gnu
Reproduce with:

cargo bisect-rustc --access=github --start=2024-10-01 --end=2025-01-27

@ChrisDenton
Copy link
Member

Ah most likely https://github.com/rust-lang/rust/pull/131072/files

I'll put up a PR tomorrow to hopefully fix this.

What's your Windows version btw?

@saethlin saethlin added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 24, 2025
@nabijaczleweli
Copy link
Contributor Author

Certainly smells like it, yeah. Procmon says:

Image

those are the only two NOT SUPPORTEDs (sorry for the screenshot but I can't seem to be able to extract something legible from this). Clicking Properties doesn't really show what the operation was supposed to be either?

I'm on windows 10. winver says "Microsoft Windows Version 1607 (OS Build 14393.693)".

bors added a commit to rust-lang-ci/rust that referenced this issue Feb 24, 2025
Windows: Fix error in `fs::rename` on Windows 1607

Fixes rust-lang#137499

There's a bug in our Windows implementation of `fs::rename` that only manifests on a specific version of Windows. Both newer and older versions of Windows work.

I took the safest route to fixing this by using the old `MoveFileExW` function to implement this and only falling back to the new behaviour if that fails. This is similar to what is done in `unlink` (just above this function).

try-job: dist-x86_64-mingw
try-job: dist-x86_64-msvc

`@bors` r? ghost
@ChrisDenton
Copy link
Member

@nabijaczleweli would you mind testing the fix using https://github.com/kennytm/rustup-toolchain-install-master to install 16619a00a6db51793008f98beb9ae3a0ab99224e

E.g.:

cargo install rustup-toolchain-install-master
rustup-toolchain-install-master 16619a00a6db51793008f98beb9ae3a0ab99224e --host=x86_64-pc-windows-gnu
cargo +16619a00a6db51793008f98beb9ae3a0ab99224e build

@nabijaczleweli
Copy link
Contributor Author

Sorry, no dice:

S:\foo>cargo +16619a00a6db51793008f98beb9ae3a0ab99224e build
warning: `D:\Users\nabijaczleweli\.cargo\config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
error: failed to create directory `S:\foo\target`

Caused by:
  Incorrect function. (os error 1)

Image

@nabijaczleweli
Copy link
Contributor Author

ok i wanted to try to do a rustc with a fully-reverted 00bf74d to see if this would help. but. well. i can't really. build the compiler? due to this. so. maybe i can cross-build lol

bors added a commit to rust-lang-ci/rust that referenced this issue Feb 24, 2025
Revert 00bf74d

In service of rust-lang#137499

try-job: dist-x86_64-mingw
try-job: dist-x86_64-msvc
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 24, 2025
Windows: Fix error in `fs::rename` on Windows 1607

Fixes rust-lang#137499

There's a bug in our Windows implementation of `fs::rename` that only manifests on a specific version of Windows. Both newer and older versions of Windows work.

I took the safest route to fixing this by using the old `MoveFileExW` function to implement this and only falling back to the new behaviour if that fails. This is similar to what is done in `unlink` (just above this function).

try-job: dist-x86_64-mingw
try-job: dist-x86_64-msvc
@ChrisDenton
Copy link
Member

ChrisDenton commented Feb 24, 2025

Ok, I tried to reproduce this on a vanilla 1607 install and it failed, the same as you saw. Then I did a bunch of updates and installed Visual Studio (to test msvc) and now the windows-gnu cargo works. I'm very confused.

@nabijaczleweli
Copy link
Contributor Author

nabijaczleweli commented Feb 24, 2025 via email

@ChrisDenton
Copy link
Member

I used a VM to install 1607 without updates, which repo'd the failure. I then ran updates (KB890830, KB5050109, KB4103720), reset the machine and now it works. So this seems like it might be a bug in the Windows kernel. Which we happened not to hit before for some reason.

@ChrisDenton
Copy link
Member

ChrisDenton commented Feb 24, 2025

I figure out I messed up rustup-toolchain-install-master. You have to add --component cargo otherwise rustup silently uses a fallback cargo (i.e. one that doesn't work). Which I guess is helpful sometimes but not in this case. So it should be:

rustup-toolchain-install-master 757f02226a4242da9087c892a4865d328700bea7 --component cargo

Doing that, my fix does indeed seem to work:

C:\Users\Administrator\hello>cargo build
error: failed to create directory `C:\Users\Administrator\hello\target`

Caused by:
  Incorrect function. (os error 1)

C:\Users\Administrator\hello>cargo +757f02226a4242da9087c892a4865d328700bea7 build
   Compiling hello v0.1.0 (C:\Users\Administrator\hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.59s

@nabijaczleweli
Copy link
Contributor Author

omg real. I can repro the fix:

S:\foo>rustup-toolchain-install-master 16619a00a6db51793008f98beb9ae3a0ab99224e --component cargo
detecting the channel of the `16619a00a6db51793008f98beb9ae3a0ab99224e` toolchain...
downloading <https://ci-artifacts.rust-lang.org/rustc-builds/16619a00a6db51793008f98beb9ae3a0ab99224e/rustc-nightly-x86_64-pc-windows-gnu.tar.xz>...
98.93 MB / 98.93 MB [========================================================================================================================================================================================] 100.00 % 14.63 MB/s
downloading <https://ci-artifacts.rust-lang.org/rustc-builds/16619a00a6db51793008f98beb9ae3a0ab99224e/cargo-nightly-x86_64-pc-windows-gnu.tar.xz>...
10.82 MB / 10.82 MB [=========================================================================================================================================================================================] 100.00 % 6.90 MB/s
downloading <https://ci-artifacts.rust-lang.org/rustc-builds/16619a00a6db51793008f98beb9ae3a0ab99224e/rust-std-nightly-x86_64-pc-windows-gnu.tar.xz>...
27.94 MB / 27.94 MB [========================================================================================================================================================================================] 100.00 % 15.05 MB/s
toolchain `16619a00a6db51793008f98beb9ae3a0ab99224e` is successfully installed!

S:\foo>cargo  +16619a00a6db51793008f98beb9ae3a0ab99224e  build
warning: `D:\Users\nabijaczleweli\.cargo\config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
   Compiling foo v0.1.0 (S:\foo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.38s

S:\foo>rm -rf target

S:\foo>rustup-toolchain-install-master 757f02226a4242da9087c892a4865d328700bea7 --component cargo
detecting the channel of the `757f02226a4242da9087c892a4865d328700bea7` toolchain...
downloading <https://ci-artifacts.rust-lang.org/rustc-builds/757f02226a4242da9087c892a4865d328700bea7/rustc-nightly-x86_64-pc-windows-gnu.tar.xz>...
98.93 MB / 98.93 MB [========================================================================================================================================================================================] 100.00 % 12.42 MB/s
downloading <https://ci-artifacts.rust-lang.org/rustc-builds/757f02226a4242da9087c892a4865d328700bea7/cargo-nightly-x86_64-pc-windows-gnu.tar.xz>...
10.81 MB / 10.81 MB [=========================================================================================================================================================================================] 100.00 % 6.89 MB/s
downloading <https://ci-artifacts.rust-lang.org/rustc-builds/757f02226a4242da9087c892a4865d328700bea7/rust-std-nightly-x86_64-pc-windows-gnu.tar.xz>...
27.94 MB / 27.94 MB [=========================================================================================================================================================================================] 100.00 % 9.36 MB/s
toolchain `757f02226a4242da9087c892a4865d328700bea7` is successfully installed!

S:\foo>cargo  +757f02226a4242da9087c892a4865d328700bea7  build
warning: `D:\Users\nabijaczleweli\.cargo\config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
   Compiling foo v0.1.0 (S:\foo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.37s

@saethlin saethlin marked this as a duplicate of #137652 Feb 26, 2025
@Fulgen301
Copy link
Contributor

Fulgen301 commented Mar 1, 2025

Build 14393.693 is (also?) Windows Server 2016.

Windows' wonderfully descriptive NT status code to Win32 error code mappings strike again:

cvt(c::SetFileInformationByHandle(

SetFileInformationByHandle calls NtSetInformationFile, which fails with 0xc000029c (STATUS_VOLUME_NOT_UPGRADED: The volume must be upgraded to enable this feature), which gets mapped to ERROR_INVALID_FUNCTION.

As we already have fallback logic for SetFileInformationByHandle failing due to ERROR_INVALID_PARAMETER, we could fix it by falling back either unconditionally or if the calls fails with ERROR_INVALID_FUNCTION, that'd avoid going through MoveFileExW as well as our renaming machinery.

workingjubilee added a commit to workingjubilee/rustc that referenced this issue Mar 7, 2025
Windows: Fix error in `fs::rename` on Windows 1607

Fixes rust-lang#137499

There's a bug in our Windows implementation of `fs::rename` that only manifests on a specific version of Windows. Both newer and older versions of Windows work.

I took the safest route to fixing this by using the old `MoveFileExW` function to implement this and only falling back to the new behaviour if that fails. This is similar to what is done in `unlink` (just above this function).

try-job: dist-x86_64-mingw
try-job: dist-x86_64-msvc
jhpratt added a commit to jhpratt/rust that referenced this issue Mar 7, 2025
Windows: Fix error in `fs::rename` on Windows 1607

Fixes rust-lang#137499

There's a bug in our Windows implementation of `fs::rename` that only manifests on a specific version of Windows. Both newer and older versions of Windows work.

I took the safest route to fixing this by using the old `MoveFileExW` function to implement this and only falling back to the new behaviour if that fails. This is similar to what is done in `unlink` (just above this function).

try-job: dist-x86_64-mingw
try-job: dist-x86_64-msvc
@bors bors closed this as completed in 0c67061 Mar 8, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 8, 2025
Rollup merge of rust-lang#137528 - ChrisDenton:rename-win, r=joboet

Windows: Fix error in `fs::rename` on Windows 1607

Fixes rust-lang#137499

There's a bug in our Windows implementation of `fs::rename` that only manifests on a specific version of Windows. Both newer and older versions of Windows work.

I took the safest route to fixing this by using the old `MoveFileExW` function to implement this and only falling back to the new behaviour if that fails. This is similar to what is done in `unlink` (just above this function).

try-job: dist-x86_64-mingw
try-job: dist-x86_64-msvc
@nabijaczleweli
Copy link
Contributor Author

rustc 1.87.0-nightly (249cb8431 2025-03-12) from rustup works

github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this issue Mar 14, 2025
Windows: Fix error in `fs::rename` on Windows 1607

Fixes rust-lang#137499

There's a bug in our Windows implementation of `fs::rename` that only manifests on a specific version of Windows. Both newer and older versions of Windows work.

I took the safest route to fixing this by using the old `MoveFileExW` function to implement this and only falling back to the new behaviour if that fails. This is similar to what is done in `unlink` (just above this function).

try-job: dist-x86_64-mingw
try-job: dist-x86_64-msvc
@ChrisDenton
Copy link
Member

The stable version 1.85.1 should hopefully now work (rustup update stable).

@nabijaczleweli
Copy link
Contributor Author

Hm, I think updating nightly to 2025-03-12 also updated my rustup and now I'm getting

T:\>rustup update stable
info: syncing channel updates for 'stable-x86_64-pc-windows-gnu'
info: latest update on 2025-03-18, rust version 1.85.1 (4eb161250 2025-03-15)
info: downloading component 'rust-src'
info: retrying download for 'https://static.rust-lang.org/dist/2025-03-18/rust-src-1.85.1.tar.xz'
error: component download failed for rust-src: could not rename downloaded file from 'D:\Users\nabijaczleweli\.rustup\downloads\b5a5539009d477a07c2535aefc39cd69ad526a74eab65886a26e2c3021020827.partial' to 'D:\Users\nabijaczleweli\.rustup\downloads\b5a5539009d477a07c2535aefc39cd69ad526a74eab65886a26e2c3021020827': Incorrect function. (os error 1)

lol

I think this is
Image

I downloaded the latest https://rustup.rs/ and got the same thing:

T:\>.\rustup.exe update stable
info: syncing channel updates for 'stable-x86_64-pc-windows-gnu'
info: latest update on 2025-03-18, rust version 1.85.1 (4eb161250 2025-03-15)
info: downloading component 'rust-src'
info: retrying download for 'https://static.rust-lang.org/dist/2025-03-18/rust-src-1.85.1.tar.xz'
error: component download failed for rust-src: could not rename downloaded file from 'D:\Users\nabijaczleweli\.rustup\downloads\b5a5539009d477a07c2535aefc39cd69ad526a74eab65886a26e2c3021020827.partial' to 'D:\Users\nabijaczleweli\.rustup\downloads\b5a5539009d477a07c2535aefc39cd69ad526a74eab65886a26e2c3021020827': Incorrect function. (os error 1)

I also replaced the rustup.exe under /d/Users/nabijaczleweli/.cargo/bin/ with it and got the same thing, so idk

@ChrisDenton
Copy link
Member

Oh no, looks like rustup's last update was built from.

You can download the previous version here https://static.rust-lang.org/rustup/archive/1.27.1/x86_64-pc-windows-msvc/rustup-init.exe and then do:

rustup set auto-self-update disable

To stop it auto-updating for now.

@nabijaczleweli
Copy link
Contributor Author

That works! and the new stable works too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-windows-gnu Toolchain: GNU, Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
5 participants