Skip to content

anchor-syn 0.31.0 is incompatible with proc-macro2 >= 1.0.95 #3661

@niooss-ledger

Description

@niooss-ledger

Hello,

When attempting to create a new Anchor project, anchor build fails with:

    Finished `release` profile [optimized] target(s) in 0.15s
   Compiling anchor-syn v0.31.0
error[E0599]: no method named `source_file` found for struct `proc_macro2::Span` in the current scope
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/anchor-syn-0.31.0/src/idl/defined.rs:499:66
    |
499 | ...an::call_site().source_file().path();
    |                    ^^^^^^^^^^^ method not found in `Span`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `anchor-syn` (lib) due to 1 previous error
Error: Building IDL failed. Run `ANCHOR_LOG=true anchor idl build` to see the logs.

TL;DR

anchor-syn has been using an unstable API from proc-macro2 where a recent release introduced a breaking change.

Root Cause Analysis

A few hours ago, proc-macro2 version 1.0.95 was released (https://github.com/dtolnay/proc-macro2/releases/tag/1.0.95) with a breaking change:

Update semver-exempt API under RUSTFLAGS=--cfg=procmacro2_semver_exempt to that of nightly-2025-04-16 (dtolnay/proc-macro2#497)

proc-macro2's documentation clearly warns about such breaking changes in minor versions in https://github.com/dtolnay/proc-macro2/blob/24bbf16d9df01d5f7d9ac39bdfbaea85f4c194fb/README.md#unstable-features :

To opt into the additional APIs available in the most recent nightly compiler, the procmacro2_semver_exempt config flag must be passed to rustc. [...] As these are unstable APIs that track the nightly compiler, minor versions of proc-macro2 may make breaking changes to them at any time.

Indeed, rust-lang/rust#139671 replaced

impl Span {
    // ...
    pub fn source_file(&self) -> SourceFile;
}
impl SourceFile {
    pub fn path(&self) -> PathBuf;
    pub fn is_real(&self) -> bool;
}

with:

impl Span {
    // ...
    pub fn file(&self) -> String; // Mapped file name, for display purposes.
    pub fn local_file(&self) -> Option<PathBuf>; // Real file name as it exists on disk.
}

anchor-syn has been using .source_file().path() in

let source_path = proc_macro2::Span::call_site().source_file().path();

Therefore it seems this code needs to be updated to the latest API. Could you please fix it?

System information

For information, I am building on x86_64 with:

$ anchor --version
anchor-cli 0.31.0
$ cargo --version
cargo 1.85.0 (d73d2caf9 2024-12-31)
$ rustc --version
rustc 1.85.0 (4d91de4e4 2025-02-17)
$ yarn --version
1.22.22

$ head -n 3 /etc/os-release 
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"

I reproduced this issue with anchor init test && cd test && anchor build.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions