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

feat: pass enviroment variables during pypi resolution and install #818

Merged

Conversation

nichmor
Copy link
Contributor

@nichmor nichmor commented Feb 19, 2024

No description provided.

lock_file_usage: LockFileUsage,
) -> miette::Result<HashMap<String, String>> {
// Get the prefix which we can then activate.
get_up_to_date_prefix(environment, lock_file_usage, false, IndexMap::default()).await?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't something like this already done as a prerequisite for the pypi resolve?

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 the old part of existing get_activation_env. I've extracted the part what I need in get_env_and_activation_variables, so I could call it without causing infinite recursion

Copy link
Contributor

@ruben-arts ruben-arts left a comment

Choose a reason for hiding this comment

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

Awesome! Nice work on getting an actual test package for this! And good idea of using the arc and lock. My experience with those is limited @tdejager or @baszalmstra could you take another quick look?

@@ -35,6 +42,9 @@ pub struct Environment<'p> {

/// The environment that this environment is based on.
pub(super) environment: &'p manifest::Environment,

/// The cache that contains environment variables
vars: Arc<Mutex<HashMap<String, String>>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

This variable might be lost if someone requests the environment again from the project instead of cloning this type. It might be better to store a hashmap in the project, indexed by environment name.

pub async fn get_env_variables(&self) -> miette::Result<HashMap<String, String>> {
let mut locked = self.vars.lock().await;

if !locked.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think its never good practice (and very pythonic) to depend on something being empty as meaning that it wasnt set. Its better to wrap these things in an Option to provide more semantics to the type.

Additionally for this usecase you could use an async_once_cell.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree!

Comment on lines +59 to +62
self.solve_group.environments.iter().map(|env_idx| {
Environment::new(
self.project,
&self.project.manifest.parsed.environments.environments[*env_idx],
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like the [] indexing too much because it can panic, I feel its better to handle this explicitly, by varing what applies in this case

  • Panicking with a better error message, if it not being there is a program error
  • Returning a Result
  • Filtering out the environments that do not match.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could you please suggest what would be the best scenario in this case?
I just reformatted this code so I'm unaware of solve_groups matching behaviour when environment is missing.
Should I throw an miette: error ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I also don't know this part well enough let's ask @baszalmstra

Copy link
Contributor

Choose a reason for hiding this comment

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

I would leave as is. If this code panics than some other thing is very wrong.

@tdejager
Copy link
Contributor

Other than the comments, look good!

@nichmor
Copy link
Contributor Author

nichmor commented Feb 23, 2024

Thanks for the review!

@ruben-arts ruben-arts merged commit 37882ae into prefix-dev:main Feb 23, 2024
14 checks passed
@zachcp
Copy link

zachcp commented Mar 21, 2024

Hi, this looks great is there an example on how to pass ENV variables?

@ruben-arts
Copy link
Contributor

He @zachcp, this pr shows an example, you need to add a activation script, e.g. activation.sh or activation.bat for windows. And then those variables will also be send to the pypi sdist building.

@tdejager
Copy link
Contributor

Also: https://github.com/prefix-dev/pixi/tree/main/examples/pypi shows it on the main branch.

@zachcp
Copy link

zachcp commented Mar 22, 2024

Thank you, both. I was trying to use this mechanism to set the UV_INDEX_URL env but it didn't work....

export UV_INDEX_URL=https://<my_corporate_pypi>//pypi/simple

Nonetheless - this could be very handy.

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.

5 participants