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 (+) In vendored crates semver metadata #2579

Merged
merged 31 commits into from
Apr 5, 2024

Conversation

ericmcbride
Copy link
Contributor

@ericmcbride ericmcbride commented Mar 27, 2024

  • Resolves Vendoring crates with a "+" in the version breaks the deps graph #2524
  • Revendored examples deps.
  • Semver Metadata can break vendored dependencies, by cargo vendoring (foo-1.2.3+1.2.3) for example. The bazel labels will look for foo-1.2.3-1.2.3 which does not work.
  • Root of the issue is, rules rust uses cargo to vendor the dependencies, which takes the semver + metadata, and generates the file path based off that (hence the + for the dependency in the issue)
  • Tested by taking that dependency and make sure it loaded using the vendor_local_manifests example
  • Did not include that dependency, because it would introduce a sys dep in CI
  • Opted to do it within the scope of the vendor cli. When trying to change the version in the render_build_files call, it fixes the version in terms of the outputs and files declared in the vendor cli file, but then we do not have a handle to the generated vendor files from cargo, meaning we cant move the contents over near as easily. I also didn't want to break anything downstream that may rely on that info.

@ericmcbride ericmcbride changed the title Fix (+) In vendored crates semver metadata DRAFT: Fix (+) In vendored crates semver metadata Mar 27, 2024
@ericmcbride ericmcbride changed the title DRAFT: Fix (+) In vendored crates semver metadata Fix (+) In vendored crates semver metadata Mar 27, 2024
.expect("All file paths should have valid directories")
.to_str()
.expect("All file paths should be strings");
if original_path_str.contains('+') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are worried about someones path on their computer having a + sign in it? If so i may need to not re use the sanitize function

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not have this logic happen in the place where we decide the output name?
https://github.com/bazelbuild/rules_rust/blob/0.41.0/crate_universe/src/rendering.rs#L286

That way we don't need to bother with an additional file op and this code function would be unchanged, right?

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 think I tried this and it had side effects somewhere . Let me change this line and make sure. Im all for smaller code changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you just change the label, it will put the build file in the correct path, but the Crate itself will be stored in the + file path, due to how cargo vendors the dep. I tried this my first attempt, and thats how I ended up with my current solution. We use cargo to run the vendor command, and it will auto generate the semver metadata with the plus sign

libbpf-sys-1.3.0+v1.3.0 
libbpf-sys-1.3.0-v1.3.0
>  ls libbpf-sys-1.3.0+v1.3.0 
bindings.h  build.rs  Cargo.toml  elfutils  libbpf  LICENSE  README.md  rebuild.sh  src  tests  zlib
>  ls libbpf-sys-1.3.0-v1.3.0 
BUILD.bazel

Copy link
Collaborator

Choose a reason for hiding this comment

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

What if you did the rename here?

https://github.com/bazelbuild/rules_rust/blob/0.41.0/crate_universe/src/rendering.rs#L292

If all this new code is doing is renaming a file, it seems like we could update the code to have a correct name in the first place. If the label shouldn't change then perhaps we can update just the output file? But the thing I'd like to avoid is renaming a file. We should be producing the correct data before writing.

Copy link
Contributor Author

@ericmcbride ericmcbride Apr 3, 2024

Choose a reason for hiding this comment

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

I agree with you 100 percent, but changing those labels only change the files for the BUILD files to be generated in.

https://github.com/bazelbuild/rules_rust/blob/0.41.1/crate_universe/src/metadata.rs#L423
This line of code the rules are using Cargo to generate versioned directories. This is where the libbpf-sys-1.3.0+v1.3.0 is coming from, not the rules or rendering themselves.

https://doc.rust-lang.org/cargo/commands/cargo-vendor.html

I dont see any options in there to allow the changing of the names. Unless im missing something here, the Cargo.lock file and cargo command is the source of truth here when generating the vendored, versioned directories and thats why I had to add that code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

AAAAH! I see now!! Thank you for helping me and for being so patient!

In this case, could you make a new function that does the renames? I'd totally forgotten cargo is what writes the directory names. I think it would be much clearer to future readers if there was something that consumed the outputs value from the render, and then looked to rename existing directories as well as mutate the target output path and we hand that off to write_outputs just as we do today. That rename function could have a nice and noticeable docstring that can communicate what's going on.

Hopefully this isn't too much of a detour but if I know myself I'll forget about this in a month and be just as confused for similar changes 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome, I will get on it! And no problem. Took me a while to grok it too. I went down the exact same path as you so I know exactly the thought pattern you were going down 😅

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

Thanks! Could you also add a unit test for this? 🙏

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

Thank you! I had just one question

.expect("All file paths should have valid directories")
.to_str()
.expect("All file paths should be strings");
if original_path_str.contains('+') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not have this logic happen in the place where we decide the output name?
https://github.com/bazelbuild/rules_rust/blob/0.41.0/crate_universe/src/rendering.rs#L286

That way we don't need to bother with an additional file op and this code function would be unchanged, right?

.expect("All file paths should have valid directories")
.to_str()
.expect("All file paths should be strings");
if original_path_str.contains('+') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

AAAAH! I see now!! Thank you for helping me and for being so patient!

In this case, could you make a new function that does the renames? I'd totally forgotten cargo is what writes the directory names. I think it would be much clearer to future readers if there was something that consumed the outputs value from the render, and then looked to rename existing directories as well as mutate the target output path and we hand that off to write_outputs just as we do today. That rename function could have a nice and noticeable docstring that can communicate what's going on.

Hopefully this isn't too much of a detour but if I know myself I'll forget about this in a month and be just as confused for similar changes 😅

outputs: BTreeMap<PathBuf, String>,
out_dir: &Path,
) -> BTreeMap<PathBuf, String> {
outputs
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 moved the re mapping of the outputs from write_outputs into this function, that way the iteration still only happens one time. So it combines

    let outputs: BTreeMap<PathBuf, String> = outputs
        .into_iter()
        .map(|(path, content)| (out_dir.join(path), content))
        .collect();

With the renaming of the files if needed.

if nothing needs to be renamed, it simply just does the out_dir join, previously in the write_outputs function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Example of SemVer change

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

Awesome! Thank you so much!

auto-merge was automatically disabled April 5, 2024 14:47

Head branch was pushed to by a user without write access

@UebelAndre UebelAndre added this pull request to the merge queue Apr 5, 2024
Merged via the queue into bazelbuild:main with commit 1a053cf Apr 5, 2024
3 checks passed
rrbutani pushed a commit to bazel-contrib/toolchains_llvm that referenced this pull request Apr 10, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [rules_rust](https://togithub.com/bazelbuild/rules_rust) |
http_archive | minor | `0.41.1` -> `0.42.0` |

---

### Release Notes

<details>
<summary>bazelbuild/rules_rust (rules_rust)</summary>

###
[`v0.42.0`](https://togithub.com/bazelbuild/rules_rust/releases/tag/0.42.0)

[Compare
Source](https://togithub.com/bazelbuild/rules_rust/compare/0.41.1...0.42.0)

### 0.42.0

```python
load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_rust",
    integrity = "sha256-XT1YVJ6FHJTXBr1v3px2fV37/OCS3dQk3ul+XvfIIf8=",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.42.0/rules_rust-v0.42.0.tar.gz"],
)
```

Additional documentation can be found at:
https://bazelbuild.github.io/rules_rust/#setup

#### What's Changed

- Fix crates.io URL by
[@&#8203;ericmcbride](https://togithub.com/ericmcbride) in
[bazelbuild/rules_rust#2597
- Add support for `--compile_one_dependency` by
[@&#8203;william-smith-skydio](https://togithub.com/william-smith-skydio)
in
[bazelbuild/rules_rust#2598
- Update rules_apple by
[@&#8203;sgowroji](https://togithub.com/sgowroji) in
[bazelbuild/rules_rust#2602
- Support building more things with bzlmod by
[@&#8203;matts1](https://togithub.com/matts1) in
[bazelbuild/rules_rust#2601
- Make bazel lock file cross-platform by
[@&#8203;cameron-martin](https://togithub.com/cameron-martin) in
[bazelbuild/rules_rust#2453
- Added Rust 1.77.1 by
[@&#8203;UebelAndre](https://togithub.com/UebelAndre) in
[bazelbuild/rules_rust#2591
- Fix (+) In vendored crates semver metadata by
[@&#8203;ericmcbride](https://togithub.com/ericmcbride) in
[bazelbuild/rules_rust#2579
- Keep default_features parity from bzlmod to workspace by
[@&#8203;Lev1ty](https://togithub.com/Lev1ty) in
[bazelbuild/rules_rust#2606
- clippy: use --cap-lints=warn; apply clippy_flags when
capture_output=True by [@&#8203;goffrie](https://togithub.com/goffrie)
in
[bazelbuild/rules_rust#2451
- Added Rust 1.77.2 by
[@&#8203;UebelAndre](https://togithub.com/UebelAndre) in
[bazelbuild/rules_rust#2608
- Re-vendor crate_universe outputs by
[@&#8203;UebelAndre](https://togithub.com/UebelAndre) in
[bazelbuild/rules_rust#2609
- Release 0.42.0 by
[@&#8203;UebelAndre](https://togithub.com/UebelAndre) in
[bazelbuild/rules_rust#2610

#### New Contributors

- [@&#8203;Lev1ty](https://togithub.com/Lev1ty) made their first
contribution in
[bazelbuild/rules_rust#2606

**Full Changelog**:
bazelbuild/rules_rust@0.41.1...0.42.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/bazel-contrib/toolchains_llvm).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yNjkuMiIsInVwZGF0ZWRJblZlciI6IjM3LjI2OS4yIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIn0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.

Vendoring crates with a "+" in the version breaks the deps graph
2 participants