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

Not able to include package.json in the patch #49

Closed
davidlampon opened this issue Apr 20, 2018 · 10 comments

Comments

Projects
6 participants
@davidlampon
Copy link

commented Apr 20, 2018

I was commenting on here as we are (for some unknown reason so far) not able to download the latest beta version which seem to fix the issue about the error after applying a patch.

@ds300 let me explain out situation so maybe you can point the way to go. We are having this issue: a third party package we are using has not properly set the folder directories to point to the /lib and /es folders.

We opened this PR for them to merge but even if that happens then the other package we want to use rc-slider. So in total this means waiting on two external PRs.

As a workaround we wanted to modify or local packages, patch them, to allow this changes which already make our build succesful but we are not able to include the package.jsonfile. We tried yarn patch-package rc-util --use-yarn --exclude /$.^/ as a void regexp to reenable the disbales package.json tracking for the patch but with no success.

As another workaround we modified the /es files to patch the package but the patch-package is presenting the whitespace error This is usually caused by inconsistent whitespace in the patch file.

TL,DR: What would be the way to include package.jsonin the patch?

@ds300

This comment has been minimized.

Copy link
Owner

commented Apr 20, 2018

Hi! 👋 Thanks for the thorough explanation :-)

The first thing I noticed is that you're using JS-style regexp literals with the --exclude override. Instead you should use regular strings. See the examples in include-exclude-paths.sh – particularly this one which uses interesting regexp syntax. Imagine you were passing the string to new RegExp(...)

Thanks for bringing this issue to my attention. I'll try to improve that section of the readme.

@dawnmist

This comment has been minimized.

Copy link

commented Apr 24, 2018

If you can add an example for disabling the default exclude, that'd be fantastic.

The other time that being able to patch package.json is desirable is when adding typescript type definitions to a package that doesn't have its own type definitions, so that you can add the "types" field to the package's package.json file pointing to your new type definition file.

Edit: It doesn't seem to matter what the exclude regex is, package.json is hardcoded to be excluded in the diff inside makePatch at:

// don't commit package.json though
fs.unlinkSync(
path.join(tmpRepo.name, "node_modules", packageName, "package.json"),
)

and:
// remove package.json again
fs.unlinkSync(
path.join(tmpRepo.name, "node_modules", packageName, "package.json"),
)

At present, the only way to be able to include package.json in the diff is to first patch patch-package to comment out those two lines in the compiled code. There's a bit of humour there in using patch-package to patch patch-package itself...

Patch: patch-package+5.1.1.patch - to allow enabling patching of package.json if you also use a regex that doesn't include it (e.g. --exclude '\*.txt').

patch-package
--- a/node_modules/patch-package/dist/makePatch.js
+++ b/node_modules/patch-package/dist/makePatch.js
@@ -66,14 +66,14 @@ function makePatch(packageName, appPath, packageManager, includePaths, excludePa
         fs.writeFileSync(path.join(tmpRepo.name, ".gitignore"), "!/node_modules\n\n");
         tmpExec_1("git", ["init"]);
         // don't commit package.json though
-        fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
+        // fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
         tmpExec_1("git", ["add", "-f", slash(path.join("node_modules", packageName))]);
         tmpExec_1("git", ["commit", "-m", "init"]);
         // replace package with user's version
         rimraf.sync(tmpRepoPackagePath);
         fsExtra.copySync(packagePath, tmpRepoPackagePath, { recursive: true });
         // remove package.json again
-        fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
+        // fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
         // stage all files
         tmpExec_1("git", ["add", "-f", slash(path.join("node_modules", packageName))]);
         // unstage any ignored files so they don't show up in the diff
@ds300

This comment has been minimized.

Copy link
Owner

commented Apr 24, 2018

Oh yes, you're right! Really sorry about the mixup. I think those lines should have been removed when I added the --include and --exclude options.

@ds300 ds300 added the bug label Apr 24, 2018

ds300 added a commit that referenced this issue Apr 24, 2018

@ds300

This comment has been minimized.

Copy link
Owner

commented Apr 24, 2018

Just published a fix for this in patch-package@6.0.0-4 – Thanks for pointing this out @dawnmist and @davidlampon 🙇 ❤️

I'll close this once v6 leaves beta.

@brunolemos

This comment has been minimized.

Copy link

commented May 23, 2018

I'm using 6.0.0-7 and I'm not able to patch a package.json from another package, how can I do that?

EDIT: Adding a --exclude '\*.txt' made it work.

I removed a package from a third party package.json but when I run yarn it gets added to yarn.lock anyway :( It makes sense, as it run a postinstall script.

Tried this but did not work: "preinstall": "patch-package --include 'package\\.json$'"

@ds300

This comment has been minimized.

Copy link
Owner

commented May 23, 2018

Hi @brunolemos 👋 !

Unfortunately what you want to do is not possible 😢 This is a common question about patch-package. I don't know of a way to get absolute control over which transient dependencies get installed. The best thing I've found is manually editing your own lock file, but that's only useful for making sure a specific version gets installed.

@ivikash

This comment has been minimized.

Copy link

commented Oct 18, 2018

I am trying to patch a dependency of a dependency in the package.json. It does generate the patch file with a little noise (using npm) but is unable to apply the patches -> **ERROR** Failed to apply patch for package somePackage. Can you include a short summary or documentation on the usage, like how to generate the patch file and especially what should be done in prepare step. I am currently using 6.0.0-v5 due to some constraints.

@ivikash

This comment has been minimized.

Copy link

commented Oct 23, 2018

@ds300 Any update on this? Also, as I am using npm so should the patch-package be applied on prepare or postinstall? While trying with preinstall its throwing an error saying patch-package is not found.

@danieldunderfelt

This comment has been minimized.

Copy link

commented Nov 19, 2018

@ds300 so when will v6 leave beta? At least the beta is installable with npm, so it's not a huge issue, but people who are looking to patch package.json need to be aware about the beta and actively install it.

And yes, the beta works very well, so far as this issue is concerned!

@ds300 ds300 added this to To do in 6.0.0 release Jan 13, 2019

@ds300 ds300 moved this from To do to In progress in 6.0.0 release Jan 20, 2019

@ds300 ds300 moved this from In progress to Published in 6.0.0 release Jan 20, 2019

@ds300 ds300 moved this from Merged to Published in 6.0.0 release Jan 20, 2019

@ds300

This comment has been minimized.

Copy link
Owner

commented Jan 20, 2019

Closing this to tidy up, final 6.0.0 release will happen soon 🎉

@ds300 ds300 closed this Jan 20, 2019

6.0.0 release automation moved this from Published to Merged Jan 20, 2019

@ds300 ds300 moved this from Merged to Published in 6.0.0 release Jan 27, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.