Skip to content

Commit 809154d

Browse files
move _CRATES_REPOSITORY_ATTRS to a variable and only include _CRATES_REPOSITORY_ATTRS in reproducible attrs, not builtin attrs to repository_ctx.attr not added by us
1 parent af24d27 commit 809154d

File tree

1 file changed

+149
-148
lines changed

1 file changed

+149
-148
lines changed

crate_universe/private/crates_repository.bzl

Lines changed: 149 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,151 @@ SUPPORTED_PLATFORM_TRIPLES = [
3636
"x86_64-unknown-nixos-gnu",
3737
]
3838

39+
_CRATES_REPOSITORY_ATTRS = {
40+
"annotations": attr.string_list_dict(
41+
doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation).",
42+
),
43+
"cargo_config": attr.label(
44+
doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file",
45+
),
46+
"cargo_lockfile": attr.label(
47+
doc = (
48+
"The path used to store the `crates_repository` specific " +
49+
"[Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html) file. " +
50+
"In the case that your `crates_repository` corresponds directly with an existing " +
51+
"`Cargo.toml` file which has a paired `Cargo.lock` file, that `Cargo.lock` file " +
52+
"should be used here, which will keep the versions used by cargo and bazel in sync."
53+
),
54+
mandatory = True,
55+
),
56+
"compressed_windows_toolchain_names": attr.bool(
57+
doc = "Whether or not the toolchain names of windows toolchains are expected to be in a `compressed` format.",
58+
default = True,
59+
),
60+
"generate_binaries": attr.bool(
61+
doc = (
62+
"Whether to generate `rust_binary` targets for all the binary crates in every package. " +
63+
"By default only the `rust_library` targets are generated."
64+
),
65+
default = False,
66+
),
67+
"generate_build_scripts": attr.bool(
68+
doc = (
69+
"Whether or not to generate " +
70+
"[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default."
71+
),
72+
default = True,
73+
),
74+
"generate_target_compatible_with": attr.bool(
75+
doc = "DEPRECATED: Moved to `render_config`.",
76+
default = True,
77+
),
78+
"generator": attr.string(
79+
doc = (
80+
"The absolute label of a generator. Eg. `@cargo_bazel_bootstrap//:cargo-bazel`. " +
81+
"This is typically used when bootstrapping"
82+
),
83+
),
84+
"generator_sha256s": attr.string_dict(
85+
doc = "Dictionary of `host_triple` -> `sha256` for a `cargo-bazel` binary.",
86+
default = CARGO_BAZEL_SHA256S,
87+
),
88+
"generator_urls": attr.string_dict(
89+
doc = (
90+
"URL template from which to download the `cargo-bazel` binary. `{host_triple}` and will be " +
91+
"filled in according to the host platform."
92+
),
93+
default = CARGO_BAZEL_URLS,
94+
),
95+
"isolated": attr.bool(
96+
doc = (
97+
"If true, `CARGO_HOME` will be overwritten to a directory within the generated repository in " +
98+
"order to prevent other uses of Cargo from impacting having any effect on the generated targets " +
99+
"produced by this rule. For users who either have multiple `crate_repository` definitions in a " +
100+
"WORKSPACE or rapidly re-pin dependencies, setting this to false may improve build times. This " +
101+
"variable is also controlled by `CARGO_BAZEL_ISOLATED` environment variable."
102+
),
103+
default = True,
104+
),
105+
"lockfile": attr.label(
106+
doc = (
107+
"The path to a file to use for reproducible renderings. " +
108+
"If set, this file must exist within the workspace (but can be empty) before this rule will work." +
109+
"If you already have a `MODULE.bazel.lock` file, you don't need this." +
110+
"If you don't have a `MODULE.bazel.lock` file, the `lockfile` will save you generation time."
111+
),
112+
),
113+
"manifests": attr.label_list(
114+
doc = "A list of Cargo manifests (`Cargo.toml` files).",
115+
),
116+
"packages": attr.string_dict(
117+
doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec).",
118+
),
119+
"quiet": attr.bool(
120+
doc = "If stdout and stderr should not be printed to the terminal.",
121+
default = True,
122+
),
123+
"render_config": attr.string(
124+
doc = (
125+
"The configuration flags to use for rendering. Use `//crate_universe:defs.bzl\\%render_config` to " +
126+
"generate the value for this field. If unset, the defaults defined there will be used."
127+
),
128+
),
129+
"repin_instructions": attr.string(
130+
doc = "Instructions to re-pin the repository if required. Many people have wrapper scripts for keeping dependencies up to date, and would like to point users to that instead of the default.",
131+
),
132+
"rust_toolchain_cargo_template": attr.string(
133+
doc = (
134+
"The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), " +
135+
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
136+
"`{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. " +
137+
"'rustc.exe') will be replaced in the string if present."
138+
),
139+
default = "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}",
140+
),
141+
"rust_toolchain_rustc_template": attr.string(
142+
doc = (
143+
"The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), " +
144+
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
145+
"`{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. " +
146+
"'cargo.exe') will be replaced in the string if present."
147+
),
148+
default = "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}",
149+
),
150+
"rust_version": attr.string(
151+
doc = "The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08`",
152+
default = rust_common.default_version,
153+
),
154+
"skip_cargo_lockfile_overwrite": attr.bool(
155+
doc = (
156+
"Whether to skip writing the cargo lockfile back after resolving. " +
157+
"You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " +
158+
"But you probably don't want to set this."
159+
),
160+
default = False,
161+
),
162+
"splicing_config": attr.string(
163+
doc = (
164+
"The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\%rsplicing_config` to " +
165+
"generate the value for this field. If unset, the defaults defined there will be used."
166+
),
167+
),
168+
"strip_internal_dependencies_from_cargo_lockfile": attr.bool(
169+
doc = (
170+
"Whether to strip internal dependencies from the cargo lockfile. " +
171+
"You may want to use this if you want to maintain a cargo lockfile for bazel only. " +
172+
"Bazel only requires external dependencies to be present in the lockfile. " +
173+
"By removing internal dependencies, the lockfile changes less frequently which reduces merge conflicts " +
174+
"in other lockfiles where the cargo lockfile's sha is stored."
175+
),
176+
default = False,
177+
),
178+
"supported_platform_triples": attr.string_list(
179+
doc = "A set of all platform triples to consider when generating dependencies.",
180+
default = SUPPORTED_PLATFORM_TRIPLES,
181+
),
182+
}
183+
39184
def _crates_repository_impl(repository_ctx):
40185
# Determine the current host's platform triple
41186
host_triple = get_host_triple(repository_ctx)
@@ -137,10 +282,9 @@ def _crates_repository_impl(repository_ctx):
137282
print("WARN: {}".format(warning))
138283

139284
# Determine the set of reproducible values
140-
attrs = {attr: getattr(repository_ctx.attr, attr) for attr in dir(repository_ctx.attr)}
141-
exclude = ["to_json", "to_proto"]
142-
for attr in exclude:
143-
attrs.pop(attr, None)
285+
attrs = {"name": repository_ctx.name}
286+
for key in _CRATES_REPOSITORY_ATTRS:
287+
attrs[key] = getattr(repository_ctx.attr, key)
144288

145289
# Note that this is only scoped to the current host platform. Users should
146290
# ensure they provide all the values necessary for the host environments
@@ -249,149 +393,6 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
249393
250394
""",
251395
implementation = _crates_repository_impl,
252-
attrs = {
253-
"annotations": attr.string_list_dict(
254-
doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation).",
255-
),
256-
"cargo_config": attr.label(
257-
doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file",
258-
),
259-
"cargo_lockfile": attr.label(
260-
doc = (
261-
"The path used to store the `crates_repository` specific " +
262-
"[Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html) file. " +
263-
"In the case that your `crates_repository` corresponds directly with an existing " +
264-
"`Cargo.toml` file which has a paired `Cargo.lock` file, that `Cargo.lock` file " +
265-
"should be used here, which will keep the versions used by cargo and bazel in sync."
266-
),
267-
mandatory = True,
268-
),
269-
"compressed_windows_toolchain_names": attr.bool(
270-
doc = "Whether or not the toolchain names of windows toolchains are expected to be in a `compressed` format.",
271-
default = True,
272-
),
273-
"generate_binaries": attr.bool(
274-
doc = (
275-
"Whether to generate `rust_binary` targets for all the binary crates in every package. " +
276-
"By default only the `rust_library` targets are generated."
277-
),
278-
default = False,
279-
),
280-
"generate_build_scripts": attr.bool(
281-
doc = (
282-
"Whether or not to generate " +
283-
"[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default."
284-
),
285-
default = True,
286-
),
287-
"generate_target_compatible_with": attr.bool(
288-
doc = "DEPRECATED: Moved to `render_config`.",
289-
default = True,
290-
),
291-
"generator": attr.string(
292-
doc = (
293-
"The absolute label of a generator. Eg. `@cargo_bazel_bootstrap//:cargo-bazel`. " +
294-
"This is typically used when bootstrapping"
295-
),
296-
),
297-
"generator_sha256s": attr.string_dict(
298-
doc = "Dictionary of `host_triple` -> `sha256` for a `cargo-bazel` binary.",
299-
default = CARGO_BAZEL_SHA256S,
300-
),
301-
"generator_urls": attr.string_dict(
302-
doc = (
303-
"URL template from which to download the `cargo-bazel` binary. `{host_triple}` and will be " +
304-
"filled in according to the host platform."
305-
),
306-
default = CARGO_BAZEL_URLS,
307-
),
308-
"isolated": attr.bool(
309-
doc = (
310-
"If true, `CARGO_HOME` will be overwritten to a directory within the generated repository in " +
311-
"order to prevent other uses of Cargo from impacting having any effect on the generated targets " +
312-
"produced by this rule. For users who either have multiple `crate_repository` definitions in a " +
313-
"WORKSPACE or rapidly re-pin dependencies, setting this to false may improve build times. This " +
314-
"variable is also controlled by `CARGO_BAZEL_ISOLATED` environment variable."
315-
),
316-
default = True,
317-
),
318-
"lockfile": attr.label(
319-
doc = (
320-
"The path to a file to use for reproducible renderings. " +
321-
"If set, this file must exist within the workspace (but can be empty) before this rule will work." +
322-
"If you already have a `MODULE.bazel.lock` file, you don't need this." +
323-
"If you don't have a `MODULE.bazel.lock` file, the `lockfile` will save you generation time."
324-
),
325-
),
326-
"manifests": attr.label_list(
327-
doc = "A list of Cargo manifests (`Cargo.toml` files).",
328-
),
329-
"packages": attr.string_dict(
330-
doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec).",
331-
),
332-
"quiet": attr.bool(
333-
doc = "If stdout and stderr should not be printed to the terminal.",
334-
default = True,
335-
),
336-
"render_config": attr.string(
337-
doc = (
338-
"The configuration flags to use for rendering. Use `//crate_universe:defs.bzl\\%render_config` to " +
339-
"generate the value for this field. If unset, the defaults defined there will be used."
340-
),
341-
),
342-
"repin_instructions": attr.string(
343-
doc = "Instructions to re-pin the repository if required. Many people have wrapper scripts for keeping dependencies up to date, and would like to point users to that instead of the default.",
344-
),
345-
"rust_toolchain_cargo_template": attr.string(
346-
doc = (
347-
"The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), " +
348-
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
349-
"`{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. " +
350-
"'rustc.exe') will be replaced in the string if present."
351-
),
352-
default = "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}",
353-
),
354-
"rust_toolchain_rustc_template": attr.string(
355-
doc = (
356-
"The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), " +
357-
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
358-
"`{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. " +
359-
"'cargo.exe') will be replaced in the string if present."
360-
),
361-
default = "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}",
362-
),
363-
"rust_version": attr.string(
364-
doc = "The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08`",
365-
default = rust_common.default_version,
366-
),
367-
"skip_cargo_lockfile_overwrite": attr.bool(
368-
doc = (
369-
"Whether to skip writing the cargo lockfile back after resolving. " +
370-
"You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " +
371-
"But you probably don't want to set this."
372-
),
373-
default = False,
374-
),
375-
"splicing_config": attr.string(
376-
doc = (
377-
"The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\%rsplicing_config` to " +
378-
"generate the value for this field. If unset, the defaults defined there will be used."
379-
),
380-
),
381-
"strip_internal_dependencies_from_cargo_lockfile": attr.bool(
382-
doc = (
383-
"Whether to strip internal dependencies from the cargo lockfile. " +
384-
"You may want to use this if you want to maintain a cargo lockfile for bazel only. " +
385-
"Bazel only requires external dependencies to be present in the lockfile. " +
386-
"By removing internal dependencies, the lockfile changes less frequently which reduces merge conflicts " +
387-
"in other lockfiles where the cargo lockfile's sha is stored."
388-
),
389-
default = False,
390-
),
391-
"supported_platform_triples": attr.string_list(
392-
doc = "A set of all platform triples to consider when generating dependencies.",
393-
default = SUPPORTED_PLATFORM_TRIPLES,
394-
),
395-
},
396+
attrs = _CRATES_REPOSITORY_ATTRS,
396397
environ = CRATES_REPOSITORY_ENVIRON,
397398
)

0 commit comments

Comments
 (0)