Skip to content

fix: rustc cfg crash when directory is deleted#21810

Open
Wilfred wants to merge 1 commit intorust-lang:masterfrom
Wilfred:fix_rustc_cfg_working_dir
Open

fix: rustc cfg crash when directory is deleted#21810
Wilfred wants to merge 1 commit intorust-lang:masterfrom
Wilfred:fix_rustc_cfg_working_dir

Conversation

@Wilfred
Copy link
Contributor

@Wilfred Wilfred commented Mar 13, 2026

rustc --print cfg can be run in any directory, but the command will error if the directory does not exist.

This is more noticeable when working on a large monorepo with a rust-project.json, but it can occur in cargo projects too.

I haven't been able to reproduce this many times, but it seems to be due to switching commits before/after a directory is created.

The rust-analyzer log looks like this:

2026-03-13T06:43:13.1075102-07:00  WARN failed to get rustc cfgs e=unable to fetch cfgs via `cd "/home/wilfred/monorepo/old_dir/foo/" && RUSTUP_AUTO_INSTALL="0" "/home/wilfred/.cache/dotslash/obj/cas/35/b8b890939206aaabc7b768fd66ad4d2c/extract/tar.zst/bin/rustc" "--print" "cfg" "-O"`

Caused by:
 0: cd "/home/wilfred/monorepo/old_dir/foo/" && RUSTUP_AUTO_INSTALL="0" "/home/wilfred/.cache/dotslash/obj/cas/35/b8b890939206aaabc7b768fd66ad4d2c/extract/tar.zst/bin/rustc" "--print" "cfg" "-O" failed

`rustc --print cfg` can be run in any directory, but the command will
error if the directory does not exist.

This is more noticeable when working on a large monorepo with a
rust-project.json, but it can occur in cargo projects too.

I haven't been able to reproduce this many times, but it seems to be
due to switching commits before/after a directory is created.

The rust-analyzer error looks like this:

```
2026-03-13T06:43:13.1075102-07:00  WARN failed to get rustc cfgs e=unable to fetch cfgs via `cd "/home/wilfred/monorepo/old_dir/foo/" && RUSTUP_AUTO_INSTALL="0" "/home/wilfred/.cache/dotslash/obj/cas/35/b8b890939206aaabc7b768fd66ad4d2c/extract/tar.zst/bin/rustc" "--print" "cfg" "-O"`

Caused by:
 0: cd "/home/wilfred/monorepo/old_dir/foo/" && RUSTUP_AUTO_INSTALL="0" "/home/wilfred/.cache/dotslash/obj/cas/35/b8b890939206aaabc7b768fd66ad4d2c/extract/tar.zst/bin/rustc" "--print" "cfg" "-O" failed
```
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 13, 2026
@ChayimFriedman2
Copy link
Contributor

It matters in case of overrides, I think.

@Wilfred
Copy link
Contributor Author

Wilfred commented Mar 15, 2026

You mean an override with rustup changing the tool chain version? Yeah, that's the only case I can think of where the working directory matters.

Another approach would be to look at the parent, grandparent etc directory until we have a directory that exists. That would help if e.g. the grandparent directory has the tool chain override.

@ChayimFriedman2
Copy link
Contributor

When we're unable to run it, it's temporary and after rust-project.json changes will be notified we will load it correctly, right? So the right solution is to ignore the project. But isn't that what we do now, except we also warn?

@Veykril
Copy link
Member

Veykril commented Mar 16, 2026

It does matter where we run this command at least when we end up using cargo due to .cargo/config.toml lookup which can affect some things here. For plain rustc I am not sure, it probably doesn't matter

@Veykril
Copy link
Member

Veykril commented Mar 16, 2026

I am a bit confused as to how we end up calling this with a non existing working directory though, that means we have a project loaded in a non existing directory no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants