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

Fixing an error when the crate's name mismatches it will panic #170

Closed
wants to merge 1 commit into from

Conversation

yijunyu
Copy link
Contributor

@yijunyu yijunyu commented Jan 5, 2021

This bug fix comes from Huawei's application of cargo-geiger to an internal product. After the fix, the tool can run to the end and produce the Safe Ratio report now.

… unwrap(), instead we can use unwrap_or() to avoid being panic.
Comment on lines 59 to 66
self.name
== krates
.get_package_name_from_cargo_metadata_package_id(&package_id)
.unwrap()
.unwrap_or(package_id.to_string())
//.unwrap()
&& self.req.matches(
&krates
.get_package_version_from_cargo_metadata_package_id(
Copy link
Contributor

@anderejd anderejd Jan 5, 2021

Choose a reason for hiding this comment

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

@yijunyu Thanks for the fix, did you find the root cause of the problem? Why is the get_package_name_from_cargo_metadata_package_id function failing to return the package name?

@jmcconnell26 Any ideas?

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, @yijunyu, could you provide the package_id that fails / a link to a repository to scan that fails?
If so, I can try and write a few more test cases to catch behaviour like this.
I would like to go back at some point, and add some further error handling to this part of the code.

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've added a print statement before the line,

 59         println!("package_id: {:?}", package_id.to_string());
 60         self.name
 61             == krates
 62                 .get_package_name_from_cargo_metadata_package_id(&package_id)
 63                 .unwrap()

It turns out that the crate that caused the error is wasm-bindgen.
This can be confirmed if you run cargo-geiger with this crate at https://github.com/rustwasm/wasm-bindgen.
I was trying to debug into this, the error is likely from .cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/krates-0.5.0/src/lib.rs, but I am not too sure about it.
Indeed if I used the unwrap_or_default instead, it will skip the wasm-bindgen crate, which is not what we wanted.
What will be the better way to fix the error?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the info @yijunyu! Taking a look at the issue, something seems strange with the dependencies being added to the graph, but only when running with wasm-bindgen as the root of the crate. If wasm-bindgen is added as a dependency in a Cargo.toml, it looks to generate the report correctly.
I'll take a deeper look at this, and try to figure out what is causing the root problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for looking into this: we will test it again later.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jmcconnell26 Any news? Just curious, no stress 😃

Copy link
Contributor

Choose a reason for hiding this comment

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

@anderejd, apologies, I've been so busy lately I haven't had a chance to do half the things I've been meaning to! I may take a look at issue #175 first, and then come back to this one if you agree?

Copy link
Contributor

Choose a reason for hiding this comment

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

No problem :) Take your time and thanks for #178!

Copy link
Contributor

Choose a reason for hiding this comment

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

I've been trying to replicate this behaviour in a new test project, and have been stumped. I can see it happening in the wasm-bindgen crate, but attempting to create a new crate with the same error fails.
@anderejd, if its OK with you, I'd like to land a few PR's cleaning up the error handling, and hopefully that will lead me to the root of the problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sound great to me, go for it!

@Shnatsel
Copy link

Shnatsel commented Feb 2, 2021

Could this be caused by renaming dependencies in Cargo.toml? rust-lang/cargo#5653

@anderejd
Copy link
Contributor

anderejd commented Feb 4, 2021

Or perhaps related to patching crate versions in Cargo.toml?

@mleonhard mleonhard mentioned this pull request Apr 14, 2021
@pinkforest
Copy link
Collaborator

pinkforest commented Jan 6, 2022

Hey all - this has been hanging around

Just to get some closure on this -

We've had bunch of cargo bumps and we are using the latest 0.58.0 as dep (#230) that ended into current 0.11.2 released today as well as git version.

Sadly - The error message has been already addressed elsewhere by @jmcconnell26

#188

commit 0f5ba7ba8f000821c61e18829beebb260bcfd503
Author: -
Date:   Thu Feb 4 19:27:16 2021 +0000

    NOISSUE - Clean up error handling, remove unwrap() calls, logging
    meaningful messages

It cleaned out those unwraps in general including this as indicated here

Just for the sake of completenes I ran with the latest 0.11.2 cargo-geiger that uses 0.58.0 cargo

  1. docker run -ti --rm rust /bin/bash
  2. cargo install cargo-geiger --git https://github.com/rust-secure-code/cargo-geiger --force
  3. mkdir app ; cd app
  4. git clone https://github.com/rustwasm/wasm-bindgen.git .
  5. cargo geiger
Functions  Expressions  Impls  Traits  Methods  Dependency

12/23      432/583      16/17  2/2     9/12     !  wasm-bindgen 0.2.78
0/0        0/0          0/0    0/0     0/0      ?  ├── cfg-if 1.0.0
0/0        0/4          0/0    0/0     0/0      ?  ├── serde 1.0.133
0/0        0/0          0/0    0/0     0/0      ?  │   └── serde_derive 1.0.133
0/0        12/12        0/0    0/0     3/3      !  │       ├── proc-macro2 1.0.36
0/0        0/0          0/0    0/0     0/0      :) │       │   └── unicode-xid 0.2.2
0/0        0/0          0/0    0/0     0/0      ?  │       ├── quote 1.0.14
0/0        12/12        0/0    0/0     3/3      !  │       │   └── proc-macro2 1.0.36
0/0        47/47        3/3    0/0     2/2      !  │       └── syn 1.0.84
0/0        12/12        0/0    0/0     3/3      !  │           ├── proc-macro2 1.0.36
0/0        0/0          0/0    0/0     0/0      ?  │           ├── quote 1.0.14
0/0        0/0          0/0    0/0     0/0      :) │           └── unicode-xid 0.2.2
0/0        0/7          0/0    0/0     0/0      ?  ├── serde_json 1.0.74
0/0        0/7          0/0    0/0     0/0      ?  │   ├── itoa 1.0.1
0/9        0/723        0/0    0/0     0/2      ?  │   ├── ryu 1.0.9
0/0        0/4          0/0    0/0     0/0      ?  │   └── serde 1.0.133
0/1        0/0          0/1    0/0     0/1      ?  └── wasm-bindgen-macro 0.2.78
0/0        0/0          0/0    0/0     0/0      ?      ├── quote 1.0.14
0/0        0/0          0/0    0/0     0/0      ?      └── wasm-bindgen-macro-support 0.2.78
0/0        12/12        0/0    0/0     3/3      !          ├── proc-macro2 1.0.36
0/0        0/0          0/0    0/0     0/0      ?          ├── quote 1.0.14
0/0        47/47        3/3    0/0     2/2      !          ├── syn 1.0.84
0/0        0/0          0/0    0/0     0/0      ?          ├── wasm-bindgen-backend 0.2.78
4/6        389/1102     3/9    1/1     12/25    !          │   ├── bumpalo 3.9.0
0/0        7/7          1/1    0/0     0/0      !          │   ├── lazy_static 1.4.0
1/1        16/16        1/1    0/0     0/0      !          │   ├── log 0.4.14
0/0        0/0          0/0    0/0     0/0      ?          │   │   ├── cfg-if 1.0.0
0/0        0/4          0/0    0/0     0/0      ?          │   │   └── serde 1.0.133
0/0        12/12        0/0    0/0     3/3      !          │   ├── proc-macro2 1.0.36
0/0        0/0          0/0    0/0     0/0      ?          │   ├── quote 1.0.14
0/0        47/47        3/3    0/0     2/2      !          │   ├── syn 1.0.84
0/0        0/0          0/0    0/0     0/0      ?          │   └── wasm-bindgen-shared 0.2.78
0/0        0/0          0/0    0/0     0/0      ?          └── wasm-bindgen-shared 0.2.78

17/40      903/2508     24/32  3/3     26/45  

(I get bunch of warnings but those are unrelated ofc.)

I will close this PR so it's not hanging dead -

Many thanks anyways @yijunyu for the effort

Also everyone else - thanks for the effort anyways 💜

@pinkforest pinkforest closed this Jan 6, 2022
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.

5 participants