From 71cb7b406d566a3cfc17637042b8a9ccda0917ef Mon Sep 17 00:00:00 2001 From: bors Date: Thu, 8 Aug 2024 21:22:58 +0000 Subject: [PATCH 1/3] Auto merge of #14359 - linyihai:issue-14354, r=weihanglo Fix: `cargo package` failed on bare commit git repo. ### What does this PR try to resolve? Fixes #14354 This approach chose to not generate a `.cargo_vcs_info.json` for bare commit git repo. ### How should we test and review this PR? Compare the test changes before and after the two commits ### Additional information --- src/cargo/ops/cargo_package.rs | 21 ++++++++++++++------- tests/testsuite/package.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index b33383422cd..dfe9ec329f8 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -583,10 +583,12 @@ fn check_repo_state( .and_then(|p| p.to_str()) .unwrap_or("") .replace("\\", "/"); - return Ok(Some(VcsInfo { - git: git(p, src_files, &repo, &opts)?, - path_in_vcs, - })); + let Some(git) = git(p, src_files, &repo, &opts)? else { + // If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning, + // then don't generate the corresponding file in order to maintain consistency with past behavior. + return Ok(None); + }; + return Ok(Some(VcsInfo { git, path_in_vcs })); } } gctx.shell().verbose(|shell| { @@ -612,7 +614,7 @@ fn check_repo_state( src_files: &[PathBuf], repo: &git2::Repository, opts: &PackageOpts<'_>, - ) -> CargoResult { + ) -> CargoResult> { // This is a collection of any dirty or untracked files. This covers: // - new/modified/deleted/renamed/type change (index or worktree) // - untracked files (which are "new" worktree files) @@ -639,11 +641,16 @@ fn check_repo_state( .collect(); let dirty = !dirty_src_files.is_empty(); if !dirty || opts.allow_dirty { + // Must check whetherthe repo has no commit firstly, otherwise `revparse_single` would fail on bare commit repo. + // Due to lacking the `sha1` field, it's better not record the `GitVcsInfo` for consistency. + if repo.is_empty()? { + return Ok(None); + } let rev_obj = repo.revparse_single("HEAD")?; - Ok(GitVcsInfo { + Ok(Some(GitVcsInfo { sha1: rev_obj.id().to_string(), dirty, - }) + })) } else { anyhow::bail!( "{} files in the working directory contain changes that were \ diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 773cdf75872..90aa6575c67 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1255,6 +1255,36 @@ fn issue_13695_allowing_dirty_vcs_info_but_clean() { ); } +#[cargo_test] +fn issue_14354_allowing_dirty_bare_commit() { + let p = project().build(); + // Init a bare commit git repo + let _ = git::repo(&paths::root().join("foo")) + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2015" + description = "foo" + license = "foo" + documentation = "foo" + "#, + ) + .file("src/lib.rs", ""); + + p.cargo("package --allow-dirty").run(); + + let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.1.0.crate", + &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], + &[], + ); +} + #[cargo_test] fn generated_manifest() { let registry = registry::alt_init(); From b24a555d0c6e8cfbef6f7851cb01b15217b88161 Mon Sep 17 00:00:00 2001 From: Urgau Date: Mon, 5 Aug 2024 11:14:16 +0200 Subject: [PATCH 2/3] Fix renamed disallowed cfg lint name --- tests/testsuite/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 49eb60db0c2..b73016554bd 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -6576,7 +6576,7 @@ fn user_specific_cfgs_are_filtered_out() { ) .build(); - p.cargo("rustc -- --cfg debug_assertions --cfg proc_macro -Aunknown_lints -Aunexpected_builtin_cfgs") + p.cargo("rustc -- --cfg debug_assertions --cfg proc_macro -Aunknown_lints -Aexplicit_builtin_cfgs_in_flags") .run(); p.process(&p.bin("foo")).run(); } From 4261a9d3be998f0d29058ded8f699dd5fb134da2 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 8 Aug 2024 21:59:12 -0400 Subject: [PATCH 3/3] test: disable build-std test to unblock beta-1.81 backport --- tests/build-std/main.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index 33df92459de..fcace7336fc 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -133,6 +133,8 @@ fn basic() { assert_eq!(p.glob(deps_dir.join("*.dylib")).count(), 0); } +#[allow(unused_attributes)] +#[ignore = "to unblock beta-1.81 backport"] #[cargo_test(build_std_real)] fn cross_custom() { let p = project() @@ -176,6 +178,8 @@ fn cross_custom() { .run(); } +#[allow(unused_attributes)] +#[ignore = "to unblock beta-1.81 backport"] #[cargo_test(build_std_real)] fn custom_test_framework() { let p = project() @@ -237,6 +241,8 @@ fn custom_test_framework() { // Fixing rust-lang/rust#117839. // on macOS it never gets remapped. // Might be a separate issue, so only run on Linux. +#[allow(unused_attributes)] +#[ignore = "to unblock beta-1.81 backport"] #[cargo_test(build_std_real)] #[cfg(target_os = "linux")] fn remap_path_scope() {