WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 8f035b1

Browse files
monchinc.men
andauthored
fix: build fails when use_uv is true (#3237)
* 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]>
1 parent b3d162f commit 8f035b1

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

news/3231.bug.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the bug that `pdm build` would fail when `use_uv` is true.

src/pdm/cli/commands/build.py

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import tarfile
77
import tempfile
8+
from pathlib import Path
89
from typing import Mapping
910

1011
from pdm.cli.commands.base import BaseCommand
@@ -32,6 +33,7 @@ def do_build(
3233
wheel: bool = True,
3334
dest: str = "dist",
3435
clean: bool = True,
36+
verbose: int = 0,
3537
config_settings: Mapping[str, str] | None = None,
3638
hooks: HookManager | None = None,
3739
) -> None:
@@ -57,29 +59,56 @@ def do_build(
5759
hooks.try_emit("pre_build", dest=dest, config_settings=config_settings)
5860
artifacts: list[str] = []
5961
with project.core.ui.logging("build"):
60-
if sdist:
61-
project.core.ui.echo("[info]Building sdist...")
62-
sdist_file = SdistBuilder(project.root, project.environment).build(dest)
63-
project.core.ui.echo(f"[info]Built sdist at {sdist_file}")
64-
artifacts.append(sdist_file)
65-
if wheel:
62+
if not project.config["use_uv"]:
6663
if sdist:
67-
project.core.ui.echo("[info]Building wheel from sdist...")
68-
sdist_out = tempfile.mkdtemp(prefix="pdm-build-via-sdist-")
69-
try:
70-
with tarfile.open(sdist_file, "r:gz") as tf:
71-
tf.extractall(sdist_out)
72-
sdist_name = os.path.basename(sdist_file)[: -len(".tar.gz")]
73-
whl = WheelBuilder(os.path.join(sdist_out, sdist_name), project.environment).build(dest)
74-
project.core.ui.echo(f"[info]Built wheel at {whl}")
75-
artifacts.append(whl)
76-
finally:
77-
shutil.rmtree(sdist_out, ignore_errors=True)
78-
else:
79-
project.core.ui.echo("[info]Building wheel...")
80-
whl = WheelBuilder(project.root, project.environment).build(dest)
81-
project.core.ui.echo(f"[info]Built wheel at {whl}")
82-
artifacts.append(whl)
64+
project.core.ui.echo("[info]Building sdist...")
65+
sdist_file = SdistBuilder(project.root, project.environment).build(dest)
66+
project.core.ui.echo(f"[info]Built sdist at {sdist_file}")
67+
artifacts.append(sdist_file)
68+
if wheel:
69+
if sdist:
70+
project.core.ui.echo("[info]Building wheel from sdist...")
71+
sdist_out = tempfile.mkdtemp(prefix="pdm-build-via-sdist-")
72+
try:
73+
with tarfile.open(sdist_file, "r:gz") as tf:
74+
tf.extractall(sdist_out)
75+
sdist_name = os.path.basename(sdist_file)[: -len(".tar.gz")]
76+
whl = WheelBuilder(os.path.join(sdist_out, sdist_name), project.environment).build(dest)
77+
project.core.ui.echo(f"[info]Built wheel at {whl}")
78+
artifacts.append(whl)
79+
finally:
80+
shutil.rmtree(sdist_out, ignore_errors=True)
81+
else:
82+
project.core.ui.echo("[info]Building wheel...")
83+
whl = WheelBuilder(project.root, project.environment).build(dest)
84+
project.core.ui.echo(f"[info]Built wheel at {whl}")
85+
artifacts.append(whl)
86+
else:
87+
import subprocess
88+
89+
dest_dir = Path(dest).absolute()
90+
91+
uv_build_cmd = [*project.core.uv_cmd, "build", "--out-dir", str(dest_dir)]
92+
if verbose == -1:
93+
uv_build_cmd.append("-q")
94+
elif verbose > 0:
95+
uv_build_cmd.append(f"-{'v'*verbose}")
96+
subprocess.run(uv_build_cmd, check=True)
97+
98+
# pdm build doesn't include .gitignore, and pdm publish would fail with .gitignore
99+
(dest_dir / ".gitignore").unlink(missing_ok=True)
100+
for sdist_fp in dest_dir.glob("*.tar.gz"):
101+
if sdist is False:
102+
sdist_fp.unlink(missing_ok=True)
103+
else:
104+
artifacts.append(str(sdist_fp))
105+
106+
for whl_file in dest_dir.glob("*.whl"):
107+
if wheel is False:
108+
whl_file.unlink(missing_ok=True)
109+
else:
110+
artifacts.append(str(whl_file))
111+
83112
hooks.try_emit("post_build", artifacts=artifacts, config_settings=config_settings)
84113

85114
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
@@ -113,5 +142,6 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
113142
wheel=options.wheel,
114143
dest=options.dest,
115144
clean=options.clean,
145+
verbose=options.verbose,
116146
hooks=HookManager(project, options.skip),
117147
)

tests/cli/test_build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_build_command(project, pdm, mocker):
3030
wheel=True,
3131
dest=mock.ANY,
3232
clean=True,
33+
verbose=0,
3334
hooks=mock.ANY,
3435
)
3536
assert project.core.state.config_settings == {"a": "1", "b": "2"}

0 commit comments

Comments
 (0)