Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capture errors from cargo metadata #254

Merged
merged 3 commits into from
Jun 28, 2022
Merged

capture errors from cargo metadata #254

merged 3 commits into from
Jun 28, 2022

Conversation

h-vetinari
Copy link
Contributor

Should fix #253.

The setup is copied largely from what's already in build.py, but using DistutilsSetupError instead of CompileError (in line with the rest of the error reporting in extension.py). Also, I don't think duplicating the _prepare_build_environment is necessary just for cargo metadata...?

@messense
Copy link
Member

Thanks for the PR, please format code with black.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Bit of feedback...

env = os.environ.copy()
try:
payload = subprocess.check_output(
metadata_command, env=env, encoding="latin-1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is to inherit the current environment? I don't think env=env is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I just wanted to make sure env definitely is present, but will remove.

metadata_command, env=env, encoding="latin-1"
)
except subprocess.CalledProcessError as e:
msg = f"cargo failed with code: {e.returncode}\n{e.output}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also include stderr? Also it would be nice to say cargo metadata instead of just cargo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I wrote, I copied the setup from build.py. Happy to add e.stderr here (and also in build.py if desired)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made these adaptations now.

@h-vetinari
Copy link
Contributor Author

Thanks for the quick review!

@h-vetinari h-vetinari changed the title capture errors from cargo metadata and pass in env capture errors from cargo metadata Jun 27, 2022
self._cargo_metadata = json.loads(subprocess.check_output(metadata_command))

try:
payload = subprocess.check_output(metadata_command, encoding="latin-1")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'm really sorry to have missed on the first read, I think we might need to opt-in to capturing stderr:

Suggested change
payload = subprocess.check_output(metadata_command, encoding="latin-1")
payload = subprocess.check_output(metadata_command, encoding="latin-1", stderr=subprocess.PIPE)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(could also use stderr=subprocess.STDOUT to merge the two together so that the error could just use e.stdout, I think both merged and separate have advantages and disadvantages so happy to go with whichever you prefer)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks!

I preferred the separate versions (no reliance that merge works correctly/legibly), but happy to change if desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, never mind, the merging is apparently the recommended (or at least documented way): https://docs.python.org/3/library/subprocess.html#subprocess.check_output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an added benefit, this makes the exception message (& diff) much smaller. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the merge was what's causing the json errors... It adds the logs into the output, making the json unparseable, e.g.:

Error parsing output of cargo metadata as json; received:
                          b'    Updating crates.io index\n Downloading crates ...\n  Downloaded unindent v0.1.8\n  Downloaded target-lexicon v0.12.3\n  Downloaded pyo3-macros v0.16.5\n  Downloaded cfg-if v1.0.0\n  Downloaded scopeguard v1.1.0\n  Downloaded unicode-xid v0.2.2\n  Downloaded proc-macro2 v1.0.36\n  Downloaded indoc v1.0.4\n  Downloaded instant v0.1.12\n  Downloaded once_cell v1.10.0\n  Downloaded lock_api v0.4.6\n  Downloaded parking_lot v0.11.2\n  Downloaded bitflags v1.3.2\n  Downloaded pyo3-ffi v0.16.5\n  Downloaded quote v1.0.16\n  Downloaded syn v1.0.89\n  Downloaded pyo3-macros-backend v0.16.5\n  Downloaded smallvec v1.8.0\n  Downloaded redox_syscall v0.2.11\n  Downloaded parking_lot_core v0.8.5\n  Downloaded pyo3 v0.16.5\n  Downloaded winapi v0.3.9\n  Downloaded libc v0.2.121\n  Downloaded pyo3-build-config v0.16.5\n  Downloaded winapi-x86_64-pc-windows-gnu v0.4.0\n  Downloaded winapi-i686-pc-windows-gnu v0.4.0\n{"packages":[{"name":"bitflags",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see!

Now that I realise that these updating / downloading messages come through stderr - it's probably quite nice to get them as output by default (i.e. not capture stderr)?

Could maybe check the --quiet flag - if set, capture and include stderr in the exception message, otherwise just don't capture stderr? What do you think of that?

I think we're pretty much there now - thanks for the patience / iteration on this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back to trying the non-merged version for now. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking better now. :)

@h-vetinari
Copy link
Contributor Author

h-vetinari commented Jun 28, 2022

I struggle to understand how any of the recent commits could make the json parsing fail (and the CI passed for b6752c8...)

@davidhewitt
Copy link
Member

Looks fine to me, thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Call to cargo metadata doesn't check for errors
3 participants