rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.59k stars 2.39k forks source link

`cargo fix` support for `exported-private-dependencies` #13095

Open epage opened 9 months ago

epage commented 9 months ago

Problem

rust-lang/rfcs#3516 calls for making exported-private-dependencies into an error in new editions. This means it needs to be machine-applicable so cargo fix can migrate it.

The problem is the most useful thing for a machine-applicable fix is to update Cargo.toml but rustc has no knowledge of that.

Proposed Solution

Intercept the message from rustc, before cargo fix (or the "machine applicable lints counter) sees this and add the machine-applicable content.

Notes

No response

epage commented 9 months ago

What we need is

Looking at the compiler message

{
  "reason": "compiler-message",
  "package_id": "cargo-pub-priv 0.1.0 (path+file:///home/epage/src/personal/dump/cargo-pub-priv)",
  "manifest_path": "/home/epage/src/personal/dump/cargo-pub-priv/Cargo.toml",
  "target": {
    "kind": [
      "lib"
    ],
    "crate_types": [
      "lib"
    ],
    "name": "cargo-pub-priv",
    "src_path": "/home/epage/src/personal/dump/cargo-pub-priv/src/lib.rs",
    "edition": "2021",
    "doc": true,
    "doctest": true,
    "test": true
  },
  "message": {
    "rendered": "warning: trait `FooTrait` from private dependency 'foo' in public interface\n  --> src/lib.rs:11:1\n   |\n11 | pub trait UseFoo: foo::FooTrait {}\n   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n",
    "children": [],
    "code": {
      "code": "exported_private_dependencies",
      "explanation": null
    },
    "level": "warning",
    "message": "trait `FooTrait` from private dependency 'foo' in public interface",
    "spans": [
      {
        "byte_end": 189,
        "byte_start": 158,
        "column_end": 32,
        "column_start": 1,
        "expansion": null,
        "file_name": "src/lib.rs",
        "is_primary": true,
        "label": null,
        "line_end": 11,
        "line_start": 11,
        "suggested_replacement": null,
        "suggestion_applicability": null,
        "text": [
          {
            "highlight_end": 32,
            "highlight_start": 1,
            "text": "pub trait UseFoo: foo::FooTrait {}"
          }
        ]
      }
    ]
  }
}

We have the code but not the dependency name. It'd be ugly and brittle but we could parse the dependency name out of "from private dependency '{name}' in public interface". We ship with rustc so we can make sure this stays up to date as rustc changes, though it'll cause CI failures.

Expectation

epage commented 9 months ago

Depending on the details, we might need #13096

linyihai commented 8 months ago

Is it currently not supported to obtain the specific line number of a certain configuration item? Give a example, I like to know the line of license and regex in the Cargo.toml.

image

It looks like this issue need this ability to fixed.

And some discussiton can be found in zulip is it possible to include position data for cargo metadata

epage commented 8 months ago

@Muscraft was going to add code to cargo to get the needed spans in a follow up to #13172. If you need it before, reach out to them for notes they have on the topic

linyihai commented 8 months ago

@Muscraft was going to add code to cargo to get the needed spans in a follow up to #13172. If you need it before, reach out to them for notes they have on the topic

https://github.com/rust-lang/cargo/pull/13172 was merged recently. I'd to take a shot :-) .

epage commented 8 months ago

Note that I was referring to a PR they'll be working on after that PR. That PR doesn't have the functionality you would need.