`env!` / `include!` / `include_str!` do not end up in dep-info entries resulting in stale artifacts under rustc-wrapper caches like sccache

Đang mở
#29 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Ít trao đổi
Công nghệ
rust
Lĩnh vực
build-system, tooling

Hướng nghiên cứu

Bắt đầu với Docker reproducer tại github.com/leighmcculloch/rust-sccache-env-proc-macro-cache-collision và so sánh các mục dep-info cho env!, include! và include_str! qua các giá trị môi trường đã thay đổi. Đọc phần đánh giá đầu vào MacroString và đường dẫn mở rộng proc-macro, bao gồm workaround const { let _ = #raw }. Công việc được xem là hoàn tất khi hành vi dep-info mong đợi đã được xác lập và mọi thay đổi thiết kế an toàn đối với MacroString đều được giới hạn phạm vi rõ ràng và được bao phủ bởi một reproducer.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

I've been debugging a situation with a procmacro that uses macro-string where changes to an environment variable used in it via env! and passed through a macro-string resulted in stale build artifacts rather than the latest environment value being embedded.

pub const GIT_REVISION: &str = mymacrolib::baked!(env!("GIT_REVISION"));

I created a Docker image that reproduces the problem with sccache setup, and a repo with a procmacro using macro-string:

My understanding so far is that this is happening because macro-string evaluates and expands the env! within the procmacro meaning that rustc never sees the env! in the crate and so is unaware that it is a dependency and the env value doesn't get included in the build artifact's dep-info. When rebuilding the build artifact with a different env! sccache will return the earlier cached artifact because it cannot distinguish between them since the env is not included in the dep-info.

As a work-around I've found that having my procmacro emit a const { let _ = #input } can resurface the env! back in the context of the crate unexpanded. For example:

#[proc_macro]
pub fn baked(input: TokenStream) -> TokenStream {
    let raw = proc_macro2::TokenStream::from(input.clone());
    let macro_string = parse_macro_input!(input as MacroString);
    let s = macro_string.eval().unwrap();
    let lit = proc_macro2::Literal::string(&s);
    quote! {
        {
            const { let _ = #raw; };  // surface env!()/include!() to rustc's dep-info
            #lit
        }
    }
    .into()
}

Is the dep-info loss of env!/include!/include_str! expected / known issue?

Because macro-string is an embedded detail in macros it can be difficult to know if it is safe to use sccache for a build or not with a rust library, because internally or somewhere in the dependency tree it may be in use in a way that poisons the cache.

Would it be reasonable for MacroString to somehow encourage re-exporting of the input as a way to reduce the occurrence of this bug? It's not obvious to me how this would be achieved so I expect my question is naive at best.

Ngôn ngữ chính
Rust
Star
46
Fork
2
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.