Skip to content

Restrict sysroot crate imports to those defined in this repo. #143548

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Diggsey
Copy link
Contributor

@Diggsey Diggsey commented Jul 6, 2025

It's common to import dependencies from the sysroot via extern crate rather than use an explicit cargo dependency, when it's necessary to use the same dependency version as used by rustc itself. However, this is dangerous for crates.io crates, since rustc may not pull in the dependency on some targets, or may pull in multiple versions. In both cases, the extern crate fails to resolve.

To address this, re-export all such dependencies from the appropriate rustc_* crates, and use this alias from crates which would otherwise need to use extern crate.

See #143492 for an example of the kind of issue that can occur.

@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-compiletest Area: The compiletest test runner A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-rustdoc-json Area: Rustdoc JSON backend A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 6, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in src/tools/compiletest

cc @jieyouxu

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in compiler/rustc_attr_data_structures

cc @jdonszelmann

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @TaKO8Ki

tracing = { version = "0.1.37", default-features = false, features = ["std"] }
tracing = { workspace = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a valid change for rustfmt. Although rustfmt is part of the r-l/rust workspace, it's independently developed in https://github.com/rust-lang/rustfmt, and I'm pretty sure this would prevent us from build from source when working on the git subtree, which isn't structured as a cargo workspace. I'd like to opt out of all rustfmt changes from this PR, but feel free to open a new PR directly to https://github.com/rust-lang/rustfmt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, will update 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ytmimi is it sufficient to revert the Cargo.toml changes, or do the other changes need to go via the other repo as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, there are some other workspace members that are also subtrees, like clippy, and miri. I didn't look at any tools besides rustfmt, but it's possible maintainers of those tools would prefer PRs be opened up directly to their respective repos as well.

Copy link
Contributor Author

@Diggsey Diggsey Jul 6, 2025

Choose a reason for hiding this comment

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

@ytmimi happy to split if needed but the wording you linked says:

The exception is that when rustc changes are required to implement a new tool feature or test, that should happen in one collective rustc PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ytmimi is it sufficient to revert the Cargo.toml changes, or do the other changes need to go via the other repo as well?

I'd rather that all changes were reverted, thanks!

Copy link
Member

@RalfJung RalfJung Jul 7, 2025

Choose a reason for hiding this comment

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

Using workspace dependencies for the Cargo.toml files in compiler (except in codegen_gcc and codegen_clf) is entirely fine, and a good idea for the reasons you mention. Only the subtree crates like Miri, clippy, rustfmt cannot use workspace dependencies.

The root problem here is not the use of workspace dependencies, the problem is that rustfmt has tracing in its Cargo.toml, which it shouldn't. It should use rustc's tracing instead.

Copy link
Member

@RalfJung RalfJung Jul 7, 2025

Choose a reason for hiding this comment

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

Ah, turns out workspace dependencies do not work since the workspace Cargo.toml is not distributed in the rustc-dev package... see Zulip.

It's common to import dependencies from the sysroot via `extern crate`
rather than use an explicit cargo dependency, when it's necessary to use
the same dependency version as used by rustc itself. However, this is
dangerous for crates.io crates, since rustc may not pull in the
dependency on some targets, or may pull in multiple versions. In both
cases, the `extern crate` fails to resolve.

To address this, re-export all such dependencies from the appropriate
`rustc_*` crates, and use this alias from crates which would otherwise
need to use `extern crate`.
@Diggsey Diggsey force-pushed the db-limit-extern-crate-usage branch from 8e8dd82 to c613bce Compare July 6, 2025 22:54
@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
 use rustc_span::{Span, sym};
-use rustc_data_structures::thin_vec::ThinVec;
 
 use crate::Input;
 use crate::parse::session::ParseSess;
{ "type": "test", "name": "test::self_tests", "event": "failed", "stdout": "Ran 8 self tests.\n\nthread 'test::self_tests' panicked at src/tools/rustfmt/src/test/mod.rs:420:5:\nassertion `left == right` failed: 1 self tests failed\n  left: 1\n right: 0\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n" }
test test::system_tests ... ok
test test::idempotence_tests ... ok

test result: FAILED. 181 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 2.19s

Comment on lines -29 to -30
// These crates are pulled from the sysroot because they are part of
// rustc's public API, so we need to ensure version compatibility.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this version compatibility still a concern?
I'm not sure what it's referring to, but I just want to make sure that removing this is not going to break something else.

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 added this comment in a previous PR as essentially a TODO for this PR. Now that crates are re-exported there's no need to pull them by name from the sysroot in order to ensure the versions match.

use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_errors::{Diag, DiagMessage};
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::TyCtxt;
pub(crate) use rustc_resolve::rustdoc::main_body_opts;
use rustc_resolve::rustdoc::may_be_doc_link;
use rustc_resolve::rustdoc::pulldown_cmark::{
self, BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, html,
};
use rustc_span::edition::Edition;
use rustc_span::{Span, Symbol};
use tracing::{debug, trace};
Copy link
Member

Choose a reason for hiding this comment

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

There are still tracing imports all over rustdoc... and indeed, tracing is oddly a cargo dependency here. That seems wrong? Tracing has a global singleton so rustc drivers should use the version from the sysroot, not their own, to ensure there's one shared logging system across the rustc core + driver.

Probably best to do that in a separate PR, but maybe add a FIXME in the Cargo.toml to at least document this?

Copy link
Member

Choose a reason for hiding this comment

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

Rustdoc intentionally doesn't use rustc's copy of tracing to be able to enable trace logs for rustdoc without having to recompile rustc:

rust/src/librustdoc/lib.rs

Lines 165 to 170 in c720f49

// When using CI artifacts with `download-rustc`, tracing is unconditionally built
// with `--features=static_max_level_info`, which disables almost all rustdoc logging. To avoid
// this, compile our own version of `tracing` that logs all levels.
// NOTE: this compiles both versions of tracing unconditionally, because
// - The compile time hit is not that bad, especially compared to rustdoc's incremental times, and
// - Otherwise, there's no warning that logging is being ignored when `download-rustc` is enabled

Copy link
Member

Choose a reason for hiding this comment

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

Ah, interesting. In Miri we use higher log levels to avoid that, since having two entirely separate loggers became quite annoying, but maybe that's less of a problem for rustdoc.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

The Miri changes LGTM, except the ones in the vendored file where only the header at the top should be edited.

I'm fine with having these Miri changes here, seems easier to do this centrally than coordinate half a dozen PRs.

Copy link
Member

Choose a reason for hiding this comment

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

This file was deliberately not formatted since it is a copy from another repo. Please undo all the formatting here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I wondered why these formatting changes appeared!

@@ -613,10 +571,9 @@ struct JsonVisitor<'a> {
object: &'a mut Object,
}

impl<'a> tracing_subscriber::field::Visit for JsonVisitor<'a> {
impl<'a> rustc_log::tracing_subscriber::field::Visit for JsonVisitor<'a> {
Copy link
Member

Choose a reason for hiding this comment

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

To avoid diff in the core of the file, please just import these modules at the top.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 7, 2025

Do you think it is feasible to add an internal lint that rejects extern crate items where the crate name doesn't start with rustc_ or sth like that?

@Diggsey
Copy link
Contributor Author

Diggsey commented Jul 7, 2025

@oli-obk I'll look into this. How does testing work for sub-trees? Would each sub-tree need its own copy of the lint, or do the subtree repos have some way to run rust-wide lints?

@RalfJung
Copy link
Member

RalfJung commented Jul 7, 2025

"internal" lints live in rustc like normal lints, but are gated behind a nightly-only flag. Miri enables them in their CI, I don't know if ./x check miri also enables them.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 7, 2025

We have a list of internal lints that rustc has and enables everywhere. We'd have to limit it to crates with #![allow(internal_features)] or sth like it. Most other lints are obviously limited to that as they look for e.g. TyKind .

@RalfJung
Copy link
Member

RalfJung commented Jul 7, 2025

Not sure what you mean about restricting it, the lints need -Zunstable-options -Wrustc::internal to be enabled, right?

@oli-obk
Copy link
Contributor

oli-obk commented Jul 7, 2025

Ah right, my bad

@@ -43,6 +43,7 @@ use std::fmt;
pub use atomic_ref::AtomicRef;
pub use ena::{snapshot_vec, undo_log, unify};
pub use rustc_index::static_assert_size;
pub use {either, indexmap, smallvec, thin_vec};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub use {either, indexmap, smallvec, thin_vec};
// Re-export some data-structure crates
pub use {either, indexmap, smallvec, thin_vec};

or something like the comment on tracing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-compiletest Area: The compiletest test runner A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-rustdoc-json Area: Rustdoc JSON backend A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants