Skip to content

The no-op always-success RUSTC_WRAPPER breaks build scripts #12973

Closed

Description

@RalfJung

It is not an uncommon pattern for build scripts to invoke rustc to detect whether some features are available. Popular crates like anyhow and eyre do that, and the autocfg crate (14 million "recent" downloads, >100 million all-time downloads) even provides a convenient interface to write such build scripts.

RA entirely breaks that by having a RUSTC_WRAPPER that always returns success. I think that is a bug in RA.

Activity

  1. changed the title [-]The no-op RUSTC_WRAPPER breaks build scripts[/-] [+]The no-op always-success RUSTC_WRAPPER breaks build scripts[/+] on Aug 8, 2022
  2. lnicola commented on Aug 8, 2022

    @lnicola
    Member

    This was never reported before. Skimming autocfg, it doesn't seem to use RUSTC_WRAPPER, which might explain the difference. You can use disable rust-analyzer.cargo.buildScripts.useRustcWrapper as a workaround.

    Specifically about anyhow, I don't think it should be testing for a feature that no longer exist, but we don't control that.

    It would be nice for cargo to have a "run the build scripts, but don't check the code" mode.

  3. RalfJung commented on Aug 8, 2022

    @RalfJung
    MemberAuthor

    This wrapper might also be responsible for rust-lang/rust#99538; at least I have not seen that problem since I disabled proc macros and build scripts.

    Skimming autocfg, it doesn't seem to use RUSTC_WRAPPER

    Oh, good point.

    anyhow was specifically asked to use RUSTC_WRAPPER in dtolnay/anyhow#157. And I tend to agree with the author that using RUSTC_WRAPPER is more correct. But neither eyre nor autocfg are using RUSTC_WRAPPER so they happen to not hit this problem. I think that is an accident though, and arguments could be made that they should use RUSTC_WRAPPER.

    Specifically about anyhow, I don't think it should be testing for a feature that no longer exist, but we don't control that.

    Well, the probe needs updating to latest nightly, yeah. And soon the feature will be stable and this will only be relevant for old compilers. But that doesn't really help with the general problem.

  4. lnicola commented on Aug 8, 2022

    @lnicola
    Member

    One solution might be to detect nested invocations of our wrapper: if we're not the outer one, we could pass through the call to rustc (or even to a RA_ORIGINAL_RUSTC_WRAPPER that we've saved).

  5. bjorn3 commented on Aug 8, 2022

    @bjorn3
    Member

    Build scripts don't cause nested invocations of the wrapper.

  6. lnicola commented on Aug 8, 2022

    @lnicola
    Member

    Build scripts don't cause nested invocations of the wrapper.

    Doesn't the anyhow build script do just that?

  7. RalfJung commented on Aug 8, 2022

    @RalfJung
    MemberAuthor

    Yes, it does.

  8. bjorn3 commented on Aug 8, 2022

    @bjorn3
    Member

    Anyhow's build script invokes the wrapper, but the build script doesn't run underneath the wrapper, but directly under cargo. As such the wrapper invocation is not nested within the wrapper.

  9. lnicola commented on Aug 8, 2022

    @lnicola
    Member

    Am I missing something here?

    1. RA sets RUSTC_WRAPPER=rust-analyzer
    2. RA runs cargo check
    3. cargo check compiles the anyhow build script
    4. cargo check runs the anyhow build script, presumably without clearing RUSTC_WRAPPER
    5. the build script checks for RUSTC_WRAPPER and uses it to compile some code
  10. bjorn3 commented on Aug 8, 2022

    @bjorn3
    Member

    That is correct. But how do we detect that anyhow's build script invoked the wrapper rather than cargo itself? Both use pretty much the same env vars and because at step 4 the wrapper isn't used to run the build script, we can't set an env var either to signal that a build script ran rustc with the wrapper as opposed to cargo.

  11. lnicola commented on Aug 8, 2022

    @lnicola
    Member

    Can't we set RA_RUSTC_WRAPPPER=$RUSTC_WRAPPER in step 1, then we check for it (in step 5) and run that instead?

  12. bjorn3 commented on Aug 8, 2022

    @bjorn3
    Member

    How do you make sure that it doesn't run for rustc invoked by cargo then?

  13. bjorn3 commented on Aug 8, 2022

    @bjorn3
    Member

    I think we can check env vars like NUM_JOBS or PROFILE which only get set for build scripts.

  14. RalfJung commented on Aug 8, 2022

    @RalfJung
    MemberAuthor

    It would be nice for cargo to have a "run the build scripts, but don't check the code" mode.

    Yeah, fundamentally this is a hack to work-around not being able to tell cargo what you really want to do. Is that being tracked on the cargo side somewhere, are the cargo devs aware of this?

  15. lnicola commented on Aug 8, 2022

    @lnicola
    Member

    How do you make sure that it doesn't run for rustc invoked by cargo then?

    Hmm...

    Is that being tracked on the cargo side somewhere, are the cargo devs aware of this?

    I suppose not, but a request for cargo build --dependencies-only (which isn't that different) has been open since 2016 without any end in sight.

  16. 4 remaining items

  17. vlad20012 commented on Aug 9, 2022

    @vlad20012
    Contributor

    There is the unstable --keep-going flag for that

    Wow, this seems like exactly what we need!

  18. added a commit that references this issue on Aug 10, 2022
  19. added
    Broken WindowBugs / technical debt to be addressed immediately
    C-bugCategory: bug
    on Aug 11, 2022
  20. matklad commented on Aug 11, 2022

    @matklad
    Contributor

    🤔 I think —-keep-going (which we maybe should rename to be consistent with analogous flag of libtest) changes the calculus here significantly. It should allow us to remove the RUSTC_WRAPPER hack, as the main problem why we need that is not performance, but reliability.

  21. Veykril commented on Aug 11, 2022

    @Veykril
    Member

    A quick test seems to not really change the times for me when running with and without the wrapper (though that might be because cargo check gets skipped due to caching here), I'd be in favor of just removing the wrapper, as it also makes cargo be confused about diagnostics (in rare cases it seems to succeed cargo checks on crates that have actual errors in them, see #12808)

  22. lnicola commented on Aug 11, 2022

    @lnicola
    Member

    I think it's going to matter a lot for people who disable rust-analyzer.checkOnSave.enable. But we can probably remove or disable it until we get some proper support in Cargo.

  23. Veykril commented on Aug 12, 2022

    @Veykril
    Member

    Opened a zulip topic in the t-cargo stream to see if we can work on this from the cargo side of things as our own options here are rather limited and to me it sounds like it would make more sense to just push rust-lang/cargo#7178 forward
    https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/cargo.20build.20--compile-time-deps.20cargo.237178/near/293136753

  24. dtolnay commented on Aug 13, 2022

    @dtolnay
    Member

    #12973 (comment) sounds the most promising to me. Here are Cargo's build script environment variables: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts. The criteria for selecting one for this purpose are:

    1. It has to be set by Cargo during build script execution
    2. It must always be set during build script execution, e.g. unlike CARGO_CFG_UNIX which is set on unix only
    3. It must not be set during compilation of crates or the build scripts
    4. There should be no reason for someone to set it globally for a whole build, so in particular it should not be one of the environment variables read by Cargo
    5. There should be no reason for it to appear coincidentally in someone's environment, like HOST

    These are the ones that I see fit the bill:

    • CARGO_CFG_TARGET_ARCH
    • CARGO_CFG_TARGET_ENDIAN
    • CARGO_CFG_TARGET_FAMILY
    • CARGO_CFG_TARGET_OS
    • CARGO_CFG_TARGET_POINTER_WIDTH
    • CARGO_CFG_TARGET_VENDOR
    • NUM_JOBS
    • OPT_LEVEL
    • PROFILE

    So the plan would be for rustc_wrapper.rs to do its early return thing only if std::env::var_os("CARGO_CFG_TARGET_ARCH").is_none().

  25. Veykril commented on Aug 13, 2022

    @Veykril
    Member

    Picking one of the CARGO_CFG_TARGET_* ones seems most reasonable, I could see someone setting one of the later 3 on their system for something unrelated given their short names (though rather unlikely still of course).

  26. Veykril commented on Aug 13, 2022

    @Veykril
    Member

    PR #13010 is up, though I can't test whether it works as I am actually not running into the issue with anyhow for some reason.

  27. sehz commented on Aug 13, 2022

    @sehz
  28. dtolnay commented on Aug 13, 2022

    @dtolnay
    Member

    This has been fixed by #13010.

  29. added a commit that references this issue on Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Broken WindowBugs / technical debt to be addressed immediatelyC-bugCategory: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions