diff --git a/docs/flatten.md b/docs/flatten.md index 7761f79d54..6a0bca539f 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1162,7 +1162,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito | edition | The rust edition to be used by default. | String | optional | "2015" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | | extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] | -| include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. | Boolean | optional | False | +| include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. An environment variable RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS can also be used to control this attribute. This variable will take precedence over the hard coded attribute. Setting it to true to activates this attribute where all other values deactivate it. | Boolean | optional | False | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | | rustfmt_version | The version of the tool among "nightly", "beta", or an exact version. | String | optional | "" | @@ -1564,7 +1564,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | edition | The rust edition to be used by default (2015 (default) or 2018) | None | | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | False | | sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | None | -| include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | False | +| include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details | False | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | diff --git a/docs/rust_analyzer.md b/docs/rust_analyzer.md index bd7964d1b5..0a74a08341 100644 --- a/docs/rust_analyzer.md +++ b/docs/rust_analyzer.md @@ -21,9 +21,17 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_deps") rust_analyzer_deps() ``` -You'll also need to ensure any `rust_repository` or `rust_repositories` calls in your `WORKSPACE` file set the arg `include_rustc_src` to `True`. +Next, add the following lines to the `.bazelrc` file of your workspace: +``` +build --repo_env=RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS=true +``` + +This will ensure rust source code is available to `rust-analyzer`. Users +can also set `include_rustc_srcs = True` on any `rust_repository` or +`rust_repositories` calls in the workspace but the environment variable +has higher priority and can override the attribute. -Then, add a rule to the root `BUILD` file like the following. +Finally, add a rule to the root `BUILD` file like the following. ```python load("@rules_rust//rust:defs.bzl", "rust_analyzer") diff --git a/docs/rust_analyzer.vm b/docs/rust_analyzer.vm index e8c1d13118..a3cf522555 100644 --- a/docs/rust_analyzer.vm +++ b/docs/rust_analyzer.vm @@ -16,9 +16,17 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_deps") rust_analyzer_deps() ``` -You'll also need to ensure any `rust_repository` or `rust_repositories` calls in your `WORKSPACE` file set the arg `include_rustc_src` to `True`. +Next, add the following lines to the `.bazelrc` file of your workspace: +``` +build --repo_env=RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS=true +``` + +This will ensure rust source code is available to `rust-analyzer`. Users +can also set `include_rustc_srcs = True` on any `rust_repository` or +`rust_repositories` calls in the workspace but the environment variable +has higher priority and can override the attribute. -Then, add a rule to the root `BUILD` file like the following. +Finally, add a rule to the root `BUILD` file like the following. ```python load("@rules_rust//rust:defs.bzl", "rust_analyzer") diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index 8a9f55cca5..f4ad7cab69 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -108,7 +108,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito | edition | The rust edition to be used by default. | String | optional | "2015" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | | extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] | -| include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. | Boolean | optional | False | +| include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. An environment variable RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS can also be used to control this attribute. This variable will take precedence over the hard coded attribute. Setting it to true to activates this attribute where all other values deactivate it. | Boolean | optional | False | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | | rustfmt_version | The version of the tool among "nightly", "beta", or an exact version. | String | optional | "" | @@ -179,7 +179,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | edition | The rust edition to be used by default (2015 (default) or 2018) | None | | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | False | | sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | None | -| include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | False | +| include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details | False | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | diff --git a/rust/repositories.bzl b/rust/repositories.bzl index a0b644ed5f..d687b7c3d3 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -61,7 +61,8 @@ def rust_repositories( edition (str, optional): The rust edition to be used by default (2015 (default) or 2018) dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. Defaults to None. - include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. + include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. + See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] """ @@ -586,7 +587,15 @@ def _rust_toolchain_repository_impl(ctx): _check_version_valid(ctx.attr.version, ctx.attr.iso_date) - if ctx.attr.include_rustc_srcs: + # Determing whether or not to include rustc sources in the toolchain. The environment + # variable will always take precedence over the attribute. + include_rustc_srcs_env = ctx.os.environ.get("RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS") + if include_rustc_srcs_env != None: + include_rustc_srcs = include_rustc_srcs_env.lower() in ["true", "1"] + else: + include_rustc_srcs = ctx.attr.include_rustc_srcs + + if include_rustc_srcs: _load_rust_src(ctx) build_components = [_load_rust_compiler(ctx)] @@ -650,7 +659,13 @@ rust_toolchain_repository = repository_rule( doc = "Additional rust-style targets that this set of toolchains should support.", ), "include_rustc_srcs": attr.bool( - doc = "Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer.", + doc = ( + "Whether to download and unpack the rustc source files. These are very large, and " + + "slow to unpack, but are required to support rust analyzer. An environment variable " + + "`RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS` can also be used to control this attribute. " + + "This variable will take precedence over the hard coded attribute. Setting it to `true` to " + + "activates this attribute where all other values deactivate it." + ), default = False, ), "iso_date": attr.string( @@ -675,6 +690,7 @@ rust_toolchain_repository = repository_rule( ), }, implementation = _rust_toolchain_repository_impl, + environ = ["RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS"], ) rust_toolchain_repository_proxy = repository_rule(