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

feat: add support for yarn 2 lock files #56 #57

Merged
merged 1 commit into from
Jun 24, 2020

Conversation

regevbr
Copy link
Contributor

@regevbr regevbr commented Apr 14, 2020

What this does

Fixes #56
Allows parsing of yarn 2 lock files, which are different from yarn 1

Notes for the reviewer

  • I used @yarnpkg/parsers to parse the lock file
  • I used @yarnpkg/core for parsing the version descriptors that are in the lock files
  • yarn 2 is only supported from node 10 and above (I didn't compromise the compatibility of this library)
  • I reused the yarn 1 parser logic, by normalizing the dep tree from yarn 2
  • I fixed some existing tests that were wrong
  • To realize that the project is using yarn 2, I check the existence of .yarnrc.yml
  • I had to upgrade to typescript 3.4 (the lowest possible version) as the typings in the dependencies of yarns packages were causing build issues and the only way to ignore them (as they are not important) is to use tsconfig skipLibCheck, which was only introduced (or at least worked) in typescript 3.4

@regevbr regevbr requested a review from a team as a code owner April 14, 2020 19:36
@CLAassistant
Copy link

CLAassistant commented Apr 14, 2020

CLA assistant check
All committers have signed the CLA.

@regevbr
Copy link
Contributor Author

regevbr commented Apr 14, 2020

@orsagie @lili2311 @FauxFaux @miiila can you please CR this?

@regevbr regevbr changed the title bug: yarn 2 lock file parsing issues #56 feat: yarn 2 lock file parsing issues #56 Apr 14, 2020
@regevbr regevbr changed the title feat: yarn 2 lock file parsing issues #56 feat: add support for yarn 2 lock files #56 Apr 14, 2020
@lili2311
Copy link
Contributor

Hi @regevbr 👋 Thank you was this awesome contribution, we have many team members on holiday today so we would like to discuss this internally with them when they are back tomorrow and get back to you.

@regevbr
Copy link
Contributor Author

regevbr commented Apr 20, 2020

@lili2311 how are you?

Can you please let me know when you are expected to fix the yarn2 issue? this is the only reason I can't migrate my projects to use yarn2...

@regevbr
Copy link
Contributor Author

regevbr commented Apr 20, 2020

@arcanis I know you are a very busy man, but I was wondering if you can take a quick look into my PR here to see if I have done it right and if maybe there is a better way to achieve it?
I thought about resuing the code from each relevant plugin but they are too coupled to the actual resolving logic.

Copy link

@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.

I didn't run the tests, just looking at the Yarn-specific parts 🙂

lib/index.ts Outdated Show resolved Hide resolved
strict = true ): Promise<PkgTree> {
if (lockfile.type !== this.type) {
throw new InvalidUserInputError('Unsupported lockfile provided. ' +
'Please provide `package-lock.json`.');
Copy link

Choose a reason for hiding this comment

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

Suggested change
'Please provide `package-lock.json`.');
'Please provide a `yarn.lock` file.');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will leave that decision to the maintainers

lib/parsers/yarn-utils.ts Outdated Show resolved Hide resolved
lib/parsers/yarn2-lock-parse.ts Outdated Show resolved Hide resolved
@regevbr
Copy link
Contributor Author

regevbr commented Apr 20, 2020

@arcanis thanks for the review! much appreciated :-) I will fix all your comments

lib/index.ts Outdated
@@ -79,7 +90,11 @@ async function buildDepTreeFromFiles(
if (_.endsWith(lockFilePath, 'package-lock.json')) {
lockFileType = LockfileType.npm;
} else if (_.endsWith(lockFilePath, 'yarn.lock')) {
lockFileType = LockfileType.yarn;
if (fs.existsSync(path.resolve(root, lockFilePath.replace('yarn.lock', '.yarnrc.yml')))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is there anywhere else these can live? they are always in the same directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, they are in the same directory

if (fs.existsSync(path.resolve(root, lockFilePath.replace('yarn.lock', '.yarnrc.yml')))) {
lockFileType = LockfileType.yarn2;
} else {
lockFileType = LockfileType.yarn;
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: we need a way to send some meta back to track yarn v2 tests vs others

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The lock file is sent back in the return value of buildDepTree. You mean you need to handle the different type in the code that uses that library?

lib/index.ts Outdated
} else {
throw new UnsupportedRuntimeError('Parsing `yarn.lock` is not ' +
'supported on Node.js version less than 10. Please upgrade your ' +
'Node.js environment or use `package-lock.json`');
Copy link
Contributor

Choose a reason for hiding this comment

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

in this situation an older v1 yarn.lock would also work? Doesn't make sense to ask to switch to npm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will not, as the lock file in v1 is not standard yaml file... And in any case yarn2 only works with node 10 and above so I don't think we will have a problem here

@dkontorovskyy
Copy link
Contributor

@regevbr that is really nice contribution. I will pull it to test locally first and then let's try to merge it. If I will need any of your help, are you up to pairing in close future?

@regevbr
Copy link
Contributor Author

regevbr commented Apr 22, 2020

@dkontorovskyy sure thing! anything you need. Thanks!

@regevbr
Copy link
Contributor Author

regevbr commented Apr 23, 2020

I rebased as there were some changes made to master.
I also synced the tree size limits logic to the base yarn parser and added the relevant test to yarn2 parser test

@dkontorovskyy
Copy link
Contributor

Hey @regevbr. I run local tests on your PR and it looks great 🎉 . I would like to make a graceful roll out for yarn v2, so it would be great if we can pair and separate this PR in 2 smaller bits and take it 1 by 1 into this plugin. I would like to schedule a pairing session with you whenever you free. What do you think about that?

@regevbr
Copy link
Contributor Author

regevbr commented Apr 27, 2020

@dkontorovskyy thanks!
Sure we can do a session, I'm generally available 12am-3am GMT+3
How do you want to split it?

@dkontorovskyy
Copy link
Contributor

@regevbr luckily we are in the same timezone 😃
I can do 12AM-2PM tomorrow. Want me to send you an invite to zoom meeting? How can I contact you ?
I would like to split it into two: first one is refactor needed for a new feature and second one is actually yarn2 support that will be easily added once the first one is in.

@dkontorovskyy dkontorovskyy mentioned this pull request Apr 28, 2020
5 tasks
@dkontorovskyy
Copy link
Contributor

@regevbr just keeping update public 😃 . We are looking to plan this feature properly this sprint and start active development next sprint. This is an amazing contribution, so we would try to bring it into the core of Snyk as soon as possible, we just need to make sure this can be done harmlessly to the overall system 🙏

@lili2311
Copy link
Contributor

#56

@dkontorovskyy
Copy link
Contributor

@regevbr first refactor PR went in 💪

@regevbr
Copy link
Contributor Author

regevbr commented May 28, 2020

@dkontorovskyy that's good news!
I will rebase this branch so we will be ready for the next phasse

@dkontorovskyy
Copy link
Contributor

dkontorovskyy commented Jun 1, 2020

@arcanis using @yarnpkg/core in module yields tsc error

node_modules/@types/emscripten/index.d.ts:53:33 - error TS2304: Cannot find name 'WebGLRenderingContext'.
53     preinitializedWebGLContext: WebGLRenderingContext;
                                   ~~~~~~~~~~~~~~~~~~~~~
node_modules/@types/emscripten/index.d.ts:67:28 - error TS2304: Cannot find name 'MessageEvent'.
67     onCustomMessage(event: MessageEvent): void;

I see that there was an addition to package.json of @yarnpgk/libzip to use @types/emscripten as production dependencies, because of the bug (yarnpkg/berry#746).

Loos like node.js doesn't like WebAssembly types.

Are you aware of any workarounds for that except silencing TSC compiler with --skipLibCheck ?

@regevbr
Copy link
Contributor Author

regevbr commented Jun 1, 2020

@dkontorovskyy this is solved by adding "dom" as a lib to tsconfig:

        "lib": [
            "es2017",
            "dom"
        ],

I tried getting the library to compile without skipping the lib check, and it requires a lot of changes as there are multiple errors originating from different modules.
The only errors left comes from d.ts reference issues in @yarnpkg/core. For example:

node_modules/@yarnpkg/core/lib/structUtils.d.ts:91:66 - error TS2307: Cannot find module '../../yarnpkg-fslib/sources'.

91 export declare function slugifyLocator(locator: Locator): import("../../yarnpkg-fslib/sources").Filename;

I will open a ticket to fix it in yarn. You can check out all the changes I made in the next commit to this branch and let me know if that is that important for you to not skip the lib check.

@regevbr
Copy link
Contributor Author

regevbr commented Jun 14, 2020

@dkontorovskyy I have worked with @arcanis and the issue is now fixed!
There is a new issue in clipanion which I will try to get resolved soon as well

@regevbr
Copy link
Contributor Author

regevbr commented Jun 15, 2020

@dkontorovskyy I fixed the issue in clipanion and now the build works perfectly!
How do we proceed?

@dkontorovskyy
Copy link
Contributor

@regevbr that is so cool!! What was the issue and fix?

@regevbr
Copy link
Contributor Author

regevbr commented Jun 16, 2020

@dkontorovskyy in clipanion? see arcanis/clipanion#25 for more details, in high level, the tool used for minifying the d.ts file has bugs in it causing a faulty d.ts file.

If you are asking about the issue in @yarnpkg/core than it is very complicated you can check out my
summary at yarnpkg/berry#1278 (comment)

@dkontorovskyy dkontorovskyy changed the base branch from master to feat/yarn-v2 June 24, 2020 10:20
@dkontorovskyy dkontorovskyy merged commit cbdb8fa into snyk:feat/yarn-v2 Jun 24, 2020
@dkontorovskyy
Copy link
Contributor

@regevbr I merged your PR in our separate feature branch to run some final round of investigation we are doing with the team today. Will keep you posted 🤗

@regevbr
Copy link
Contributor Author

regevbr commented Jun 24, 2020

Thanks @dkontorovskyy!
Looking forward to hear back from you

@anthogez anthogez mentioned this pull request Jun 24, 2020
3 tasks
@regevbr
Copy link
Contributor Author

regevbr commented Jul 12, 2020

@dkontorovskyy! how's it going? I see that the feature was finally merged! does that mean I can enable snyk in my yarn2 packages now (ci and PR guard)?

1 similar comment
@regevbr
Copy link
Contributor Author

regevbr commented Jul 28, 2020

@dkontorovskyy! how's it going? I see that the feature was finally merged! does that mean I can enable snyk in my yarn2 packages now (ci and PR guard)?

@dkontorovskyy
Copy link
Contributor

Hi @regevbr, sorry for spacing out!

It should work in CLI now. Just try scanning your project locally or in CI. Not in PR guard yet. My team will be looking soon into this.

@regevbr regevbr deleted the #56 branch August 6, 2020 11:37
@regevbr
Copy link
Contributor Author

regevbr commented Aug 6, 2020

That's great news! Please let me know when the guard is working as well as we use it as well :-)

@arcanis arcanis mentioned this pull request Dec 21, 2020
10 tasks
@kachkaev
Copy link

Still getting parse errors in a Github PR here: kachkaev/njt#29

Screenshot 2020-12-24 at 11 35 29

What are the current plans to resolve this?

I had to replace Snyk integration with running yarn audit on schedule in another project because it was blocking the PR merge. Looking forward to bring Snyk back when CI integration is finally working!

cc @arcanis

@kachkaev
Copy link

kachkaev commented Aug 7, 2021

👋 from August 2021 😅

kachkaev/njt#29

Screenshot 2021-08-07 at 12 45 52

Screenshot 2021-08-07 at 12 46 11

@kachkaev
Copy link

UPD: 👋 from November 2021 😅

https://github.com/snyk/snyk/issues/1518#issuecomment-974822508

Screenshot 2021-11-21 at 13 42 53

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.

[🐛] yarn 2 lock file parsing issues
6 participants