Skip to content

Commit fdd3d52

Browse files
authored
capture errors from cargo metadata (#254)
1 parent 17ae26a commit fdd3d52

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

setuptools_rust/build.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,21 @@ def build_extension(
210210

211211
# Execute cargo
212212
try:
213-
output = subprocess.check_output(command, env=env, encoding="latin-1")
213+
output = subprocess.check_output(
214+
command, env=env, encoding="latin-1", stderr=subprocess.PIPE
215+
)
214216
except subprocess.CalledProcessError as e:
215-
raise CompileError(f"cargo failed with code: {e.returncode}\n{e.output}")
217+
raise CompileError(
218+
f"""
219+
cargo failed with code: {e.returncode}
220+
221+
Output captured from stderr:
222+
{e.stderr}
223+
224+
Output captured from stdout:
225+
{e.stdout}
226+
"""
227+
)
216228

217229
except OSError:
218230
raise DistutilsExecError(

setuptools_rust/extension.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,32 @@ def _metadata(self) -> "_CargoMetadata":
240240
]
241241
if self.cargo_manifest_args:
242242
metadata_command.extend(self.cargo_manifest_args)
243-
self._cargo_metadata = json.loads(subprocess.check_output(metadata_command))
243+
244+
try:
245+
payload = subprocess.check_output(
246+
metadata_command, encoding="latin-1", stderr=subprocess.PIPE
247+
)
248+
except subprocess.CalledProcessError as e:
249+
raise DistutilsSetupError(
250+
f"""
251+
cargo metadata failed with code: {e.returncode}
252+
253+
Output captured from stderr:
254+
{e.stderr}
255+
256+
Output captured from stdout:
257+
{e.stdout}
258+
"""
259+
)
260+
try:
261+
self._cargo_metadata = json.loads(payload)
262+
except json.decoder.JSONDecodeError as e:
263+
raise DistutilsSetupError(
264+
f"""
265+
Error parsing output of cargo metadata as json; received:
266+
{payload}
267+
"""
268+
) from e
244269
return self._cargo_metadata
245270

246271
def _uses_exec_binding(self) -> bool:

0 commit comments

Comments
 (0)