You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "aidant (via GitHub)" <gi...@apache.org> on 2024/12/16 23:02:31 UTC
[PR] Keep final new line in package.json [cordova-lib]
aidant opened a new pull request, #941:
URL: https://github.com/apache/cordova-lib/pull/941
### Platforms affected
### Motivation and Context
Cordova removes the final new line in a package.json when running cordova commands. This causes us a problem since we have a formatting rule for final new lines.
### Description
Apply the same writing rules every time the package.json is written to.
### Testing
We have had the following patch in production for the last 3 months.
<details>
<summary>Patch</summary>
```diff
diff --git a/node_modules/cordova-lib/src/cordova/platform/addHelper.js b/node_modules/cordova-lib/src/cordova/platform/addHelper.js
index 0856245..653f834 100644
--- a/node_modules/cordova-lib/src/cordova/platform/addHelper.js
+++ b/node_modules/cordova-lib/src/cordova/platform/addHelper.js
@@ -27,6 +27,9 @@ const cordova_util = require('../util');
const promiseutil = require('../../util/promise-util');
const platforms = require('../../platforms');
const detectIndent = require('detect-indent');
+const detectNewline = require('detect-newline');
+const stringifyPackage = require('stringify-package');
+const writeFileAtomicSync = require('write-file-atomic').sync;
const getPlatformDetailsFromDir = require('./getPlatformDetailsFromDir');
const preparePlatforms = require('../prepare/platforms');
@@ -228,7 +231,8 @@ function addHelper (cmd, hooksRunner, projectRoot, targets, opts) {
if (modifiedPkgJson === true) {
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ writeFileAtomicSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), { encoding: 'utf8' });
}
});
}).then(function () {
diff --git a/node_modules/cordova-lib/src/cordova/platform/remove.js b/node_modules/cordova-lib/src/cordova/platform/remove.js
index 5a91906..e8ade46 100644
--- a/node_modules/cordova-lib/src/cordova/platform/remove.js
+++ b/node_modules/cordova-lib/src/cordova/platform/remove.js
@@ -25,6 +25,9 @@ const cordova_util = require('../util');
const promiseutil = require('../../util/promise-util');
const platforms = require('../../platforms/platforms');
const detectIndent = require('detect-indent');
+const detectNewline = require('detect-newline');
+const stringifyPackage = require('stringify-package');
+const writeFileAtomicSync = require('write-file-atomic').sync;
module.exports = remove;
@@ -71,7 +74,8 @@ function remove (hooksRunner, projectRoot, targets, opts) {
if (modifiedPkgJson === true) {
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ writeFileAtomicSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), { encoding: 'utf8' });
}
}
}).then(function () {
diff --git a/node_modules/cordova-lib/src/cordova/plugin/add.js b/node_modules/cordova-lib/src/cordova/plugin/add.js
index 087bc29..95b1c10 100644
--- a/node_modules/cordova-lib/src/cordova/plugin/add.js
+++ b/node_modules/cordova-lib/src/cordova/plugin/add.js
@@ -32,6 +32,9 @@ const fs = require('fs-extra');
const semver = require('semver');
const url = require('url');
const detectIndent = require('detect-indent');
+const detectNewline = require('detect-newline');
+const stringifyPackage = require('stringify-package');
+const writeFileAtomicSync = require('write-file-atomic').sync;
const preparePlatforms = require('../prepare/platforms');
module.exports = add;
@@ -154,7 +157,8 @@ function add (projectRoot, hooksRunner, opts) {
// Write to package.json
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ writeFileAtomicSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), { encoding: 'utf8' });
}
const src = module.exports.parseSource(target, opts);
diff --git a/node_modules/cordova-lib/src/cordova/plugin/remove.js b/node_modules/cordova-lib/src/cordova/plugin/remove.js
index f6cdf89..a9cbfe6 100644
--- a/node_modules/cordova-lib/src/cordova/plugin/remove.js
+++ b/node_modules/cordova-lib/src/cordova/plugin/remove.js
@@ -28,6 +28,9 @@ const path = require('path');
const fs = require('fs-extra');
const PluginInfoProvider = require('cordova-common').PluginInfoProvider;
const detectIndent = require('detect-indent');
+const detectNewline = require('detect-newline');
+const stringifyPackage = require('stringify-package');
+const writeFileAtomicSync = require('write-file-atomic').sync;
const { Q_chainmap } = require('../../util/promise-util');
const preparePlatforms = require('../prepare/platforms');
@@ -142,7 +145,8 @@ function remove (projectRoot, targets, hooksRunner, opts) {
// Write out new package.json with plugin removed correctly.
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ writeFileAtomicSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), { encoding: 'utf8' });
}
}
}
```
</details>
### Checklist
- [ ] I've run the tests to see all new and existing tests pass
- [ ] I added automated test coverage as appropriate for this change
- [ ] Commit is prefixed with `(platform)` if this change only applies to one platform (e.g. `(android)`)
- [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/))
- [ ] I've updated the documentation if necessary
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "dpogue (via GitHub)" <gi...@apache.org>.
dpogue merged PR #941:
URL: https://github.com/apache/cordova-lib/pull/941
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "aidant (via GitHub)" <gi...@apache.org>.
aidant commented on PR #941:
URL: https://github.com/apache/cordova-lib/pull/941#issuecomment-2547506696
How would you like me to proceed? I've not changed the semantics around the interactions with the file system. Yet the security bot is calling out a potential race condition that already exists.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "dpogue (via GitHub)" <gi...@apache.org>.
dpogue commented on PR #941:
URL: https://github.com/apache/cordova-lib/pull/941#issuecomment-2547084492
You might need to update some test mocks or stub methods to use writeFileAtomicSync
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #941:
URL: https://github.com/apache/cordova-lib/pull/941#discussion_r1887771173
##########
src/cordova/platform/addHelper.js:
##########
@@ -226,7 +228,8 @@
if (modifiedPkgJson === true) {
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
Review Comment:
## Potential file system race condition
The file may have changed since it [was checked](1).
[Show more details](https://github.com/apache/cordova-lib/security/code-scanning/8)
##########
src/cordova/platform/remove.js:
##########
@@ -71,7 +73,8 @@
if (modifiedPkgJson === true) {
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
Review Comment:
## Potential file system race condition
The file may have changed since it [was checked](1).
[Show more details](https://github.com/apache/cordova-lib/security/code-scanning/9)
##########
src/cordova/plugin/remove.js:
##########
@@ -145,7 +147,8 @@
// Write out new package.json with plugin removed correctly.
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
Review Comment:
## Potential file system race condition
The file may have changed since it [was checked](1).
[Show more details](https://github.com/apache/cordova-lib/security/code-scanning/10)
##########
src/cordova/plugin/add.js:
##########
@@ -154,7 +156,8 @@
// Write to package.json
const file = fs.readFileSync(pkgJsonPath, 'utf8');
const indent = detectIndent(file).indent || ' ';
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, indent), 'utf8');
+ const newline = detectNewline(file);
+ fs.writeFileSync(pkgJsonPath, stringifyPackage(pkgJson, indent, newline), 'utf8');
Review Comment:
## Potential file system race condition
The file may have changed since it [was checked](1).
[Show more details](https://github.com/apache/cordova-lib/security/code-scanning/11)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "aidant (via GitHub)" <gi...@apache.org>.
aidant commented on PR #941:
URL: https://github.com/apache/cordova-lib/pull/941#issuecomment-2547516010
Thanks for the quick responses, I did not expect to get these reviewed or merged today! I assume these will be included in `12.0.3` or `12.1.0`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #941:
URL: https://github.com/apache/cordova-lib/pull/941#issuecomment-2547296547
## [Codecov](https://app.codecov.io/gh/apache/cordova-lib/pull/941?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 87.97%. Comparing base [(`db5c8c0`)](https://app.codecov.io/gh/apache/cordova-lib/commit/db5c8c014e917004108a440563b1aad7fc6b3140?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`4c21d44`)](https://app.codecov.io/gh/apache/cordova-lib/commit/4c21d44afbf85e9762ad1a8eab8b20f10a6452fe?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
> Report is 1 commits behind head on master.
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #941 +/- ##
==========================================
+ Coverage 87.91% 87.97% +0.06%
==========================================
Files 46 46
Lines 2134 2146 +12
==========================================
+ Hits 1876 1888 +12
Misses 258 258
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/cordova-lib/pull/941?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "dpogue (via GitHub)" <gi...@apache.org>.
dpogue commented on PR #941:
URL: https://github.com/apache/cordova-lib/pull/941#issuecomment-2547511476
yeah, I think this is probably fine to merge as is.
npm has refactored and deprecated a bunch of their tooling internally and now has a [`@npmcli/package-json`](https://www.npmjs.com/package/@npmcli/package-json) module that handles a bunch of package.json-related stuff. In our next major we should look at refactoring our package.json handling to use that, and centralize everywhere that we read/write package.json so that we don't have to make fixes like these ones in a bunch of different spots.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] Keep final new line in package.json [cordova-lib]
Posted by "aidant (via GitHub)" <gi...@apache.org>.
aidant commented on PR #941:
URL: https://github.com/apache/cordova-lib/pull/941#issuecomment-2547154618
I've replaced them with `fs.writeFileSync` because I don't think the internal structure of `writeFileAtomicSync` is compatible with the test structure.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org