Skip to content

Commit 6dd57b2

Browse files
committed
Auto merge of #7779 - ehuss:fix-cargo-lock-ignore, r=alexcrichton
Fix .gitignore of Cargo.lock in a subdirectory. The code for checking if `Cargo.lock` is ignored was erroneously assuming it was at the root of the git repo. This would cause a problem if `Cargo.lock` is in `.gitignore` in a subdirectory. Fixes issue noted in #7705 (comment)
2 parents c95f396 + 3cedb8e commit 6dd57b2

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/cargo/ops/cargo_package.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ fn check_repo_state(
299299
if let Ok(status) = repo.status_file(relative) {
300300
if status == git2::Status::CURRENT {
301301
false
302-
} else if relative.to_str().unwrap_or("") == "Cargo.lock" {
302+
} else if relative.file_name().and_then(|s| s.to_str()).unwrap_or("")
303+
== "Cargo.lock"
304+
{
303305
// It is OK to include this file even if it is ignored.
304306
status != git2::Status::IGNORED
305307
} else {

tests/testsuite/publish_lockfile.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -404,24 +404,18 @@ fn ignore_lockfile() {
404404
// With an explicit `include` list, but Cargo.lock in .gitignore, don't
405405
// complain about `Cargo.lock` being ignored. Note that it is still
406406
// included in the packaged regardless.
407-
let (p, _r) = git::new_repo("foo", |p| {
407+
let p = git::new("foo", |p| {
408408
p.file(
409409
"Cargo.toml",
410-
r#"
411-
[package]
412-
name = "foo"
413-
version = "0.0.1"
414-
authors = []
415-
license = "MIT"
416-
description = "foo"
417-
documentation = "foo"
418-
homepage = "foo"
419-
repository = "foo"
420-
410+
&pl_manifest(
411+
"foo",
412+
"0.0.1",
413+
r#"
421414
include = [
422415
"src/main.rs"
423416
]
424-
"#,
417+
"#,
418+
),
425419
)
426420
.file("src/main.rs", "fn main() {}")
427421
.file(".gitignore", "Cargo.lock")
@@ -453,3 +447,27 @@ src/main.rs
453447
)
454448
.run();
455449
}
450+
451+
#[cargo_test]
452+
fn ignore_lockfile_inner() {
453+
// Ignore `Cargo.lock` if in .gitignore in a git subdirectory.
454+
let p = git::new("foo", |p| {
455+
p.no_manifest()
456+
.file("bar/Cargo.toml", &pl_manifest("bar", "0.0.1", ""))
457+
.file("bar/src/main.rs", "fn main() {}")
458+
.file("bar/.gitignore", "Cargo.lock")
459+
});
460+
p.cargo("generate-lockfile").cwd("bar").run();
461+
p.cargo("package -v --no-verify")
462+
.cwd("bar")
463+
.with_stderr(
464+
"\
465+
[PACKAGING] bar v0.0.1 ([..])
466+
[ARCHIVING] Cargo.toml
467+
[ARCHIVING] src/main.rs
468+
[ARCHIVING] .cargo_vcs_info.json
469+
[ARCHIVING] Cargo.lock
470+
",
471+
)
472+
.run();
473+
}

0 commit comments

Comments
 (0)