Skip to content

[rush] malformed package.json by explicitly setting jsonSyntax to strict #4813

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

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Fix malformed package.json by explicitly setting jsonSyntax to strict",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion common/reviews/api/node-core-library.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export interface IJsonFileSaveOptions extends IJsonFileStringifyOptions {
}

// @public
export interface IJsonFileStringifyOptions {
export interface IJsonFileStringifyOptions extends IJsonFileParseOptions {
headerComment?: string;
ignoreUndefinedValues?: boolean;
newlineConversion?: NewlineKind;
Expand Down
19 changes: 16 additions & 3 deletions libraries/node-core-library/src/JsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand Down Expand Up @@ -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 ?? JsonSyntax.Json5,
indent: 2
});
} else if (options.prettyFormatting) {
stringified = jju.stringify(newJsonObject, {
mode: 'json',
mode: explicitMode ?? 'json',
indent: 2
});

Expand Down
7 changes: 5 additions & 2 deletions libraries/rush-lib/src/api/PackageJsonEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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;
Expand Down
Loading