@@ -624,58 +624,65 @@ def build_sdist(
624624 sdist_root_dir : pathlib .Path ,
625625 build_env : build_environment .BuildEnvironment ,
626626) -> pathlib .Path :
627- """Build a source distribution using the configured strategy or legacy hook."""
627+ """Build a source distribution using the configured strategy or legacy hook.
628+
629+ ``source.build_sdist: hook`` requires a package override. Configured
630+ ``pep517`` and ``tarball`` strategies take precedence over overrides.
631+
632+ .. versionchanged:: 0.104.0
633+ Configured source profiles can select the ``build_sdist`` override.
634+ """
628635 pbi = ctx .package_build_info (req )
629636 source_resolver = pbi .source_resolver
637+ strategy = source_resolver .build_sdist if source_resolver is not None else None
630638 build_dir = pbi .build_dir (sdist_root_dir )
631639
632640 logger .info (f"building { ctx .variant } source distribution for { req } in { build_dir } " )
641+ if source_resolver is not None :
642+ build_sdist_override = overrides .find_override_method (req .name , "build_sdist" )
643+ if strategy is packagesettings .BuildSDist .hook :
644+ if not callable (build_sdist_override ):
645+ raise ValueError (
646+ f"{ req .name } : source.build_sdist='hook' requires a callable "
647+ "build_sdist override hook"
648+ )
649+ elif build_sdist_override is not None :
650+ logger .warning (
651+ "%s: ignoring build_sdist override because source.build_sdist is %s" ,
652+ req .name ,
653+ strategy ,
654+ )
633655 extra_environ = packagesettings .get_extra_environ (
634656 ctx = ctx ,
635657 req = req ,
636658 version = version ,
637659 sdist_root_dir = sdist_root_dir ,
638660 build_env = build_env ,
639661 )
640- if source_resolver is not None :
641- if overrides .find_override_method (req .name , "build_sdist" ) is not None :
642- logger .warning (
643- "%s: ignoring build_sdist override because source.build_sdist is %s" ,
644- req .name ,
645- source_resolver .build_sdist ,
646- )
647- if (
648- source_resolver is not None
649- and source_resolver .build_sdist is packagesettings .BuildSDist .pep517
650- ):
651- sdist_file = pep517_build_sdist (
662+ if source_resolver is None or strategy is packagesettings .BuildSDist .hook :
663+ sdist_file : pathlib .Path = overrides .find_and_invoke (
664+ req .name ,
665+ "build_sdist" ,
666+ default_build_sdist ,
652667 ctx = ctx ,
653668 extra_environ = extra_environ ,
654669 req = req ,
655670 version = version ,
656671 sdist_root_dir = sdist_root_dir ,
672+ build_dir = build_dir ,
657673 build_env = build_env ,
658674 )
659- elif (
660- source_resolver is not None
661- and source_resolver .build_sdist is packagesettings .BuildSDist .tarball
662- ):
663- sdist_file = default_build_sdist (
675+ elif strategy is packagesettings .BuildSDist .pep517 :
676+ sdist_file = pep517_build_sdist (
664677 ctx = ctx ,
665678 extra_environ = extra_environ ,
666679 req = req ,
667680 version = version ,
668681 sdist_root_dir = sdist_root_dir ,
669- build_dir = build_dir ,
670682 build_env = build_env ,
671- # Git checkouts keep their `.git`; keep it out of the sdist.
672- exclude_vcs = True ,
673683 )
674- elif source_resolver is None :
675- sdist_file = overrides .find_and_invoke (
676- req .name ,
677- "build_sdist" ,
678- default_build_sdist ,
684+ elif strategy is packagesettings .BuildSDist .tarball :
685+ sdist_file = default_build_sdist (
679686 ctx = ctx ,
680687 extra_environ = extra_environ ,
681688 req = req ,
@@ -686,8 +693,7 @@ def build_sdist(
686693 )
687694 else :
688695 raise ValueError (
689- f"{ req .name } : unsupported source.build_sdist strategy "
690- f"{ source_resolver .build_sdist !r} "
696+ f"{ req .name } : unsupported source.build_sdist strategy { strategy !r} "
691697 )
692698 logger .info (f"built source distribution { sdist_file } " )
693699
@@ -713,12 +719,20 @@ def default_build_sdist(
713719 build_env : build_environment .BuildEnvironment ,
714720 build_dir : pathlib .Path ,
715721 * ,
716- exclude_vcs : bool = False ,
722+ exclude_vcs : bool | None = None ,
717723) -> pathlib .Path :
718724 """Repack *build_dir* as an sdist.
719725
720- With *exclude_vcs*, VCS metadata such as ``.git`` is left out.
726+ With *exclude_vcs*, VCS metadata such as ``.git`` is left out. By default,
727+ exclude it when ``source:`` is configured, and retain it otherwise.
728+ Explicit booleans override this policy.
729+
730+ .. versionchanged:: 0.104.0
731+ With ``exclude_vcs=None``, exclude VCS metadata when the effective
732+ package or variant has ``source:`` configured; retain it otherwise.
721733 """
734+ if exclude_vcs is None :
735+ exclude_vcs = ctx .package_build_info (req ).source_resolver is not None
722736 # It seems like the "correct" way to do this would be to run the
723737 # PEP 517 API in the source tree we have modified. However, quite
724738 # a few packages assume their source distribution is being built
0 commit comments