patch-package icon indicating copy to clipboard operation
patch-package copied to clipboard

Patch is unrecognized if it is for a version prefixed with a 'v'

Open et304383 opened this issue 3 years ago • 5 comments

This package happily creates a diff patch for a package that has a version starting with a 'v' but then fails to apply that patch, throwing the following warning:

Unrecognized patch file in patches directory serverless-plugin-aws-alerts+v1.7.3.patch

If I edit PackageDetails.js to drop the 'v' from the version, it somehow just works:

function parseNameAndVersion(s) {
    const parts = s.split("+");
    parts[1] = '1.7.3'
    switch (parts.length) {

The logic in parseNameAndVersion seems odd to me as you are returning the package name as name/version if the version doesn't match a regex of digits:

return { name: `${nameOrScope}/${versionOrName}` };

Yet, in getPackageDetailsFromPatchFilename you explicitly expect that you will have a version key in a map after returning from parseNameAndVersion:

    const lastPart = parts[parts.length - 1];
    if (!lastPart.version) {
        return null;
    }

Seems you assume that if versionOrName isn't a digit then the parts must correspond to scope/name, which is false in this case.

In my case, the obvious work-around that somehow works is to drop the 'v' from the patch name, but this isn't obvious to anyone who expects the plugin to just work out of the box. :)

et304383 avatar Jul 15 '21 12:07 et304383

This should be fixed as it effectively breaks the entire purpose.

xblabs avatar Sep 09 '22 16:09 xblabs

Oh my, you just saved my whole works 🙏. I don't understand why this hasn't been fixed yet.

emreastarcioglu avatar Nov 22 '23 20:11 emreastarcioglu

really, why this is not fixed yet?

nchorniy avatar Nov 23 '23 13:11 nchorniy

I managed to work around the issue I was going to patch a completely different way so I haven't tested this let alone put up a PR -- but skimming the code, this regex here:

https://github.com/ds300/patch-package/blob/c7c63bf80b3c6b8640b933e20229121b4edfc100/src/PackageDetails.ts#L39

Does it fix to change that to

part.match(/^v?\d+\.\d+\.\d+.*$/)

That's probably not a "great" fix -- assuming versions have a format at all is maybe not a good idea and the code should maybe be refactored? -- but might help with the immediate issue here?

jwatzman avatar Jan 11 '24 11:01 jwatzman

I can confirm, running a command like npx patch-package @pulumi/aws produces a file called @pulumi+aws+v5.13.0.patch, which results in Unrecognized patch file in patches directory @pulumi+aws+v5.13.0.patch After renaming the file to @pulumi+aws+5.13.0.patch (removing the v), the problem is gone.

IjzerenHein avatar Mar 11 '24 09:03 IjzerenHein