Skip to content

Make Cargo.lock overwrite optional #3526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crate_universe/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ def _generate_hub_and_spokes(
render_config,
splicing_config,
lockfile,
skip_cargo_lockfile_overwrite,
cargo_lockfile = None,
manifests = {},
packages = {}):
Expand All @@ -571,6 +572,9 @@ def _generate_hub_and_spokes(
render_config (dict): The render config to use.
splicing_config (dict): The splicing config to use.
lockfile (path): The path to the crate_universe lock file, if one was provided.
skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving.
You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
But you probably don't want to set this.
cargo_lockfile (path): Path to Cargo.lock, if we have one.
manifests (dict): The set of Cargo.toml manifests that apply to this closure, if any, keyed by path.
packages (dict): The set of extra cargo crate tags that apply to this closure, if any, keyed by package name.
Expand Down Expand Up @@ -672,6 +676,7 @@ def _generate_hub_and_spokes(
nonhermetic_root_bazel_workspace_dir = nonhermetic_root_bazel_workspace_dir,
paths_to_track_file = paths_to_track_file,
warnings_output_file = warnings_output_file,
skip_cargo_lockfile_overwrite = skip_cargo_lockfile_overwrite,
**kwargs
)

Expand Down Expand Up @@ -1018,6 +1023,7 @@ def _crate_impl(module_ctx):
splicing_config = splicing_config,
manifests = manifests,
packages = packages,
skip_cargo_lockfile_overwrite = cfg.skip_cargo_lockfile_overwrite,
)

metadata_kwargs = {}
Expand Down Expand Up @@ -1051,6 +1057,14 @@ _FROM_COMMON_ATTRS = {
"If set, this file must exist within the workspace (but can be empty) before this rule will work."
),
),
"skip_cargo_lockfile_overwrite": attr.bool(
doc = (
"Whether to skip writing the cargo lockfile back after resolving. " +
"You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " +
"But you probably don't want to set this."
),
default = False,
),
"supported_platform_triples": attr.string_list(
doc = "A set of all platform triples to consider when generating dependencies.",
default = SUPPORTED_PLATFORM_TRIPLES,
Expand Down
9 changes: 9 additions & 0 deletions crate_universe/private/crates_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def _crates_repository_impl(repository_ctx):
nonhermetic_root_bazel_workspace_dir = repository_ctx.workspace_root,
paths_to_track_file = paths_to_track_file,
warnings_output_file = warnings_output_file,
skip_cargo_lockfile_overwrite = repository_ctx.attr.skip_cargo_lockfile_overwrite,
# sysroot = tools.sysroot,
**kwargs
)
Expand Down Expand Up @@ -354,6 +355,14 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
doc = "The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08`",
default = rust_common.default_version,
),
"skip_cargo_lockfile_overwrite": attr.bool(
doc = (
"Whether to skip writing the cargo lockfile back after resolving. " +
"You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " +
"But you probably don't want to set this."
),
default = False,
),
"splicing_config": attr.string(
doc = (
"The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\%rsplicing_config` to " +
Expand Down
9 changes: 9 additions & 0 deletions crate_universe/private/generate_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def execute_generator(
nonhermetic_root_bazel_workspace_dir,
paths_to_track_file,
warnings_output_file,
skip_cargo_lockfile_overwrite,
metadata = None,
generator_label = None):
"""Execute the `cargo-bazel` binary to produce `BUILD` and `.bzl` files.
Expand All @@ -452,6 +453,9 @@ def execute_generator(
nonhermetic_root_bazel_workspace_dir (path): The path to the current workspace root
paths_to_track_file (path): Path to file where generator should write which files should trigger re-generating as a JSON list.
warnings_output_file (path): Path to file where generator should write warnings to print.
skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving.
You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
But you probably don't want to set this.
generator_label (Label): The label of the `generator` parameter.
metadata (path, optional): The path to a Cargo metadata json file. If this is set, it indicates to
the generator that repinning is required. This file must be adjacent to a `Cargo.toml` and
Expand Down Expand Up @@ -490,6 +494,11 @@ def execute_generator(
lockfile_path,
])

if skip_cargo_lockfile_overwrite:
args.extend([
"--skip-cargo-lockfile-overwrite",
])

# Some components are not required unless re-pinning is enabled
if metadata:
args.extend([
Expand Down
10 changes: 9 additions & 1 deletion crate_universe/src/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ pub struct GenerateOptions {
/// so this provides a way for the repository rule to force printing.
#[clap(long)]
pub warnings_output_path: PathBuf,

/// Whether to skip writing the cargo lockfile back after resolving.
/// You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
/// But you probably don't want to set this.
#[clap(long)]
pub skip_cargo_lockfile_overwrite: bool,
}

pub fn generate(opt: GenerateOptions) -> Result<()> {
Expand Down Expand Up @@ -213,7 +219,9 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
write_lockfile(lock_content, &lockfile, opt.dry_run)?;
}

update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?;
if !opt.skip_cargo_lockfile_overwrite {
update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?;
}

Ok(())
}
Expand Down