• 
      

    Patch setuptools to remove mandatory config

    Review Request #14957 — Created March 24, 2026 and submitted

    Information

    buildthings
    master

    Reviewers

    Buildthings injected dependencies into Setuptools through a bit of an
    annoying workaround that we inherited from the in-tree build backends
    for Djblets and Review Board. This required telling Setuptools where to
    find a package-requirements.txt file, which we'd then write to before
    Setuptools got a chance to run, and that it should look up dependencies
    dynamically.

    This change swaps out that hacky workaround for another hacky
    workaround. Instead of making the pyproject.toml file work around
    this, we now do it inside of buildthings.

    When setuptools loads pyproject.toml, it creates a _ConfigExpander
    object that builds a finalized dictionary of settings (factoring in any
    dynamic dependencies or other variables).

    We now hook into this to add mutation functions for the final
    dictionary. We remove any dynamic dependency configuration and then set
    our final list of dependencies for the package. This is equivalent to
    temporarily replacing the pyproject.toml file itself. This resulting
    list then gets set in the internal Distribution objects for the
    package definition.

    While monkey-patching Setuptools is indeed hacky and could break, we are
    at least pinning the version so there are no surprises. Setuptools
    itself is full of patches like this on top of distutils to control
    behavior.

    Tested this on a package with a pre-set list of static dependencies
    and a package with a list of dynamic dependencies. Built both and
    inspected their resulting wheels for the dependencies lists. Verified
    the expected final set of dependencies were present.

    Summary ID
    Patch setuptools to remove mandatory config
    Buildthings injected dependencies into Setuptools through a bit of an annoying workaround that we inherited from the in-tree build backends for Djblets and Review Board. This required telling Setuptools where to find a `package-requirements.txt` file, which we'd then write to before Setuptools got a chance to run, and that it should look up dependencies dynamically. This change swaps out that hacky workaround for another hacky workaround. Instead of making the `pyproject.toml` file work around this, we now do it inside of buildthings. When setuptools loads `pyproject.toml`, it creates a `_ConfigExpander` object that builds a finalized dictionary of settings (factoring in any dynamic dependencies or other variables). We now hook into this to add mutation functions for the final dictionary. We remove any dynamic dependency configuration and then set our final list of dependencies for the package. This is equivalent to temporarily replacing the `pyproject.toml` file itself. This resulting list then gets set in the internal `Distribution` objects for the package definition. While monkey-patching Setuptools is indeed hacky and could break, we are at least pinning the version so there are no surprises. Setuptools itself is full of patches like this on top of distutils to control behavior.
    6f33cda808411005c4a6dc38b7bb40949e5e5ac1
    Description From Last Updated

    RuntimeError might be more appropriate. We don't want this to get optimized out.

    daviddavid
    chipx86
    david
    1. 
        
    2. buildthings/setuptools_patches.py (Diff revision 2)
       
       
       
      Show all issues

      RuntimeError might be more appropriate. We don't want this to get optimized out.

      1. Only assert (and __debug__) gets optimized out. AssertionError does not. It's safe to explicitly use in optimized code.

        print('hi')
        raise AssertionError('oh no')
        print('hi again')
        

        $ python3 assert-test.py -O
        hi
        Traceback (most recent call last):
          File "/private/tmp/assert-test.py", line 2, in <module>
            raise AssertionError('oh no')
        AssertionError: oh no
        
        $ python3 assert-test.py -OO
        hi
        Traceback (most recent call last):
          File "/private/tmp/assert-test.py", line 2, in <module>
            raise AssertionError('oh no')
        AssertionError: oh no
        

        This is an internal assertion that things are working right, so I think it's the right exception.

    3. 
        
    david
    1. Ship It!
    2. 
        
    maubin
    1. Ship It!
    2. 
        
    chipx86
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to master (391f3cf)