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

fix(node-packages): pin peer dependencies with undefined version #1609

Merged
merged 6 commits into from
Feb 24, 2022

Conversation

cgatt
Copy link
Contributor

@cgatt cgatt commented Feb 11, 2022

fixes #1611

Peer dependencies added without a fixed version are being added to devDependencies in package.json as name@^version even with pinnedDevDependency=true, which is throwing a JSII warning. This change adds another pass through the peer dependencies in the post-synth step to pin those peer dependencies who's version is not known during presynth. While this causes a degree of code duplication, I decided against replacing the pre-synth peerDependency pass as it minimises the impact of this change on existing workflows.

This change is tested both in mocked unit tests and through yarn link to a local project, where it displayed the expected behaviour, including correctly respecting any existing versioning in package.json.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Contributor

@Chriscbr Chriscbr left a comment

Choose a reason for hiding this comment

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

Could you create a bug report first (and then link this PR to it)? It's really helpful for us to have an explanation of the issue by itself -- something of the flavor of "when I did X I expected Y, but instead Z happened".

Comment on lines 196 to 197
expect(pkgFile.peerDependencies).toStrictEqual({ ms: "^2.1.3" });
expect(pkgFile.devDependencies).toStrictEqual({ ms: "2.1.3" });
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change this to some other random version number to make it clear that the mock is working and the test isn't dependent on the actual latest version of "ms"

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

test/javascript/node-package.test.ts Show resolved Hide resolved
Comment on lines 232 to 244
const project = new Project({ name: "test" });
const pkg = new NodePackage(project);

const orig = {
name: "test",
devDependencies: {
ms: "^2.2.2",
},
main: "lib/index.js",
license: "Apache-2.0",
version: "0.0.0",
"//": '~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".',
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if I understand this test case. It looks like we are asserting that if the user happens to have "ms" as a dev dependency (but not configured by projen), that projen will not change that after synthesizing? I'm not sure if that is an appropriate promise to make in our API -- either the user should be managing all of their dependencies through projen, or none of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was included to mimic the current behaviour, on line 979 of node-package.ts:
// respect user version if it's not '*' or if current version is undefined
I'd be more than happy for the behaviour of the peer pinning to behave differently in this case if that's preferable?

Comment on lines 293 to 294
const project = new Project({ name: "test" });
const pkg = new NodePackage(project);
Copy link
Contributor

Choose a reason for hiding this comment

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

If these test cases are assuming that pinnedDevDependencies is true, we should specify that explicitly in the tests when initializing NodePackage, and not take the default for granted. If the test case assertions here are irrespective of the setting of pinnedDevDependencies, then I think it might make sense to show that explicitly (e.g. by dividing this into one test where pinnedDevDependencies is true, and one test where it's false).

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

@codecov-commenter
Copy link

codecov-commenter commented Feb 12, 2022

Codecov Report

Merging #1609 (c119004) into main (d90284c) will increase coverage by 1.31%.
The diff coverage is 89.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1609      +/-   ##
==========================================
+ Coverage   88.06%   89.38%   +1.31%     
==========================================
  Files         132      146      +14     
  Lines        5109     5970     +861     
  Branches     1207     1543     +336     
==========================================
+ Hits         4499     5336     +837     
- Misses        610      632      +22     
- Partials        0        2       +2     
Impacted Files Coverage Δ
src/awscdk/internal.ts 100.00% <ø> (ø)
src/cdk/consts.ts 100.00% <ø> (+36.36%) ⬆️
src/dev-env.ts 83.33% <0.00%> (ø)
src/java/index.ts 100.00% <ø> (ø)
src/python/index.ts 100.00% <ø> (ø)
src/release/index.ts 100.00% <ø> (ø)
src/typescript/typescript-typedoc.ts 20.00% <0.00%> (ø)
src/typescript/typescript.ts 94.21% <ø> (-0.29%) ⬇️
src/util.ts 91.66% <ø> (-0.71%) ⬇️
src/util/semver.ts 57.14% <ø> (+6.68%) ⬆️
... and 171 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 38127d7...c119004. Read the comment docs.

@cgatt
Copy link
Contributor Author

cgatt commented Feb 12, 2022

I've raised an issue and linked to it in the PR description, and modified the tests.
Went slightly overboard because the repeated code for mocking yarn install was annoying me, so I moved it into a utility function which has a bit of extra logic for handling multiple dependencies fields and installing a version that meets all given dependency ranges. Wasn't needed for this, but I saw it being a problem for future tests. This is why I've slightly changed an existing test (line 162), as I made the fake yaml.lock file a little closer to the real format.

  • Tests have been modified to each use arbitrary dep versions to ensure no interaction/external calls.
  • Tests explicitly set pinnedDevDependencies to true
  • Additional test added for pinnedDevDependencies = false
  • Additional comments included

Copy link
Contributor

@Chriscbr Chriscbr 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 your patience @cgatt. I think is almost ready to get merged in, just have a few comments to improve readability in a few places.

Overall the code in projen for resolving dependencies is a bit confusing to grok / feels like it could benefit from some kind of refactoring, but that makes more sense for a separate PR. I appreciate you writing out several test cases and keeping the actual fix here pretty self-contained. 👍

test/javascript/node-package.test.ts Outdated Show resolved Hide resolved
test/javascript/node-package.test.ts Outdated Show resolved Hide resolved
cgatt and others added 2 commits February 24, 2022 13:57
Co-authored-by: Christopher Rybicki <crybicki98@gmail.com>
Co-authored-by: Christopher Rybicki <crybicki98@gmail.com>
@mergify mergify bot merged commit 1811df3 into projen:main Feb 24, 2022
@cgatt cgatt deleted the cgatt/pin-unversioned-peers branch February 14, 2024 06:21
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.

Peer dependencies arent pinned as dev dependencies if their version is unset
3 participants