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

Expose rust-version through env var #10713

Merged
merged 1 commit into from
Jun 6, 2022
Merged

Conversation

flip1995
Copy link
Member

This adds another env var that is exposed by cargo. In Clippy we would like to use that in order to efficiently check if a rust-version is set for the current package: rust-lang/rust-clippy#8774

Currently we either have to parse the Cargo.toml file ourselves or use the cargo_metadata crate which has a notable performance impact when running clippy-driver on single files.

@rust-highfive
Copy link

r? @ehuss

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 28, 2022
@@ -214,6 +214,7 @@ corresponding environment variable is set to the empty string, `""`.
* `CARGO_PKG_REPOSITORY` — The repository from the manifest of your package.
* `CARGO_PKG_LICENSE` — The license from the manifest of your package.
* `CARGO_PKG_LICENSE_FILE` — The license file from the manifest of your package.
* `CARGO_PKG_RUST_VERSION` — The Rust version from the manifest of your package.
Copy link
Contributor

Choose a reason for hiding this comment

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

The thing going through my head is the question: "what principle governs us exposing the manifest as environment variables?".

For myself, exposing CARGO_PKG_RUST_VERSION feels very arbitrary from a cargo-design perspective. Why this field and not many of the others?

Copy link
Member Author

@flip1995 flip1995 May 29, 2022

Choose a reason for hiding this comment

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

Many of the other fields are already exposed. Like the version field is even exposed through 4 or 5 env vars. Most package metadata is exposed. I think this makes it more consistent.

Copy link

Choose a reason for hiding this comment

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

Also see #8758 where CARGO_PRIMARY_PACKAGE was added "primarily for Clippy".

Copy link
Contributor

Choose a reason for hiding this comment

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

Many of the other fields are already exposed. Like the version field is even exposed through 4 or 5 env vars. Most package metadata is exposed. I think this makes it more consistent.

Except for CARGO_PRIMARY_PACKAGE , those all look targeted at the code being built, either so a crate can access its own metadata or build.rs / test purposes.

Also see #8758 where CARGO_PRIMARY_PACKAGE was added "primarily for Clippy".

Thanks for pointing out precedence on this. I am still interested in seeing if there is an answer to my question or if there is interest in creating an answer to my question. Until that time, I am not comfortable merging this though maybe others of the cargo team would be.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm good with waiting on other team members. Just wanted to bring this up through a PR again, because my question some time ago on Zulip didn't get any replies 😅

Copy link
Member

@joshtriplett joshtriplett May 31, 2022

Choose a reason for hiding this comment

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

Suggested change
* `CARGO_PKG_RUST_VERSION` — The Rust version from the manifest of your package.
* `CARGO_PKG_RUST_VERSION` — The Rust version from the manifest of your package. Note that this is the minimum Rust version supported by the package, not the current Rust version.

(People regularly want Rust compiler version information, so I want to make sure people don't think this is the thing they're looking for for that.)

Copy link
Contributor

Choose a reason for hiding this comment

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

We discussed this in the cargo team meeting and we have no guiding principle on what env variables should exist and are intending to be responsive to what different teams needs are. I'm fine merging this after the documentation gets improved.

Copy link
Contributor

@epage epage May 31, 2022

Choose a reason for hiding this comment

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

I'm surprised we don't link any of these fields to their manifest field's documentation. That would also be a natural way to clarify what this is intending to contain

@est31
Copy link
Member

est31 commented May 30, 2022

Alternative to an environment variable, one could also think about a flag that is passed to the clippy invocation. @epage would you be more okay with a flag?

@est31
Copy link
Member

est31 commented May 30, 2022

Also, the flag would be useful in rustc too. For example, it could give a warning if you are using a library feature that was stabilized after the specified minimum rust version.

@epage
Copy link
Contributor

epage commented May 30, 2022

Alternative to an environment variable, one could also think about a flag that is passed to the clippy invocation. @epage would you be more okay with a flag?

Not dealt enough with that machinery to have much of an opinion.

Also, the flag would be useful in rustc too. For example, it could give a warning if you are using a library feature that was stabilized after the specified minimum rust version.

That'd be slick

@hellow554
Copy link

Also, the flag would be useful in rustc too. For example, it could give a warning if you are using a library feature that was stabilized after the specified minimum rust version.

Not only library features, but deprecated methods/struts/... Would be cool.
Currently you have to write #[allow(deprecated)] which is unpleasant, because you may forget to remove that and use a deprecated function for ever.

I would be down to implement that.

@epage
Copy link
Contributor

epage commented May 31, 2022

To double check my understanding, are we interested in changing the direction of how to solve clippy's problem away from reading a cargo-specific env variable to a general flag for the driver (sorry if I've used the wrong terms) so rustc can also take advantage of this?

@est31
Copy link
Member

est31 commented May 31, 2022

The env var would be available to both rustc and clippy, from my understanding. Of course unless it's implemented in a way that it's only set for cargo clippy. But you could make rustc read the env var as well.

I guess my comment reads like if a flag would enable that, and I'm sorry for the bad formulation. I meant it as general support for having cargo pass it some way to rustc/clippy.

I think a flag would still be better. The env vars are mostly useful for people who want to do env! in their code or want to read it in build.rs, but I don't see any use cases for reading the MSRV. I'm also not sure of precedent in rustc for reading env vars in such a way.

@epage
Copy link
Contributor

epage commented May 31, 2022

. I'm also not sure of precedent in rustc for reading env vars in such a way.

This was my reason for assuming an alternative solution would be needed for rustc. Let us know either way you all go with this.

@est31
Copy link
Member

est31 commented May 31, 2022

Yeah idk if compiler maintainers will accept something that uses an env var. I wonder why clippy needs an env var at all, what are the advantages over an argument from clippy's point of view?

@ghost
Copy link

ghost commented Jun 1, 2022

The advantage of the env var is that it doesn't require changing the compiler and only needs minimal changes to Cargo.

Keep in mind that it took something like 4 years just to get rust-version into the Cargo manifest. The flag can get added when the compiler wants/needs it.

@hellow554
Copy link

hellow554 commented Jun 2, 2022

I'm also in favor of the environment variable for now.

Maybe the documentation should be adjusted, but all in all, for the current situation, an env variable is the simplest solution.

I'm more than willing to adjust it to a command line argument, if needed.

The reason for having it in the first place is pointed out by @flip1995:

Currently we either have to parse the Cargo.toml file ourselves or use the cargo_metadata crate which has a notable performance impact when running clippy-driver on single files.

So in fact, we have to parse Cargo.toml twice which can be avoided and lead to a slight performance improvement. I think adding an environment variable should not be a big performance impact.

@epage
Copy link
Contributor

epage commented Jun 2, 2022

The downside I see to an environment variable, especially one defined in cargo and not in the compiler/clippy, is that it makes the API less clear for people building without cargo. Granted, I don't know how much of a problem that already is and how much we care.

If you all feel this is the route to go, if you get the documentation updated and tests passing, we can merge this.

@est31
Copy link
Member

est31 commented Jun 2, 2022

I've originally believed that the presence of an environment variable was an unchangeable fact due to stability guarantees, but given that programs have to be prepared to the environment variable not being present at all, like when there is no rust-version in the manifest, I guess that it should be possible to remove it in the future in favour of a flag. So there's less pressure to "do it right" I guess...

How do people feel about me submitting a PR to add flags for it instead of an environment variable?

@flip1995
Copy link
Member Author

flip1995 commented Jun 2, 2022

How do people feel about me submitting a PR to add flags for it instead of an environment variable?

A flag would be a way bigger commitment if added to stable Rust. And a -Z flag doesn't really help, because you couldn't pass this to stable Clippy AFAIK. And I think most people use stable toolchains with stable Clippy now.

@flip1995 flip1995 force-pushed the rust-version-env branch from 670de7c to 375d3b3 Compare June 6, 2022 10:02
@flip1995 flip1995 force-pushed the rust-version-env branch from 375d3b3 to 0852f54 Compare June 6, 2022 10:04
@epage
Copy link
Contributor

epage commented Jun 6, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Jun 6, 2022

📌 Commit 0852f54 has been approved by epage

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 6, 2022
@bors
Copy link
Contributor

bors commented Jun 6, 2022

⌛ Testing commit 0852f54 with merge 7d289b1...

@bors
Copy link
Contributor

bors commented Jun 6, 2022

☀️ Test successful - checks-actions
Approved by: epage
Pushing 7d289b1 to master...

@bors bors merged commit 7d289b1 into rust-lang:master Jun 6, 2022
@flip1995 flip1995 deleted the rust-version-env branch June 6, 2022 14:57
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 8, 2022
Update cargo

7 commits in 38472bc19f2f76e245eba54a6e97ee6821b3c1db..85e457e158db216a2938d51bc3b617a5a7fe6015
2022-05-31 02:03:24 +0000 to 2022-06-07 21:57:52 +0000
- Make -Z http-registry use index.crates.io when accessing crates-io (rust-lang/cargo#10725)
- Respect submodule update=none strategy in .gitmodules (rust-lang/cargo#10717)
- Expose rust-version through env var (rust-lang/cargo#10713)
- add validation for string "true"/"false" in lto profile (rust-lang/cargo#10676)
- Enhance documentation of testing (rust-lang/cargo#10726)
- Clear disk space on CI. (rust-lang/cargo#10724)
- Enforce to use tar v0.4.38 (rust-lang/cargo#10720)
@ehuss ehuss added this to the 1.63.0 milestone Jun 22, 2022
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Oct 10, 2022
Pkgsrc changes:
 * Adjust patches as needed & checksum updates.

Upstream changes:

Version 1.63.0 (2022-08-11)
==========================

Language
--------
- [Remove migrate borrowck mode for pre-NLL errors.][95565]
- [Modify MIR building to drop repeat expressions with length zero.][95953]
- [Remove label/lifetime shadowing warnings.][96296]
- [Allow explicit generic arguments in the presence of `impl Trait` args.]
  [96868]
- [Make `cenum_impl_drop_cast` warnings deny-by-default.][97652]
- [Prevent unwinding when `-C panic=abort` is used regardless of
  declared ABI.][96959]
- [lub: don't bail out due to empty binders.][97867]

Compiler
--------
- [Stabilize the `bundle` native library modifier,][95818] also removing the
  deprecated `static-nobundle` linking kind.
- [Add Apple WatchOS compile targets\*.][95243]
- [Add a Windows application manifest to rustc-main.][96737]

\* Refer to Rust's [platform support page][platform-support-doc] for more
   information on Rust's tiered platform support.

Libraries
---------
- [Implement `Copy`, `Clone`, `PartialEq` and `Eq` for
  `core::fmt::Alignment`.][94530]
- [Extend `ptr::null` and `null_mut` to all thin (including extern)
  types.][94954]
- [`impl Read and Write for VecDeque<u8>`.][95632]
- [STD support for the Nintendo 3DS.][95897]
- [Make write/print macros eagerly drop temporaries.][96455]
- [Implement internal traits that enable `[OsStr]::join`.][96881]
- [Implement `Hash` for `core::alloc::Layout`.][97034]
- [Add capacity documentation for `OsString`.][97202]
- [Put a bound on collection misbehavior.][97316]
- [Make `std::mem::needs_drop` accept `?Sized`.][97675]
- [`impl Termination for Infallible` and then make the `Result` impls
  of `Termination` more generic.][97803]
- [Document Rust's stance on `/proc/self/mem`.][97837]

Stabilized APIs
---------------

- [`array::from_fn`]
- [`Box::into_pin`]
- [`BinaryHeap::try_reserve`]
- [`BinaryHeap::try_reserve_exact`]
- [`OsString::try_reserve`]
- [`OsString::try_reserve_exact`]
- [`PathBuf::try_reserve`]
- [`PathBuf::try_reserve_exact`]
- [`Path::try_exists`]
- [`Ref::filter_map`]
- [`RefMut::filter_map`]
- [`NonNull::<[T]>::len`][`NonNull::<slice>::len`]
- [`ToOwned::clone_into`]
- [`Ipv6Addr::to_ipv4_mapped`]
- [`unix::io::AsFd`]
- [`unix::io::BorrowedFd<'fd>`]
- [`unix::io::OwnedFd`]
- [`windows::io::AsHandle`]
- [`windows::io::BorrowedHandle<'handle>`]
- [`windows::io::OwnedHandle`]
- [`windows::io::HandleOrInvalid`]
- [`windows::io::HandleOrNull`]
- [`windows::io::InvalidHandleError`]
- [`windows::io::NullHandleError`]
- [`windows::io::AsSocket`]
- [`windows::io::BorrowedSocket<'handle>`]
- [`windows::io::OwnedSocket`]
- [`thread::scope`]
- [`thread::Scope`]
- [`thread::ScopedJoinHandle`]

These APIs are now usable in const contexts:

- [`array::from_ref`]
- [`slice::from_ref`]
- [`intrinsics::copy`]
- [`intrinsics::copy_nonoverlapping`]
- [`<*const T>::copy_to`]
- [`<*const T>::copy_to_nonoverlapping`]
- [`<*mut T>::copy_to`]
- [`<*mut T>::copy_to_nonoverlapping`]
- [`<*mut T>::copy_from`]
- [`<*mut T>::copy_from_nonoverlapping`]
- [`str::from_utf8`]
- [`Utf8Error::error_len`]
- [`Utf8Error::valid_up_to`]
- [`Condvar::new`]
- [`Mutex::new`]
- [`RwLock::new`]

Cargo
-----
- [Stabilize the `--config path` command-line argument.][cargo/10755]
- [Expose rust-version in the environment as
  `CARGO_PKG_RUST_VERSION`.][cargo/10713]

Compatibility Notes
-------------------

- [`#[link]` attributes are now checked more strictly,][96885]
  which may introduce errors for invalid attribute arguments that
  were previously ignored.

Internal Changes
----------------

These changes provide no direct user facing benefits, but represent
significant improvements to the internals and overall performance
of rustc and related tools.

- [Prepare Rust for LLVM opaque pointers.][94214]

[94214]: rust-lang/rust#94214
[94530]: rust-lang/rust#94530
[94954]: rust-lang/rust#94954
[95243]: rust-lang/rust#95243
[95565]: rust-lang/rust#95565
[95632]: rust-lang/rust#95632
[95818]: rust-lang/rust#95818
[95897]: rust-lang/rust#95897
[95953]: rust-lang/rust#95953
[96296]: rust-lang/rust#96296
[96455]: rust-lang/rust#96455
[96737]: rust-lang/rust#96737
[96868]: rust-lang/rust#96868
[96881]: rust-lang/rust#96881
[96885]: rust-lang/rust#96885
[96959]: rust-lang/rust#96959
[97034]: rust-lang/rust#97034
[97202]: rust-lang/rust#97202
[97316]: rust-lang/rust#97316
[97652]: rust-lang/rust#97652
[97675]: rust-lang/rust#97675
[97803]: rust-lang/rust#97803
[97837]: rust-lang/rust#97837
[97867]: rust-lang/rust#97867
[cargo/10713]: rust-lang/cargo#10713
[cargo/10755]: rust-lang/cargo#10755

[`array::from_fn`]: https://doc.rust-lang.org/stable/std/array/fn.from_fn.html
[`Box::into_pin`]: https://doc.rust-lang.org/stable/std/boxed/struct.Box.html#method.into_pin
[`BinaryHeap::try_reserve_exact`]: https://doc.rust-lang.org/stable/alloc/collections/binary_heap/struct.BinaryHeap.html#method.try_reserve_exact
[`BinaryHeap::try_reserve`]: https://doc.rust-lang.org/stable/std/collections/struct.BinaryHeap.html#method.try_reserve
[`OsString::try_reserve`]: https://doc.rust-lang.org/stable/std/ffi/struct.OsString.html#method.try_reserve
[`OsString::try_reserve_exact`]: https://doc.rust-lang.org/stable/std/ffi/struct.OsString.html#method.try_reserve_exact
[`PathBuf::try_reserve`]: https://doc.rust-lang.org/stable/std/path/struct.PathBuf.html#method.try_reserve
[`PathBuf::try_reserve_exact`]: https://doc.rust-lang.org/stable/std/path/struct.PathBuf.html#method.try_reserve_exact
[`Path::try_exists`]: https://doc.rust-lang.org/stable/std/path/struct.Path.html#method.try_exists
[`Ref::filter_map`]: https://doc.rust-lang.org/stable/std/cell/struct.Ref.html#method.filter_map
[`RefMut::filter_map`]: https://doc.rust-lang.org/stable/std/cell/struct.RefMut.html#method.filter_map
[`NonNull::<slice>::len`]: https://doc.rust-lang.org/stable/std/ptr/struct.NonNull.html#method.len
[`ToOwned::clone_into`]: https://doc.rust-lang.org/stable/std/borrow/trait.ToOwned.html#method.clone_into
[`Ipv6Addr::to_ipv4_mapped`]: https://doc.rust-lang.org/stable/std/net/struct.Ipv6Addr.html#method.to_ipv4_mapped
[`unix::io::AsFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsFd.html
[`unix::io::BorrowedFd<'fd>`]: https://doc.rust-lang.org/stable/std/os/unix/io/struct.BorrowedFd.html
[`unix::io::OwnedFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/struct.OwnedFd.html
[`windows::io::AsHandle`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.AsHandle.html
[`windows::io::BorrowedHandle<'handle>`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.BorrowedHandle.html
[`windows::io::OwnedHandle`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.OwnedHandle.html
[`windows::io::HandleOrInvalid`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.HandleOrInvalid.html
[`windows::io::HandleOrNull`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.HandleOrNull.html
[`windows::io::InvalidHandleError`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.InvalidHandleError.html
[`windows::io::NullHandleError`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.NullHandleError.html
[`windows::io::AsSocket`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.AsSocket.html
[`windows::io::BorrowedSocket<'handle>`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.BorrowedSocket.html
[`windows::io::OwnedSocket`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.OwnedSocket.html
[`thread::scope`]: https://doc.rust-lang.org/stable/std/thread/fn.scope.html
[`thread::Scope`]: https://doc.rust-lang.org/stable/std/thread/struct.Scope.html
[`thread::ScopedJoinHandle`]: https://doc.rust-lang.org/stable/std/thread/struct.ScopedJoinHandle.html

[`array::from_ref`]: https://doc.rust-lang.org/stable/std/array/fn.from_ref.html
[`slice::from_ref`]: https://doc.rust-lang.org/stable/std/slice/fn.from_ref.html
[`intrinsics::copy`]: https://doc.rust-lang.org/stable/std/intrinsics/fn.copy.html
[`intrinsics::copy_nonoverlapping`]: https://doc.rust-lang.org/stable/std/intrinsics/fn.copy_nonoverlapping.html
[`<*const T>::copy_to`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.copy_to
[`<*const T>::copy_to_nonoverlapping`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.copy_to_nonoverlapping
[`<*mut T>::copy_to`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.copy_to-1
[`<*mut T>::copy_to_nonoverlapping`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.copy_to_nonoverlapping-1
[`<*mut T>::copy_from`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.copy_from
[`<*mut T>::copy_from_nonoverlapping`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.copy_from_nonoverlapping
[`str::from_utf8`]: https://doc.rust-lang.org/stable/std/str/fn.from_utf8.html
[`Utf8Error::error_len`]: https://doc.rust-lang.org/stable/std/str/struct.Utf8Error.html#method.error_len
[`Utf8Error::valid_up_to`]: https://doc.rust-lang.org/stable/std/str/struct.Utf8Error.html#method.valid_up_to
[`Condvar::new`]: https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html#method.new
[`Mutex::new`]: https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html#method.new
[`RwLock::new`]: https://doc.rust-lang.org/stable/std/sync/struct.RwLock.html#method.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants