Skip to content

Add uv run command #3074

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

Merged
merged 1 commit into from
Apr 17, 2024
Merged

Add uv run command #3074

merged 1 commit into from
Apr 17, 2024

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Apr 16, 2024

Adds uv run which executes a command in your current virtual environment.

This is a simple first milestone, lots of remaining work and behavior. The command is hidden.

@zanieb zanieb added internal A refactor or improvement that is not user-facing cli Related to the command line interface labels Apr 16, 2024
@zanieb zanieb marked this pull request as ready for review April 16, 2024 23:15
#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct RunArgs {
/// Command
Copy link
Member

Choose a reason for hiding this comment

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

This needs some explanation what command means and some examples, esp. compared to

usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to reserve the real documentation for when it's a little more nailed down what the interface is. I think I extend these a bit in the next pull request though.

// Spawn and wait for completion
// Standard input, output, and error streams are all inherited
// TODO(zanieb): Throw a nicer error message if the command is not found
let mut handle = process.spawn()?;
Copy link
Member

Choose a reason for hiding this comment

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

Can you use .status() here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I could switch yeah, I thought I might need to do something while it's running but perhaps not?

let status = handle.wait().await?;

// Exit based on the result of the command
// TODO(zanieb): Do we want to exit with the code of the child process? Probably.
Copy link
Member

Choose a reason for hiding this comment

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

This is important for scripts that check the output status of the python code (e.g. exit status 1 vs exit status 2)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah agree that's just not built into our exit system so I want to do it separately

@konstin
Copy link
Member

konstin commented Apr 17, 2024

Some things i learned i'm gonna dump here:

You can load dlopen libpython and use the c api to load libpython, and run Py_Main. This is more efficient and has a better integration than a subprocess, but it requires that uv and python have the same libc, which we can't assume.

Again on unix, you use execv to have the current process been taken over by a new one (instead of nested and handling subprocess status). This is apparently not supported on windows, where you always spawn a new process (https://github.com/konstin/poc-monotrail/blob/810f7e1ff43c1f95c96d318691241225352357d1/crates/monotrail/src/monotrail.rs#L759-L794)

@zanieb
Copy link
Member Author

zanieb commented Apr 17, 2024

Thanks for the notes! Do you see a strong benefit to doing execv I feel like we might want to be able to take action after the command? I'll include a note about that somewhere regardless as it does seem kind of nice.

@konstin
Copy link
Member

konstin commented Apr 17, 2024

Not really, execv just seemed more elegant

@zanieb zanieb merged commit f7b83e9 into main Apr 17, 2024
38 checks passed
@zanieb zanieb deleted the zb/uv-run branch April 17, 2024 16:20
@zanieb zanieb mentioned this pull request May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command line interface internal A refactor or improvement that is not user-facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants