diff --git a/.changeset/solid-eagles-mate.md b/.changeset/solid-eagles-mate.md new file mode 100644 index 0000000000..a5ac5e6b46 --- /dev/null +++ b/.changeset/solid-eagles-mate.md @@ -0,0 +1,8 @@ +--- +"@pnpm/workspace.manifest-writer": patch +"pnpm": patch +--- + +Fix YAML formatting preservation in `pnpm-workspace.yaml` when running commands like `pnpm update`. Previously, quotes and other formatting were lost even when catalog values didn't change. + +Closes #10425 diff --git a/workspace/manifest-writer/src/index.ts b/workspace/manifest-writer/src/index.ts index e5e376fbe0..e523efe3b8 100644 --- a/workspace/manifest-writer/src/index.ts +++ b/workspace/manifest-writer/src/index.ts @@ -115,13 +115,14 @@ function addCatalogs (manifest: Partial, newCatalogs: Catalog } targetCatalog ??= {} - targetCatalog[dependencyName] = specifier + if (targetCatalog[dependencyName] !== specifier) { + targetCatalog[dependencyName] = specifier + shouldBeUpdated = true + } } if (targetCatalog == null) continue - shouldBeUpdated = true - if (targetCatalogWasNil) { if (catalogName === 'default') { manifest.catalog = targetCatalog diff --git a/workspace/manifest-writer/test/updateWorkspaceManifest.test.ts b/workspace/manifest-writer/test/updateWorkspaceManifest.test.ts index 6270509cfe..c1d94ba600 100644 --- a/workspace/manifest-writer/test/updateWorkspaceManifest.test.ts +++ b/workspace/manifest-writer/test/updateWorkspaceManifest.test.ts @@ -98,3 +98,58 @@ test('updateWorkspaceManifest updates allowBuilds', async () => { }, }) }) + +test('updateWorkspaceManifest adds a new catalog', async () => { + const dir = tempDir(false) + const filePath = path.join(dir, WORKSPACE_MANIFEST_FILENAME) + + fs.writeFileSync(filePath, 'packages:\n - \'*\'\n') + + await updateWorkspaceManifest(dir, { + updatedCatalogs: { + default: { + foo: '1.0.0', + }, + }, + }) + + expect(readYamlFile(filePath)).toStrictEqual({ + packages: ['*'], + catalog: { foo: '1.0.0' }, + }) +}) + +test('updateWorkspaceManifest preserves quotes', async () => { + const dir = tempDir(false) + const filePath = path.join(dir, WORKSPACE_MANIFEST_FILENAME) + + const manifest = `\ +catalog: + "bar": "2.0.0" + 'foo': '1.0.0' + qar: 3.0.0 +` + + const expected = `\ +catalog: + "bar": "2.0.0" + 'foo': '1.0.0' + qar: 3.0.0 + zoo: 4.0.0 +` + + fs.writeFileSync(filePath, manifest) + + await updateWorkspaceManifest(dir, { + updatedCatalogs: { + default: { + foo: '1.0.0', + bar: '2.0.0', + qar: '3.0.0', + zoo: '4.0.0', + }, + }, + }) + + expect(fs.readFileSync(filePath).toString()).toStrictEqual(expected) +})