Skip to content
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

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

Closed
1 task
jdb8 opened this issue Oct 24, 2023 · 1 comment · Fixed by #5871
Closed
1 task

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

jdb8 opened this issue Oct 24, 2023 · 1 comment · Fixed by #5871
Labels
bug Something isn't working

Comments

@jdb8
Copy link

jdb8 commented Oct 24, 2023

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

@arcanis
Copy link
Member

arcanis commented Oct 25, 2023

Oops, thanks for the report! Will be fixed by #5871

arcanis added a commit that referenced this issue Oct 26, 2023
…#5871)

**What's the problem this PR addresses?**

I forgot to add a `Promise.all`, so the `package.json` files could end
up corrupted when the constraints engine had to perform multiple
iterations of the loop.

Fixes #5853

**How did you fix it?**

Adds `Promise.all` before running subsequent iterations.

**Checklist**
<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants