Skip to content

[Bug?]: yarn constraints --fix leads to malformed package.jsons #5853

Closed

Description

Self-service

  • I'd be willing to implement a fix

Describe the bug

I just upgraded to yarn v4 from 3.6.1 and have been playing around with the new js-based constraints. I've written a couple of constraints in yarn.config.cjs and they seem to work well, but running yarn constraints --fix followed by a second yarn constraints in a monorepo reveals that the --fix call mangled one or more package.jsons:

❯ yarn constraints --fix
....
❯ yarn constraints
undefined:74
s"
^

SyntaxError: Unexpected token s in JSON at position 2623 (when parsing /some/monorepo/package/package.json)
    at JSON.parse (<anonymous>)
    at uE.loadFile (/some/monorepo/.yarn/releases/yarn-4.0.0.cjs:140:120447)
    at async uE.fromFile (/some/monorepo/.yarn/releases/yarn-4.0.0.cjs:140:120130)
    at async uE.tryFind (/some/monorepo/.yarn/releases/yarn-4.0.0.cjs:140:119850)
    at async lC.setup (/some/monorepo/.yarn/releases/yarn-4.0.0.cjs:205:9258)

Node.js v18.18.0

git diff shows there are multiple package.jsons that have this modification:

     "*.global.scss"
   ]
 }
+s"
+  ]
+}

To reproduce

(the link above to https://yarnpkg.com/advanced/sherlock does not resolve, fwiw)

EDIT: I originally had a more complicated set of constraints, but have reproduced the issue consistently even with one constraint below

My constraint looks as follows:

// @ts-check
/** @type {import('@yarnpkg/types')} */
const { defineConfig } = require(`@yarnpkg/types`);

/**
 * @typedef {import('@yarnpkg/types').Yarn.Constraints.Workspace} Workspace
 * @typedef {import('@yarnpkg/types').Yarn.Constraints.Dependency} Dependency
 */

/** @param {Dependency} dependency */
function enforceWorkspaceDependencyRange(dependency) {
    const rangePrefix = dependency.range.startsWith('>=') ? '>=' : '^';
    dependency.update(`workspace:${rangePrefix}`);
}

module.exports = defineConfig({
    constraints: async ({ Yarn }) => {
        const allDependencies = Yarn.dependencies();

        for (const dependency of allDependencies) {
            if (dependency.resolution?.workspace) {
                enforceWorkspaceDependencyRange(dependency);
            }
        }
    },
});

Environment

System:
    OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (32) x64 Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz
  Binaries:
    Node: 18.18.0 - /nail/tmp/xfs-f84e62f7/node
    Yarn: 4.0.0 - /nail/tmp/xfs-f84e62f7/yarn
    npm: 9.8.1 - /opt/nodejs/node-v18.18.0/bin/npm
  npmPackages:
    jest: ^29.1.2 => 29.1.2

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions