Skip to content

Question: how to fix versions between dependencies? #359

Closed

Description

@SamyPesse

I don't know if Lerna has some automation to manage this, and I couldn't find any related issues to this:

Basically, I understand that the "Fixed/Locked mode" uses one version for all modules (this is what I want), but is there a way to fix dependencies between modules?

For example, if A depends on B, and the version in lerna.json is 4.1.0, I want packages/A/package.json to be updated with:

{
  "dependencies": {
    "B": "4.1.0"
  }
}

instead of "B": "^4.0.0" (it seems to be what Babel is doing).

Activity

  1. SamyPesse commented on Oct 4, 2016

    @SamyPesse
    Author

    Update: It doesn't look like something that can be done by lerna. I'll write a script in the repository to update all package.json files to fix versions according to the "master" version.

  2. SamyPesse commented on Oct 8, 2016

    @SamyPesse
    Author

    Here is the script I'm using:

    #! /usr/bin/env node
    
    const fs = require('fs');
    const path = require('path');
    
    const lernaConfig = require('../lerna.json');
    
    // List all the packages
    const PACKAGES_DIR = path.resolve(__dirname, '../packages');
    const packages = fs.readdirSync(PACKAGES_DIR);
    
    function updateDependencies(dependencies) {
        Object.keys(dependencies).map((key) => {
            if (!packages.includes(key)) {
                return;
            }
    
            dependencies[key] = lernaConfig.version;
        });
    }
    
    packages.forEach((name) => {
        // Avoid .DS_Store
        if (name[0] === '.') {
            return;
        }
    
        const pkgPath = path.resolve(PACKAGES_DIR, name, 'package.json');
        const pkg = require(pkgPath);
    
        pkg.version = lernaConfig.version;
        updateDependencies(pkg.dependencies);
        updateDependencies(pkg.devDependencies);
    
        fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 3), 'utf-8');
    });
  3. Gongreg commented on Nov 3, 2016

    @Gongreg
    Contributor

    Hey, when do you run this script? One person in our team accidentally made a change in multiple packages, which looked like it didn't create a breaking change, but because inner package dependencies are not locked, it broke other place which used older version. This thing would help us a lot.

    EDIT: Didn't get a response, so I solved it this way:
    Run a script during git pre-commit and check if lerna.json file has changed.

    #!/bin/bash
    
    UPDATED=$(git diff --cached --name-only)
    
    if grep -q lerna.json <<< $UPDATED; then
        echo 'Fixing package versions...'
        node ./scripts/fix-versions.js
        exit 0;
    else
        exit 0;
    fi
    

    If it has changed I run script which simply removes ^ from before version.

    const fs = require('fs');
    const path = require('path');
    const exec = require('child_process').execSync;
    
    const lernaConfig = require('../lerna.json');
    
    // List all the packages
    const PACKAGES_DIR = path.resolve(__dirname, '../packages');
    const packages = fs.readdirSync(PACKAGES_DIR);
    
    //only updates private dependencies
    function updateDependencies(dependencies) {
        Object.keys(dependencies).forEach(key => {
            if (!key.includes('$private repo$')) {
                return;
            }
    
            dependencies[key] = dependencies[key].replace('^', '');
        });
    }
    
    packages.forEach(name => {
        // Avoid .DS_Store
        if (name[0] === '.') {
            return;
        }
    
        const pkgPath = path.resolve(PACKAGES_DIR, name, 'package.json');
        const pkg = require(pkgPath);
    
        if (pkg.version === lernaConfig.version) {
    
            pkg.dependencies && updateDependencies(pkg.dependencies);
            pkg.devDependencies && updateDependencies(pkg.devDependencies);
    
            fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 3), 'utf-8');
            exec('git add ' + pkgPath);
        }
    });
    

    Code is not nice (mutations everywhere), but it will hold.

    And after changing file it adds it to staged files. I am not sure it works correctly, we will still have to try it out.

  4. jamiebuilds commented on Nov 16, 2016

    @jamiebuilds
    Contributor

    Merging into #259

  5. gigabo commented on Dec 13, 2016

    @gigabo
    Contributor

    Looking at this again, I think it may actually be different from #259. If I'm reading this right, this issue is about pinning to exact versions within the repo, while #259 is about managing external deps.

    @Gongreg pointed out that #426 seems to address this issue.

  6. reopened this on Dec 13, 2016
  7. develar commented on Jan 2, 2017

    @develar

    My expectation was exactly the same — dependent package version should be fixed, not using ^ or ~ My main package is an aggregator of other packages and, so, I want to be sure that all are compatible and tested together, so, versions should be fixed.

  8. vitorbal commented on Jan 5, 2018

    @vitorbal

    I think this can be closed now that #426 was merged, which adds support for the --exact flag.

  9. lock commented on Dec 27, 2018

    @lock

    This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  10. locked as resolved and limited conversation to collaborators on Dec 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions