Skip to content

Commit

Permalink
fix: build fails when use_uv is true (#3237)
Browse files Browse the repository at this point in the history
* fix: build fails when use_uv is true(#3231)

* fix: delete uv build .gitignore so that publish could work

* fix: make uv build compatible to --no-clean

* feat: uv build enables --quiet

* fix: resolve #3237 conversation

* fix: build test failed

---------

Co-authored-by: c.men <[email protected]>
  • Loading branch information
monchin and c.men authored Oct 31, 2024
1 parent b3d162f commit 8f035b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
1 change: 1 addition & 0 deletions news/3231.bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the bug that `pdm build` would fail when `use_uv` is true.
74 changes: 52 additions & 22 deletions src/pdm/cli/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import tarfile
import tempfile
from pathlib import Path
from typing import Mapping

from pdm.cli.commands.base import BaseCommand
Expand Down Expand Up @@ -32,6 +33,7 @@ def do_build(
wheel: bool = True,
dest: str = "dist",
clean: bool = True,
verbose: int = 0,
config_settings: Mapping[str, str] | None = None,
hooks: HookManager | None = None,
) -> None:
Expand All @@ -57,29 +59,56 @@ def do_build(
hooks.try_emit("pre_build", dest=dest, config_settings=config_settings)
artifacts: list[str] = []
with project.core.ui.logging("build"):
if sdist:
project.core.ui.echo("[info]Building sdist...")
sdist_file = SdistBuilder(project.root, project.environment).build(dest)
project.core.ui.echo(f"[info]Built sdist at {sdist_file}")
artifacts.append(sdist_file)
if wheel:
if not project.config["use_uv"]:
if sdist:
project.core.ui.echo("[info]Building wheel from sdist...")
sdist_out = tempfile.mkdtemp(prefix="pdm-build-via-sdist-")
try:
with tarfile.open(sdist_file, "r:gz") as tf:
tf.extractall(sdist_out)
sdist_name = os.path.basename(sdist_file)[: -len(".tar.gz")]
whl = WheelBuilder(os.path.join(sdist_out, sdist_name), project.environment).build(dest)
project.core.ui.echo(f"[info]Built wheel at {whl}")
artifacts.append(whl)
finally:
shutil.rmtree(sdist_out, ignore_errors=True)
else:
project.core.ui.echo("[info]Building wheel...")
whl = WheelBuilder(project.root, project.environment).build(dest)
project.core.ui.echo(f"[info]Built wheel at {whl}")
artifacts.append(whl)
project.core.ui.echo("[info]Building sdist...")
sdist_file = SdistBuilder(project.root, project.environment).build(dest)
project.core.ui.echo(f"[info]Built sdist at {sdist_file}")
artifacts.append(sdist_file)
if wheel:
if sdist:
project.core.ui.echo("[info]Building wheel from sdist...")
sdist_out = tempfile.mkdtemp(prefix="pdm-build-via-sdist-")
try:
with tarfile.open(sdist_file, "r:gz") as tf:
tf.extractall(sdist_out)
sdist_name = os.path.basename(sdist_file)[: -len(".tar.gz")]
whl = WheelBuilder(os.path.join(sdist_out, sdist_name), project.environment).build(dest)
project.core.ui.echo(f"[info]Built wheel at {whl}")
artifacts.append(whl)
finally:
shutil.rmtree(sdist_out, ignore_errors=True)
else:
project.core.ui.echo("[info]Building wheel...")
whl = WheelBuilder(project.root, project.environment).build(dest)
project.core.ui.echo(f"[info]Built wheel at {whl}")
artifacts.append(whl)
else:
import subprocess

dest_dir = Path(dest).absolute()

uv_build_cmd = [*project.core.uv_cmd, "build", "--out-dir", str(dest_dir)]
if verbose == -1:
uv_build_cmd.append("-q")
elif verbose > 0:
uv_build_cmd.append(f"-{'v'*verbose}")
subprocess.run(uv_build_cmd, check=True)

# pdm build doesn't include .gitignore, and pdm publish would fail with .gitignore
(dest_dir / ".gitignore").unlink(missing_ok=True)
for sdist_fp in dest_dir.glob("*.tar.gz"):
if sdist is False:
sdist_fp.unlink(missing_ok=True)
else:
artifacts.append(str(sdist_fp))

for whl_file in dest_dir.glob("*.whl"):
if wheel is False:
whl_file.unlink(missing_ok=True)
else:
artifacts.append(str(whl_file))

hooks.try_emit("post_build", artifacts=artifacts, config_settings=config_settings)

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
Expand Down Expand Up @@ -113,5 +142,6 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
wheel=options.wheel,
dest=options.dest,
clean=options.clean,
verbose=options.verbose,
hooks=HookManager(project, options.skip),
)
1 change: 1 addition & 0 deletions tests/cli/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_build_command(project, pdm, mocker):
wheel=True,
dest=mock.ANY,
clean=True,
verbose=0,
hooks=mock.ANY,
)
assert project.core.state.config_settings == {"a": "1", "b": "2"}
Expand Down

0 comments on commit 8f035b1

Please sign in to comment.