From 45c1d903bbb5b62cbb2cf525a4125b028491aec2 Mon Sep 17 00:00:00 2001 From: Kenrick Date: Thu, 27 Jun 2024 10:40:31 +0800 Subject: [PATCH 1/4] fix: malformed package.json by explicitly setting jsonSyntax to strict --- ...kenrick-fix-rush-add_2024-06-27-02-39.json | 10 ++++++++++ ...kenrick-fix-rush-add_2024-06-27-02-39.json | 10 ++++++++++ common/reviews/api/node-core-library.api.md | 2 +- libraries/node-core-library/src/JsonFile.ts | 19 ++++++++++++++++--- .../rush-lib/src/api/PackageJsonEditor.ts | 7 +++++-- 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json create mode 100644 common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json diff --git a/common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json b/common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json new file mode 100644 index 00000000000..3f769b697bf --- /dev/null +++ b/common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/rush", + "comment": "Fix malformed package.json by explicitly setting jsonSyntax to strict", + "type": "none" + } + ], + "packageName": "@microsoft/rush" +} \ No newline at end of file diff --git a/common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json b/common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json new file mode 100644 index 00000000000..bcf4c4aa1fb --- /dev/null +++ b/common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/node-core-library", + "comment": "Accept jsonSyntax option at save method to avoid malformed package.json", + "type": "patch" + } + ], + "packageName": "@rushstack/node-core-library" +} \ No newline at end of file diff --git a/common/reviews/api/node-core-library.api.md b/common/reviews/api/node-core-library.api.md index 156a23e81d0..3ad36597629 100644 --- a/common/reviews/api/node-core-library.api.md +++ b/common/reviews/api/node-core-library.api.md @@ -416,7 +416,7 @@ export interface IJsonFileSaveOptions extends IJsonFileStringifyOptions { } // @public -export interface IJsonFileStringifyOptions { +export interface IJsonFileStringifyOptions extends IJsonFileParseOptions { headerComment?: string; ignoreUndefinedValues?: boolean; newlineConversion?: NewlineKind; diff --git a/libraries/node-core-library/src/JsonFile.ts b/libraries/node-core-library/src/JsonFile.ts index b5294bb5d2d..4afa5fc4888 100644 --- a/libraries/node-core-library/src/JsonFile.ts +++ b/libraries/node-core-library/src/JsonFile.ts @@ -128,7 +128,7 @@ export interface IJsonFileLoadAndValidateOptions extends IJsonFileParseOptions, * * @public */ -export interface IJsonFileStringifyOptions { +export interface IJsonFileStringifyOptions extends IJsonFileParseOptions { /** * If provided, the specified newline type will be used instead of the default `\r\n`. */ @@ -345,17 +345,30 @@ export class JsonFile { JsonFile.validateNoUndefinedMembers(newJsonObject); } + let explicitMode: 'json5' | 'json' | 'cjson' | undefined = undefined; + switch (options.jsonSyntax) { + case JsonSyntax.Strict: + explicitMode = 'json'; + break; + case JsonSyntax.JsonWithComments: + explicitMode = 'cjson'; + break; + case JsonSyntax.Json5: + explicitMode = 'json5'; + break; + } + let stringified: string; if (previousJson !== '') { // NOTE: We don't use mode=json here because comments aren't allowed by strict JSON stringified = jju.update(previousJson, newJsonObject, { - mode: JsonSyntax.Json5, + mode: explicitMode ? explicitMode : JsonSyntax.Json5, indent: 2 }); } else if (options.prettyFormatting) { stringified = jju.stringify(newJsonObject, { - mode: 'json', + mode: explicitMode ? explicitMode : 'json', indent: 2 }); diff --git a/libraries/rush-lib/src/api/PackageJsonEditor.ts b/libraries/rush-lib/src/api/PackageJsonEditor.ts index f09c2a1e0fe..8212164169b 100644 --- a/libraries/rush-lib/src/api/PackageJsonEditor.ts +++ b/libraries/rush-lib/src/api/PackageJsonEditor.ts @@ -2,7 +2,7 @@ // See LICENSE in the project root for license information. import * as semver from 'semver'; -import { InternalError, type IPackageJson, JsonFile, Sort } from '@rushstack/node-core-library'; +import { InternalError, type IPackageJson, JsonFile, Sort, JsonSyntax } from '@rushstack/node-core-library'; import { cloneDeep } from '../utilities/objectUtilities'; /** @@ -308,7 +308,10 @@ export class PackageJsonEditor { if (this._modified) { this._modified = false; this._sourceData = this._normalize(this._sourceData); - JsonFile.save(this._sourceData, this.filePath, { updateExistingFile: true }); + JsonFile.save(this._sourceData, this.filePath, { + updateExistingFile: true, + jsonSyntax: JsonSyntax.Strict + }); return true; } return false; From 99c94e7b1281dab21f108076ec8e8cc41fcbd60a Mon Sep 17 00:00:00 2001 From: Kenrick Date: Fri, 28 Jun 2024 09:04:01 +0800 Subject: [PATCH 2/4] Update libraries/node-core-library/src/JsonFile.ts Co-authored-by: David Michon --- libraries/node-core-library/src/JsonFile.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/node-core-library/src/JsonFile.ts b/libraries/node-core-library/src/JsonFile.ts index 4afa5fc4888..70162acbeaf 100644 --- a/libraries/node-core-library/src/JsonFile.ts +++ b/libraries/node-core-library/src/JsonFile.ts @@ -368,7 +368,7 @@ export class JsonFile { }); } else if (options.prettyFormatting) { stringified = jju.stringify(newJsonObject, { - mode: explicitMode ? explicitMode : 'json', + mode: explicitMode ?? 'json', indent: 2 }); From b8f2a2e9a56460bb76f0580e56c9755ffcd74ac4 Mon Sep 17 00:00:00 2001 From: Kenrick Date: Fri, 28 Jun 2024 09:04:06 +0800 Subject: [PATCH 3/4] Update libraries/node-core-library/src/JsonFile.ts Co-authored-by: David Michon --- libraries/node-core-library/src/JsonFile.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/node-core-library/src/JsonFile.ts b/libraries/node-core-library/src/JsonFile.ts index 70162acbeaf..e396de08131 100644 --- a/libraries/node-core-library/src/JsonFile.ts +++ b/libraries/node-core-library/src/JsonFile.ts @@ -363,7 +363,7 @@ export class JsonFile { if (previousJson !== '') { // NOTE: We don't use mode=json here because comments aren't allowed by strict JSON stringified = jju.update(previousJson, newJsonObject, { - mode: explicitMode ? explicitMode : JsonSyntax.Json5, + mode: explicitMode ?? JsonSyntax.Json5, indent: 2 }); } else if (options.prettyFormatting) { From 1a9600d1949bde5e9c58bf6837a5eec76c46a218 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Mon, 1 Jul 2024 11:20:16 -0700 Subject: [PATCH 4/4] Rush change. --- .../rush/kenrick-fix-rush-add_2024-06-27-02-39.json | 2 +- .../kenrick-fix-rush-add_2024-06-27-02-39.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json b/common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json index 3f769b697bf..851253ca45c 100644 --- a/common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json +++ b/common/changes/@microsoft/rush/kenrick-fix-rush-add_2024-06-27-02-39.json @@ -2,7 +2,7 @@ "changes": [ { "packageName": "@microsoft/rush", - "comment": "Fix malformed package.json by explicitly setting jsonSyntax to strict", + "comment": "Fix an issue where running `rush add` in a project can generate a `package.json` file that uses JSON5 syntax. Package managers expect strict JSON.", "type": "none" } ], diff --git a/common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json b/common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json index bcf4c4aa1fb..445fecfcfe2 100644 --- a/common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json +++ b/common/changes/@rushstack/node-core-library/kenrick-fix-rush-add_2024-06-27-02-39.json @@ -2,8 +2,8 @@ "changes": [ { "packageName": "@rushstack/node-core-library", - "comment": "Accept jsonSyntax option at save method to avoid malformed package.json", - "type": "patch" + "comment": "Add support for the `jsonSyntax` option to the `JsonFile.save`, `JsonFile.saveAsync`, and `JsonFile.stringify` functions.", + "type": "minor" } ], "packageName": "@rushstack/node-core-library"