Skip to content

Add a way to distinguish proc-macro deps - #1420

Merged
krasimirgg merged 1 commit into
bazelbuild:mainfrom
krasimirgg:proc_macro_dep
Jul 14, 2022
Merged

krasimirgg merged 1 commit into
bazelbuild:mainfrom
krasimirgg:proc_macro_dep

Conversation

@krasimirgg

@krasimirgg krasimirgg commented Jun 24, 2022 •

Copy link
Copy Markdown
Collaborator

Depends on #1424.

We have a peculiar use case related to bootstrapping rustc (https://rustc-dev-guide.rust-lang.org/building/bootstrapping.html).
At stage0, the beta compiler is used:

  • first to build the new standard libraries (stage0-stdlib),
  • next to build the new rustc (stage0-rustc).

To build stage0-rustc, there are two kinds of dependencies of rustc:

  • proc-macro deps and their transitive deps, and
  • everything else ("standard" dependencies).

The bootstrapping wrapper treats these two types of dependencies differently:

  • the proc-macro deps and their transitive dependencies are built using beta with the beta sysroot,
  • everything else is built using beta with the stage0-stdlib sysroot.

The reason for this is that proc-macros are loaded by the beta rustc while building everything else, and they need to be compiled using a sysroot containing the proc-macro rlib crate ABI compatible with the beta compiler (which the stage0-stdlib one is not always, e.g., rust-lang/rust#98056; https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/ICE.2Fboot.20invalid.20enum.20variant.20tag.20while.20decoding.20.60MacroKind.60).

In a setup like this, we need to be able to distinguish somehow whether the rustc action building a crate is building it as a (transitive) proc-macro dependency or not.

In this PR I propose we mark the deps attribute of the rust_proc_macro rule with a custom transition that sets a new is_proc_macro_dep boolean setting. The value of this setting is passed over as a new BAZEL_RULES_RUST_IS_PROC_MACRO_DEP environment variable. This allows us to have a rustc wrapper that examines this value and adapt accordingly in cases where we care about the distinction.

This whole feature is gated by the new is_proc_macro_dep_enabled boolean setting: when is_proc_macro_dep_enabled is false (the default), there is no difference between deps of proc-macros and "standard" deps (BAZEL_RULES_RUST_IS_PROC_MACRO_DEP is not set). This is to account for a case like this, where we would otherwise have an additional version of the proc-macro dep built: we have a rust_library(d), which is a dep of both a proc_macro(p) and a tool rust_binary(t) in exec config. When is_proc_macro_dep_enabled = false (the default) and we build everything, we will have 2 versions of d: one top-level non-exec mode, and one in exec mode used both as a dependency to the proc-macro and the tool. When is_proc_macro_dep_enabled = true and we build everything, we will have 3 versions of d: one top-level in non-exec mode, and two in exec mode: one d1 built as a dependency of the proc-macro where BAZEL_RULES_RUST_IS_PROC_MACRO_DEP=1, and another d0, built as a dependency of the tool where BAZEL_RULES_RUST_IS_PROC_MACRO_DEP=0.

A natural way that almost works to distinguish dependencies of proc-macro's from others is to check whether the configuration is exec: proc_macro_deps are marked exec, and exec trickles down to proc-macro's dependencies as well. This doesn't work however in situations where the top-level target is already in an exec configuration -- then all of its dependencies (both proc-macro's and non-proc-macro dependencies) are being compiled in exec configuration and we lose the ability to distinguish between those).


RESOLVED in the latest version of this PR, but for context on the dependence on #1424: I'm hitting a strange //crate_universe:rustdoc_test failure:
https://buildkite.com/bazel/rules-rust-rustlang/builds/6631#01819625-365a-42fd-bb11-f3a95db9f999/280-523

exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //crate_universe:rustdoc_test
-----------------------------------------------------------------------------
thread 'main' panicked at 'process wrapper error: No such file or directory (os error 2)', util/process_wrapper/main.rs:54:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It took me a while to figure out exactly what was going on, since the process_wrapper error doesn't mention the path to the missing file nor the context in which the failure had occurred. I added some error logging to process_wrapper so errors like these are more evident:

https://storage.googleapis.com/bazel-untrusted-buildkite-artifacts/0181afb8-96b4-494c-a21c-e4bf120d9698/crate_universe/rustdoc_test/test.log

exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //crate_universe:rustdoc_test
-----------------------------------------------------------------------------
thread 'main' panicked at 'process wrapper error: No such file or directory (os error 2) reading path: "bazel-out/k8-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__proc-macro2-1.0.39/proc-macro2_build_script.linksearchpaths", current directory: Ok("/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/1682/execroot/rules_rust/bazel-out/k8-fastbuild/bin/crate_universe/rustdoc_test.rustdoc_test.sh.runfiles/rules_rust") while processing args from file paths: ["external/crate_universe_crate_index__anyhow-1.0.57/anyhow_build_script.linksearchpaths", "external/crate_universe_crate_index__semver-1.0.10/semver_build_script.linksearchpaths", "external/crate_universe_crate_index__serde-1.0.137/serde_build_script.linksearchpaths", "bazel-out/k8-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__proc-macro2-1.0.39/proc-macro2_build_script.linksearchpaths", "bazel-out/k8-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__serde_derive-1.0.137/serde_derive_build_script.linksearchpaths", "bazel-out/k8-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__syn-1.0.96/syn_build_script.linksearchpaths", "external/crate_universe_crate_index__camino-1.0.9/camino_build_script.linksearchpaths", "external/crate_universe_crate_index__serde_json-1.0.81/serde_json_build_script.linksearchpaths", "external/crate_universe_crate_index__libc-0.2.126/libc_build_script.linksearchpaths", "external/crate_universe_crate_index__indexmap-1.8.2/indexmap_build_script.linksearchpaths", "external/crate_universe_crate_index__unicode-linebreak-0.1.2/unicode-linebreak_build_script.linksearchpaths", "bazel-out/k8-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__proc-macro-error-1.0.4/proc-macro-error_build_script.linksearchpaths", "bazel-out/k8-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__proc-macro-error-attr-1.0.4/proc-macro-error-attr_build_script.linksearchpaths", "external/crate_universe_crate_index__log-0.4.17/log_build_script.linksearchpaths", "external/crate_universe_crate_index__memchr-2.5.0/memchr_build_script.linksearchpaths", "external/crate_universe_crate_index__smartstring-1.0.1/smartstring_build_script.linksearchpaths", "external/crate_universe_crate_index__generic-array-0.14.5/generic-array_build_script.linksearchpaths", "external/crate_universe_crate_index__typenum-1.15.0/typenum_build_script.linksearchpaths", "external/crate_universe_crate_index__num-integer-0.1.45/num-integer_build_script.linksearchpaths", "external/crate_universe_crate_index__num-traits-0.2.15/num-traits_build_script.linksearchpaths", "external/crate_universe_crate_index__chrono-tz-0.6.1/chrono-tz_build_script.linksearchpaths", "external/crate_universe_crate_index__uncased-0.9.7/uncased_build_script.linksearchpaths", "external/crate_universe_crate_index__crossbeam-utils-0.8.8/crossbeam-utils_build_script.linksearchpaths"]', util/process_wrapper/main.rs:54:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The root cause of the issue was that link_search_path's for (transitive) dependencies of proc-macro's were being added to the process_wrapper invocation. When I changed the cfg of rust_proc_macro.deps, these were pointing to a configuration-specific relative path starting with blaze-out/, like
bazel-out/k8-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__proc-macro2-1.0.39/proc-macro2_build_script.linksearchpaths, which doesn't exist at test time (the "standard" deps link_search_path's are passed relative to bin/, e.g., external/crate_universe_crate_index__blah-1.0.39/blah_build_script.linksearchpaths.

@krasimirgg krasimirgg changed the title [WIP] Proc macro dep [WIP] Add a way to distinguish proc-macro deps Jun 24, 2022
@krasimirgg
krasimirgg force-pushed the proc_macro_dep branch 2 times, most recently from 67a9fd7 to dcc23d5 Compare June 28, 2022 09:21
@krasimirgg
krasimirgg force-pushed the proc_macro_dep branch 3 times, most recently from 12fb234 to b4970a1 Compare June 29, 2022 12:31
@krasimirgg
krasimirgg requested a review from scentini July 1, 2022 11:21
@krasimirgg krasimirgg changed the title [WIP] Add a way to distinguish proc-macro deps Add a way to distinguish proc-macro deps Jul 7, 2022
@krasimirgg
krasimirgg force-pushed the proc_macro_dep branch 3 times, most recently from 30af393 to baa9e1b Compare July 7, 2022 15:05
@krasimirgg
krasimirgg requested a review from UebelAndre July 11, 2022 12:42
@krasimirgg

krasimirgg commented Jul 11, 2022 •

Copy link
Copy Markdown
Collaborator Author

Hey @UebelAndre -- could you please take a look, I'd appreciate your help.

With this I'm hitting an issue on windows build bots (https://buildkite.com/bazel/rules-rust-rustlang/builds/6734#0181d931-f2a4-4427-810d-f18596a5cb4d/237-312). This we suspect is because the addition of a cfg on the deps of a proc-macro pushed some of the absolute path names for the cargo_universe actions over the windows (260 characters), e.g.: C:/b/2gork4yn/execroot/rules_rust/bazel-out/x64_windows-opt-exec-2B5CBBC6-ST-fa1bc2ca6f16/bin/external/crate_universe_crate_index__proc-macro-error-attr-1.0.4/proc-macro-error-attr_build_script_.exe.runfiles/crate_universe_crate_index__proc-macro-error-attr-1.0.4/, which is now 264 characters (including the trailing null character).
I wanted to try and experiment with this hypothesis, for example to try and shorten the crate_universe_crate_index fragment that occurs twice on this path to, say cuci. But I realised I don't know enough about the crate_universe infrastructure to try this out, e.g., the .bzl files under https://github.com/bazelbuild/rules_rust/tree/main/crate_universe/3rdparty/crates are auto-generated and contain references to crate_universe_crate_index, and I don't know how to re-generate them.
Do you think supporting such shortening would be reasonable and do you have suggestions how to approach it?

@UebelAndre

Copy link
Copy Markdown
Collaborator

re: #1420 (comment)

I think that'd be acceptable but unfortunate. The fundamental problem (#1120) is not solved in the rename and could definitely come back if an external crate just happens to have a long path somewhere. Is there anyone on or around your team that has any amount of windows experience to take a look at the root issues. I know there are ways to tell windows to ignore MAX_PATH constraints using \\?\ but I'm not sure what all is needed to get the system to correctly understand the intention and not just see a funny looking path.

@UebelAndre

UebelAndre commented Jul 11, 2022 •

Copy link
Copy Markdown
Collaborator

But I realised I don't know enough about the crate_universe infrastructure to try this out, e.g., the .bzl files under https://github.com/bazelbuild/rules_rust/tree/main/crate_universe/3rdparty/crates are auto-generated and contain references to crate_universe_crate_index, and I don't know how to re-generate them.

There's a crates_vendor rule that generates the target. I can update the comment at the top of each file to reflect this. But otherwise you can run:

bazel run //crate_universe/3rdparty:crates_vendor

This should update the generated files without changing any of the pins.

I filed:

@krasimirgg

Copy link
Copy Markdown
Collaborator Author

But I realised I don't know enough about the crate_universe infrastructure to try this out, e.g., the .bzl files under https://github.com/bazelbuild/rules_rust/tree/main/crate_universe/3rdparty/crates are auto-generated and contain references to crate_universe_crate_index, and I don't know how to re-generate them.

There's a crates_vendor rule that generates the target. I can update the comment at the top of each file to reflect this. But otherwise you can run:

bazel run //crate_universe/3rdparty:crates_vendor

This should update the generated files without changing any of the pins.

I filed:

This is awesome! Thank you very much!

@krasimirgg

Copy link
Copy Markdown
Collaborator Author

re: #1420 (comment)

I think that'd be acceptable but unfortunate. The fundamental problem (#1120) is not solved in the rename and could definitely come back if an external crate just happens to have a long path somewhere. Is there anyone on or around your team that has any amount of windows experience to take a look at the root issues. I know there are ways to tell windows to ignore MAX_PATH constraints using \\?\ but I'm not sure what all is needed to get the system to correctly understand the intention and not just see a funny looking path.

We will follow-up on #1120. It seems like there are multiple separate issues that contribute the problems with long paths on Windows and addressing all of them doesn't seem likely in the near future.

@krasimirgg
krasimirgg marked this pull request as ready for review July 12, 2022 15:25
Comment thread BUILD.bazel Resolved

@UebelAndre UebelAndre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change looks fine but am over-all nervous about introducing a transition here. I don't have any concrete issue from this though so fingers crossed, and thanks!

Comment thread rust/private/rust.bzl Resolved
Comment thread rust/private/rust.bzl Resolved
@krasimirgg
krasimirgg force-pushed the proc_macro_dep branch 3 times, most recently from f32013c to 3485cf0 Compare July 13, 2022 14:42
@scentini

Copy link
Copy Markdown
Collaborator

Thanks! With the latest update my concern about building some targets 3 instead of 2 times goes away and I think we're ready to land this.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants