anchor build fails compiling anchor-syn 0.30.1: proc_macro2 'method not found in Span' for source_file, renamed to file()
Symptom: anchor build on a freshly created anchor init project fails while compiling anchor-syn, even though proc_macro2 itself compiled successfully earlier in the run:
anchor-syn-0.30.1/src/idl/defined.rs:499:66
let source_path = proc_macro2::Span::call_site().source_file().path();
^^^^^^^^^^^ method not found in Span
Downgrading Anchor or rustc does not help, which is the confusing part.
Cause: this is not an Anchor version problem or a rustc problem. proc-macro2 is a transitive dependency resolved by Cargo to its newest semver-compatible release, and a 1.0.9x release removed the nightly-only Span::source_file() accessor in favour of Span::file(). anchor-syn 0.30.x still calls the old name, so any fresh cargo resolution picks a proc-macro2 that no longer has it. Because the offending crate is pulled in indirectly, changing the declared Anchor version leaves the resolved proc-macro2 untouched.
Fix, in order of preference:
1. Pin the dependency back below the breaking release, e.g. cargo update -p proc-macro2 --precise 1.0.94, and commit Cargo.lock.
2. Upgrade to an Anchor release whose anchor-syn uses .file().
3. As a last-resort local unblock, edit the vendored source in ~/.cargo/registry/src/.../anchor-syn-0.30.1/src/idl/defined.rs line 499 to let source_path = proc_macro2::Span::call_site().file(); — this is wiped by any registry refresh and will not work for CI or teammates.
Non-obvious part: the error points at Anchor's source, so the instinct is to change the Anchor version. The real lever is the lockfile entry for a crate you never named.
Source: adapted from a Stack Exchange answer by BlockBuddy (question by Rico Tobacco), https://solana.stackexchange.com/questions/21553/proc-macro2-source-file-method-not-found-in-span — CC BY-SA 4.0