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: provide a better experience for patch_args when pnpm patches are used #837

Closed
wants to merge 1 commit into from

Conversation

kormide
Copy link
Member

@kormide kormide commented Feb 2, 2023

Revisiting this change as it was brought up recently in the Bazel slack group: https://bazelbuild.slack.com/archives/CEZUUKQ6P/p1691179585032929

@kormide kormide requested a review from gregmagolan February 2, 2023 19:45
Comment on lines 433 to 438
# TODO (2.0): Stop defaulting patch_args with {"*": ["-p0"]} when no patches are supplied
# as this conflicts with the default pnpm.patchedDependencies format of "-p1" and
# needs to be explicitly overridden by the user.
if len(patches) == 0:
patch_args = {}

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 figure this isn't a breaking change because it won't affect anyone who doesn't have patches.

@jbedard
Copy link
Member

jbedard commented Feb 2, 2023

Did an issue come up with this or is it just confusing / a bad API now that the pnpm one has a different default?

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

Did an issue come up with this or is it just confusing / a bad API now that the pnpm one has a different default?

I just noticed that I have to override patch_args when I don't have any patches in some client work, and it feels odd.

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

Alternatively, what I could do is have patches ignore the patch_args in npm_import if there are no non-pnpm patches. Patch args only make sense when there are patches since pnpm patches are always p1. Wdyt @gregmagolan ?

@jbedard
Copy link
Member

jbedard commented Feb 2, 2023

Patch args only make sense when there are patches since pnpm patches are always p1

Aren't there other patch args that the pnpm patches might need?

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

Patch args only make sense when there are patches since pnpm patches are always p1

Aren't there other patch args that the pnpm patches might need?

pnpm patches have to be -p1.

@jbedard
Copy link
Member

jbedard commented Feb 2, 2023

Aren't there other arguments though? I thought it was a generic array of arguments and not only the p*?

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

Aren't there other arguments though? I thought it was a generic array of arguments and not only the p*?

None that are defaulted in the macro.

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

Oh I see what you mean @jbedard . I shouldn't remove any non-strip args here. I'll do another pass.

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

Oh I see what you mean @jbedard . I shouldn't remove any non-strip args here. I'll do another pass.

Actually no, if there are no patches then there should be no need to apply any other args to pnpm patches...

@jbedard
Copy link
Member

jbedard commented Feb 2, 2023

But now if patches are added then the default args for pnpm suddenly change. I don't think adding an entry to patches should suddenly change the args to pnpm patches. Or am I reading that wrong?

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

But now if patches are added then the default args for pnpm suddenly change. I don't think adding an entry to patches should suddenly change the args to pnpm patches. Or am I reading that wrong?

Yeah, that's true. I think there might be another way to solve this. Maybe "*" patch_args just shouldn't apply to pnpm patches (at least any strip args in there).

@jbedard
Copy link
Member

jbedard commented Feb 2, 2023

Is the pnpm format a default of "-p1" but it can be changed? Maybe aligning our default with pnpm is best then?

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

Is the pnpm format a default of "-p1" but it can be changed? Maybe aligning our default with pnpm is best then?

Cannot change the format for pnpm patches, and changing our default would be a breaking change.

@jbedard
Copy link
Member

jbedard commented Feb 2, 2023

I guess not applying * to pnpm patches is one option like you said, but that's an odd exception :/

Do we need to apply the patch args to pnpm at all? Normally with pnpm how are patch args applied? Why do we have to add our own patch args?

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

I guess not applying * to pnpm patches is one option like you said, but that's an odd exception :/

Do we need to apply the patch args to pnpm at all? Normally with pnpm how are patch args applied? Why do we have to add our own patch args?

It's just because pnpm patches and patches follow the same implementation path, and patches allows you to be flexible with what arguments are passed.

@jbedard
Copy link
Member

jbedard commented Feb 2, 2023

It's just because pnpm patches and patches follow the same implementation path

So it would be hard to treat pnpm different and ignore the patch args?

@kormide
Copy link
Member Author

kormide commented Feb 2, 2023

It's just because pnpm patches and patches follow the same implementation path

So it would be hard to treat pnpm different and ignore the patch args?

I think it's doable. I'll do a pass when I get back to this.

@kormide kormide marked this pull request as draft February 7, 2023 19:14
@gregmagolan gregmagolan added the wip Work-in-progress; not ready for a final review or for landing label Feb 13, 2023
@kormide kormide changed the title fix: don't default patch_args when using pnpm.patchedDependencies fix: provide a better experience for patch_args when pnpm patches are used Aug 8, 2023
@kormide
Copy link
Member Author

kormide commented Aug 8, 2023

Finally got back to this. I improved the error message when a strip prefix is set for patches that won't work with pnpm.patchedDependencies. I also removed the strip prefix when there are no patches supplied so that the default of -p1 will be used for pnpm patches.

@kormide kormide requested a review from jbedard August 8, 2023 03:50
@kormide kormide marked this pull request as ready for review August 8, 2023 03:50
Comment on lines -14 to -16
patch_args = {
"*": ["-p1"],
},
Copy link
Member Author

@kormide kormide Aug 8, 2023

Choose a reason for hiding this comment

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

The patch arg has to be -p1 for this e2e where pnpm.patchedDependencies is used, so this exclusion exercises the logic where our default of -p0 is overridden.

@kormide kormide added enhancement New feature or request and removed wip Work-in-progress; not ready for a final review or for landing labels Aug 8, 2023
@kormide kormide requested a review from gregmagolan August 8, 2023 04:47
# requirement of "-p1" for patches. When pnpm patches are used, it's highly likely that
# `patches` is unset, so when it is empty remove any strip prefixes from the default or
# any user-supplied argument as they won't have an effect anyway.
if len(patches) == 0 and patch_args:
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible and any nicer changing the default to patch_args = None and adding the default when there are patches instead of clearing it when no patches? Mainly to avoid the extra strip method...

def npm_translate_lock(
        name,
        ....
        patches = {},
        patch_args = None,

...

    if len(patches) > 0 and patch_args == None:
       patch_args = {"*": ["-p0"]}

Copy link
Member Author

Choose a reason for hiding this comment

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

My intention to perform the substitution whether patch_args is defaulted or the user supplies a value. As long as patches is empty, the strip prefix should be irrelevant because we always override the pnpm patches with -p1.

Copy link
Member

Choose a reason for hiding this comment

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

Why would patch_args be set if patches is empty though?

Copy link
Member Author

Choose a reason for hiding this comment

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

It could be. Technically someone could set non-strip args (I'm not sure what those are) and they would apply to the pnpm patches.

Copy link
Member

Choose a reason for hiding this comment

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

Then they'd be overriding the None default and we wouldn't have to do anything though?

Copy link
Member

Choose a reason for hiding this comment

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

That's what we do currently and it causes issues with the default -p0 when using pnpm patches which prefer the -p1

Copy link
Member

Choose a reason for hiding this comment

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

I mean change how our defaults work. Different defaults depending if we are using patches or assuming we're using pnpm (because patches is empty). Different defaults in these 2 cases instead of modifying/converting the args in 1/2 cases.

If that makes sense?

Copy link
Member

Choose a reason for hiding this comment

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

It's hard to document in that case since stardoc wouldn't show the default.

I think it is cleaner to document that -p1 is forced for any patches that are listed in the pnpm package.json patches and both the default and any user supplied patch_args for -p or --strip are ignored in that case.

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 think of a way of doing that without the current _remove_strip_prefix_arg() implementation though?

Scanning args for various -p* or --strip=* names and modifying the args is just something I'd prefer avoiding, especially with almost no test coverage :/

Copy link
Member

Choose a reason for hiding this comment

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

I don't have any better ideas besides that. Maybe just appending -p1 is sufficient if patch lets the last strip argument win but we'd have to test that.

@jbedard
Copy link
Member

jbedard commented May 9, 2024

Done with #1694

@jbedard jbedard closed this May 9, 2024
gregmagolan pushed a commit that referenced this pull request May 10, 2024
gregmagolan pushed a commit that referenced this pull request May 10, 2024
gregmagolan pushed a commit that referenced this pull request May 13, 2024
jbedard added a commit to jbedard/rules_js that referenced this pull request May 14, 2024
jbedard added a commit that referenced this pull request May 14, 2024
jbedard added a commit to jbedard/rules_js that referenced this pull request May 16, 2024
gregmagolan pushed a commit that referenced this pull request May 20, 2024
gregmagolan pushed a commit that referenced this pull request May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants