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

adding resolutions should cause lockfile regeneration #5603

Conversation

MhMadHamster
Copy link
Contributor

Summary
After PR #5572 if you already have package.json with some dependencies and generated lockfile, after adding resolution and running yarn install lockfile wont be updated and package overriden by resolution will not be removed

Test plan
added new test which checks that after adding resolution, redundant package removed, maybe it is better to check the lockfile itself?

before:
image

after:
image

BYK
BYK previously requested changes Apr 11, 2018
Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for addressing this! I've made some comments regarding tests.

The main logic seems fine but I'd love @kaylieEB to give it a look before merging.

async (config, reporter): Promise<void> => {
const packageJson = await fs.readFile(path.join(config.cwd, 'package.json'));
// create new package.json with resolutions which override e/left-pad version
const newPackageJson = packageJson.replace(/}\n}/g, ' },\n"resolutions": {\n "e/left-pad": "1.1.1"\n }\n}');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use JSON.parse, modify the parsed object and then use JSON.stringify to write it back instead of using .replace on the JSON string?

const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
// don't expect left-pad in e/node_modules since it's now being replaced by single dependency [email protected]
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'e', 'node_modules', 'left-pad', 'index.js'))).toBe(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also verify that the lockfile got updated here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@MhMadHamster
Copy link
Contributor Author

tests failed because of the new version of left-pad, so snapshot doesn't match

Copy link
Member

@kaylie-alexa kaylie-alexa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the pr!

> => {
return runInstall(
{},
{source: 'resolutions', cwd: 'adding-resolutions-should-casue-lockfile-regeneration'},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here casue :)

// write new package.json
await fs.writeFile(path.join(config.cwd, 'package.json'), JSON.stringify(newPackageJson));
// run install again
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add an assertion before the reinstall the reverse of this logic (it should have a nested node module following the correct semver version?)

getPackageRemoteResolved(manifest: Manifest): ?string {
const packageReference: ?PackageReference = manifest._reference;
const packageRemote: ?PackageRemote = packageReference ? packageReference.remote : null;
return packageRemote ? packageRemote.resolved : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to do all the null checks given the types and invariant check... Also I'm seeing that this can be more of a helper/validation logic with what you have below that could later be moved to integrity checker, i.e. if there is a lockfile version, does the lockfile version match the resolution's resolved version?

// in resolution-map.js
export const shouldUpdateLockfile = (lockfileEntry: ?LockManifest, resolutionEntry: ?PackageReference) => {
    if (!lockfileEntry || !resolutionEntry) {
        return false;
    }

   return lockfileEntry.resolved !== resolutionEntry.remote.resolved;
}

// then call below;
if (shouldUpdateLockfile(lockManifest, resolutionManifest._reference)) {
   this.lockfile.removePattern(pattern);
}

maybe something like this would work? wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, this looks more readable

Copy link
Member

@arcanis arcanis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@arcanis arcanis merged commit f1b63ef into yarnpkg:master Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants