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

"cargo metadata" no longer displays "example" kind if example specifies crate-type #3654

Closed
ehuss opened this issue Feb 6, 2017 · 23 comments · Fixed by #3668
Closed

"cargo metadata" no longer displays "example" kind if example specifies crate-type #3654

ehuss opened this issue Feb 6, 2017 · 23 comments · Fixed by #3668

Comments

@ehuss
Copy link
Contributor

ehuss commented Feb 6, 2017

As of /pull/3556, the cargo metadata command does not output the "kind" as "example" if the example specifies a crate-type. Sample Cargo.toml:

[package]
name = "scratch"
version = "0.0.0"

[[example]]
name = "elib1"
path = "examples/elib1.rs"
crate-type=["staticlib"]

I am working on a tool that uses the metadata output to determine which command-line arguments to pass to Cargo in order to build a target. Without knowing this is an example, it is impossible to know to pass --example.

I am unsure of what the correct solution is. Some ideas:

  • "kind" can display "example" even if it is an ExampleLib.
  • Include an "is_example" field.
  • Include an "args" field that indicates how to build this target.
@jethrogb
Copy link
Contributor

jethrogb commented Feb 6, 2017

Duplicate of #3572 Edit: wait. This seems to be the inverse of #3572?

@KalitaAlexey
Copy link
Contributor

KalitaAlexey commented Feb 6, 2017

@alexcrichton,
I think kind should still show "example".
If it is right, I will change that. It is easy change.

Do we have tests for cargo-metadata?
None of tests failed.
I can write some tests.

@jethrogb
Copy link
Contributor

jethrogb commented Feb 6, 2017

I think metadata should show both type of output and the information required to determine the correct cargo cmd line.

@KalitaAlexey
Copy link
Contributor

KalitaAlexey commented Feb 6, 2017

@jethrogb,
You can't execute cargo run --example if an example is a library so we might need something new, i.e., examplelib.

@jethrogb
Copy link
Contributor

jethrogb commented Feb 6, 2017

But you can execute build, rustc, or test with that flag.

@KalitaAlexey
Copy link
Contributor

But I would rather prefer to add a field example which is either true or false.

@jethrogb
Copy link
Contributor

jethrogb commented Feb 6, 2017

I think that would be sufficient?

@KalitaAlexey
Copy link
Contributor

@jethrogb,
Yes it would since it allows to determine whether it is an example or not and to determine what it produces.

@alexcrichton
Copy link
Member

I posted on the PR as well but I think we should revert the breaking change here and just extend the metadata format to otherwise accommodate examples-as-libraries in metadata.

@KalitaAlexey would you be willing to prepare such a PR?

@KalitaAlexey
Copy link
Contributor

@alexcrichton,
I will send a PR today (in 3 hours).

@KalitaAlexey
Copy link
Contributor

@alexcrichton,
It is not a breaking change because we didn't accept crate-type for an example.
It seems that cargo metadata just reads crate-type.
I can specify crate-type = ["e"] and it prints it.
So I didn't break anything.
This is why I think we should add a field "is_example" of boolean.
"kind" is what specified in crate-type.
What do you think about it?

@jethrogb
Copy link
Contributor

jethrogb commented Feb 7, 2017

Sure it is a breaking change. Before it was kind: ["example"], now it's not.

@KalitaAlexey
Copy link
Contributor

@jethrogb,
If you don't specify any crate-type it will be "kind": ["example"].

@alexcrichton
Copy link
Member

@KalitaAlexey yeah this was a breaking change from before because existing code has broken. I think "kind" was probably a misnomer from before, so we should leave "kind" as it was and then add a separate "crate-types" field which has the information about crate types.

@KalitaAlexey
Copy link
Contributor

@alexcrichton,
Okay.
I will find the solution.

@alexcrichton
Copy link
Member

Thanks @KalitaAlexey

@KalitaAlexey
Copy link
Contributor

@alexcrichton,
I checked the code.
"kind" is actually "crate-type".
I think what can I do with that.

@KalitaAlexey
Copy link
Contributor

@alexcrichton,
I'd prefer to add a field named "is_example".
If we added "crate-type" what values it would have for "kind": "bin"?

@alexcrichton
Copy link
Member

I think we've already got a list of crate types for any target (IIRC there's a method). Something like is_example I believe is already inferrable from kind.

At this point kind was sort of badly named, but there's not much we can do about it now.

@jethrogb
Copy link
Contributor

jethrogb commented Feb 7, 2017

Can we do the opposite and add a field named crate-type which contains the actual crate types? This way we have backwards-compatibility, the ability to determine cargo cmdline flags (through kind), and the information about crate types passed by cargo to rustc (crate-type).

@alexcrichton
Copy link
Member

@jethrogb yes that's what I'd like to do.

@ehuss
Copy link
Contributor Author

ehuss commented Feb 8, 2017

Just FYI, here are some in-the-wild usages of the "kind" metadata, if it will help you see how it is used:

@KalitaAlexey
Copy link
Contributor

Okay.
I will add crate-type which would duplicate kind field for all targets, but example.

bors added a commit that referenced this issue Feb 11, 2017
Reverted the "kind" field to the previous value.

Added a new field named "crate_types".
Fixes #3654
After this PR `cargo metadata` would output:
```json
{
    "packages": [
        {
            "targets": [
                {
                    "kind": ["lib"],
                    "crate_types": ["lib"]
                },
                {
                    "kind": ["example"],
                    "crate_types": ["staticlib"]
                }
            ]
        }
    ]
}
```

I have added tests.
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 a pull request may close this issue.

4 participants