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

fix: omit also jinja functions #37

Merged
merged 13 commits into from
Aug 1, 2024

Conversation

nichmor
Copy link
Collaborator

@nichmor nichmor commented Jul 31, 2024

We are missing the use-case when jinja function is used in template - it will raise an Exception.
This PR aims to fix it.

@wolfv
Copy link
Member

wolfv commented Jul 31, 2024

Hmm, I think in conda-smithy and friends they have some "shim" functions for this. Do you think we should implement the necessary jinja logic?

@nichmor
Copy link
Collaborator Author

nichmor commented Jul 31, 2024

Hmm, I think in conda-smithy and friends they have some "shim" functions for this. Do you think we should implement the necessary jinja logic?

what do you mean by shims?

"split": split_filter,
}
)
return env
Copy link
Member

Choose a reason for hiding this comment

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

Do we also need to add some stub functions for env.get and env.get_default?

Copy link
Collaborator

Choose a reason for hiding this comment

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

All the more reason tho have an actual recipe that contains some of these corner cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that's true , added mamba_recipe as a test one

Copy link
Collaborator

@baszalmstra baszalmstra left a comment

Choose a reason for hiding this comment

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

For laughs and giggles you could also add an actual recipe to test this on.

Comment on lines 37 to 45
def version_to_build_string(some_string: str) -> str:
"""Converts some version by removing the . character and returning only the first two elements of the version)"""
# We first split the string by whitespace and take the first part
split = some_string.split()[0] if some_string.split() else some_string
# We then split the string by . and take the first two parts
parts = split.split(".")
major = parts[0] if len(parts) > 0 else ""
minor = parts[1] if len(parts) > 1 else ""
return f"{major}{minor}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can imagine that it would be useful to add a test for this implementation too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added!

"split": split_filter,
}
)
return env
Copy link
Collaborator

Choose a reason for hiding this comment

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

All the more reason tho have an actual recipe that contains some of these corner cases.

@baszalmstra
Copy link
Collaborator

This is a list of all jinja functionality supported in the recipe: conda/ceps#71 (@wolfv we should merge this)

Do all of these work as expected now?

@nichmor
Copy link
Collaborator Author

nichmor commented Aug 1, 2024

Updated the missing filters and added env object. Also added mamba recipe to test that we really don't fail

src/rattler_build_conda_compat/jinja/objects.py Outdated Show resolved Hide resolved
src/rattler_build_conda_compat/jinja/objects.py Outdated Show resolved Hide resolved
from jinja2 import DebugUndefined


class _MissingUndefined(DebugUndefined):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cant this error be returned by the functions to the user? In that case I think this thing shouldnt be private.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should in that case also either not put it in the utils module or reexport it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This error is not returned to the user ( jinja is raising directly jinja2.Undefined ) and it's something specific for our error handling - that's why I would prefer to keep private

outputs:
- build:
script:
- ''
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems wrong it should be

- ${{ "build_mamba.sh" if unix }}

about:
description: '# Mamba, the Fast Cross-Platform Package Manager

${{ env.get("MY_ENV_VAR")}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the escaping correct here?

Copy link
Collaborator Author

@nichmor nichmor Aug 1, 2024

Choose a reason for hiding this comment

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

it is quotation mark ( this one: " ) , I'm not exactly sure why syrupy keep it like this in snapshots

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed by asking jinja to not escape it :)

@nichmor
Copy link
Collaborator Author

nichmor commented Aug 1, 2024

@baszalmstra @wolfv I'm setting similar shims as: https://github.com/wolfv/conda-smithy/blob/77cd535868932b088b3db7051cd1640655511343/conda_smithy/utils.py#L110

basically this means that we will render unix part and skip windows

- ${{ "build_mamba.sh" if unix }}
 - ${{ "build_mamba.bat" if win }}

I couldn't find an easier way to preserve them ( chatgpt said that I need to write my own node parser, not sure if we need it right now )

Let me know if it's ok

Copy link
Member

@wolfv wolfv left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few questions :)

if default:
return f"""${{{{ env.get("{env_var}", default="{default}") }}}}"""

return f"""${{{{ env.get("{env_var}")}}}}"""
Copy link
Member

Choose a reason for hiding this comment

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

If I understand correctly, this just returns the jinja string as is? Fine with me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

made them all to return just the simple string



def _stub_subpackage_pin(*args, **kwargs) -> str: # noqa: ARG001, ANN003, ANN002
return f"subpackage_pin {args[0]}"
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure I understand the difference. Here you return simple string, but in other functions you return ${{ .... }}.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

made them all to return just the simple string

env.filters.update(
{
"version_to_buildstring": _version_to_build_string,
"split": _split,
Copy link
Member

Choose a reason for hiding this comment

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

I actually don't know if this is implemented in Python Jinja or not. Is it missing from Python jinja?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was missing from python jinja

@nichmor
Copy link
Collaborator Author

nichmor commented Aug 1, 2024

@wolfv made the stubs behave the same

@nichmor nichmor requested a review from wolfv August 1, 2024 10:00
@wolfv wolfv merged commit 5c61261 into prefix-dev:main Aug 1, 2024
7 checks passed
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.

3 participants