@@ -630,3 +630,178 @@ feat2 = ["bar?/feat"]
630
630
) ] ,
631
631
) ;
632
632
}
633
+
634
+ #[ cargo_test]
635
+ fn disabled_weak_direct_dep ( ) {
636
+ // Issue #10801
637
+ // A weak direct dependency should be included in Cargo.lock,
638
+ // even if disabled, and even if on lockfile version 4.
639
+ Package :: new ( "bar" , "1.0.0" )
640
+ . feature ( "feat" , & [ ] )
641
+ . file ( "src/lib.rs" , & require ( & [ "feat" ] , & [ ] ) )
642
+ . publish ( ) ;
643
+ let p = project ( )
644
+ . file (
645
+ "Cargo.toml" ,
646
+ r#"
647
+ [package]
648
+ name = "foo"
649
+ version = "0.1.0"
650
+
651
+ [dependencies]
652
+ bar = { version = "1.0", optional = true }
653
+
654
+ [features]
655
+ f1 = ["bar?/feat"]
656
+ "# ,
657
+ )
658
+ . file ( "src/lib.rs" , & require ( & [ "f1" ] , & [ ] ) )
659
+ . build ( ) ;
660
+
661
+ p. cargo ( "generate-lockfile" ) . run ( ) ;
662
+
663
+ let lockfile = p. read_lockfile ( ) ;
664
+ assert ! (
665
+ lockfile. contains( r#"version = 3"# ) ,
666
+ "lockfile version is not 3!\n {lockfile}" ,
667
+ ) ;
668
+ // Previous behavior: bar is inside lockfile.
669
+ assert ! (
670
+ lockfile. contains( r#"name = "bar""# ) ,
671
+ "bar not found\n {lockfile}" ,
672
+ ) ;
673
+
674
+ // Update to new lockfile version
675
+ let new_lockfile = lockfile. replace ( "version = 3" , "version = 4" ) ;
676
+ p. change_file ( "Cargo.lock" , & new_lockfile) ;
677
+
678
+ p. cargo ( "check --features f1" )
679
+ . with_stderr (
680
+ "\
681
+ [CHECKING] foo v0.1.0 [..]
682
+ [FINISHED] [..]
683
+ " ,
684
+ )
685
+ . run ( ) ;
686
+
687
+ let lockfile = p. read_lockfile ( ) ;
688
+ assert ! (
689
+ lockfile. contains( r#"version = 4"# ) ,
690
+ "lockfile version is not 4!\n {lockfile}" ,
691
+ ) ;
692
+ // New behavior: bar is still there because it is a direct (optional) dependency.
693
+ assert ! (
694
+ lockfile. contains( r#"name = "bar""# ) ,
695
+ "bar not found\n {lockfile}" ,
696
+ ) ;
697
+
698
+ p. cargo ( "check --features f1,bar" )
699
+ . with_stderr (
700
+ "\
701
+ [DOWNLOADING] crates ...
702
+ [DOWNLOADED] bar v1.0.0 [..]
703
+ [CHECKING] bar v1.0.0
704
+ [CHECKING] foo v0.1.0 [..]
705
+ [FINISHED] [..]
706
+ " ,
707
+ )
708
+ . run ( ) ;
709
+ }
710
+
711
+ #[ cargo_test]
712
+ fn disabled_weak_optional_deps ( ) {
713
+ // Issue #10801
714
+ // A weak dependency of a dependency should not be included in Cargo.lock,
715
+ // at least on lockfile version 4.
716
+ Package :: new ( "bar" , "1.0.0" )
717
+ . feature ( "feat" , & [ ] )
718
+ . file ( "src/lib.rs" , & require ( & [ "feat" ] , & [ ] ) )
719
+ . publish ( ) ;
720
+ Package :: new ( "dep" , "1.0.0" )
721
+ . add_dep ( Dependency :: new ( "bar" , "1.0" ) . optional ( true ) )
722
+ . feature ( "feat" , & [ "bar?/feat" ] )
723
+ //.feature("default", &["feat"])
724
+ . file ( "src/lib.rs" , "" )
725
+ . publish ( ) ;
726
+ let p = project ( )
727
+ . file (
728
+ "Cargo.toml" ,
729
+ r#"
730
+ [package]
731
+ name = "foo"
732
+ version = "0.1.0"
733
+
734
+ [dependencies]
735
+ dep = { version = "1.0", features = ["feat"] }
736
+ "# ,
737
+ )
738
+ . file ( "src/lib.rs" , "" )
739
+ . build ( ) ;
740
+
741
+ p. cargo ( "generate-lockfile" ) . run ( ) ;
742
+
743
+ let lockfile = p. read_lockfile ( ) ;
744
+
745
+ assert ! (
746
+ lockfile. contains( r#"version = 3"# ) ,
747
+ "lockfile version is not 3!\n {lockfile}" ,
748
+ ) ;
749
+ // Previous behavior: bar is inside lockfile.
750
+ assert ! (
751
+ lockfile. contains( r#"name = "bar""# ) ,
752
+ "bar not found\n {lockfile}" ,
753
+ ) ;
754
+
755
+ // Update to new lockfile version
756
+ let new_lockfile = lockfile. replace ( "version = 3" , "version = 4" ) ;
757
+ p. change_file ( "Cargo.lock" , & new_lockfile) ;
758
+
759
+ // Note how we are not downloading bar here
760
+ p. cargo ( "check" )
761
+ . with_stderr (
762
+ "\
763
+ [DOWNLOADING] crates ...
764
+ [DOWNLOADED] dep v1.0.0 [..]
765
+ [CHECKING] dep v1.0.0
766
+ [CHECKING] foo v0.1.0 [..]
767
+ [FINISHED] [..]
768
+ " ,
769
+ )
770
+ . run ( ) ;
771
+
772
+ let lockfile = p. read_lockfile ( ) ;
773
+ assert ! (
774
+ lockfile. contains( r#"version = 4"# ) ,
775
+ "lockfile version is not 4!\n {lockfile}" ,
776
+ ) ;
777
+ // New behavior: bar is gone.
778
+ assert ! (
779
+ !lockfile. contains( r#"name = "bar""# ) ,
780
+ "bar inside lockfile!\n {lockfile}" ,
781
+ ) ;
782
+
783
+ // Note how we are not downloading bar here
784
+ p. cargo ( "check --features dep/bar" )
785
+ . with_stderr (
786
+ "\
787
+ [DOWNLOADING] crates ...
788
+ [DOWNLOADED] bar v1.0.0 [..]
789
+ [CHECKING] bar v1.0.0
790
+ [CHECKING] dep v1.0.0
791
+ [CHECKING] foo v0.1.0 [..]
792
+ [FINISHED] [..]
793
+ " ,
794
+ )
795
+ . run ( ) ;
796
+
797
+ let lockfile = p. read_lockfile ( ) ;
798
+ assert ! (
799
+ lockfile. contains( r#"version = 4"# ) ,
800
+ "lockfile version is not 4!\n {lockfile}" ,
801
+ ) ;
802
+ // New behavior: bar is gone.
803
+ assert ! (
804
+ lockfile. contains( r#"name = "bar""# ) ,
805
+ "bar inside lockfile!\n {lockfile}" ,
806
+ ) ;
807
+ }
0 commit comments