refactor: no star imports#364
Conversation
Star imports (`use super::*;`, `pub use module::*;`, `use some_crate::prelude::*;`) hide where names come from, mask accidental shadowing, and make code review harder. Forbid them everywhere — production, tests, integration tests, build scripts, developer tooling — with no exceptions for `prelude::*` or test modules. https://claude.ai/code/session_011yRgFgrJbtbneNyrvTJp32
Apply the new `CODE_STYLE_GUIDE.md` rule across the workspace. Each
`use ...::*;` is replaced with the explicit list of items it brought
into scope:
- `pub use module::*;` re-exports in `lib.rs` files.
- `use super::*;` in `#[cfg(test)] mod tests` blocks.
- External crate preludes — `use rayon::prelude::*;` →
`use rayon::iter::{IntoParallelRefIterator, ParallelIterator};`,
`use assert_cmd::prelude::*;` →
`use assert_cmd::assert::OutputAssertExt;` /
`use assert_cmd::cargo::CommandCargoExt;`.
No behavioral changes; the refactor only narrows what each `use`
brings into scope.
https://claude.ai/code/session_011yRgFgrJbtbneNyrvTJp32
This comment was marked as resolved.
This comment was marked as resolved.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #364 +/- ##
==========================================
+ Coverage 88.65% 88.67% +0.01%
==========================================
Files 64 64
Lines 5685 5685
==========================================
+ Hits 5040 5041 +1
+ Misses 645 644 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
External-crate preludes are part of the upstream crate's documented surface, intentionally curated by the crate author for glob import. Replacing them with explicit lists creates a maintenance burden every time the upstream prelude changes. Update the rule in CODE_STYLE_GUIDE.md and AGENTS.md, and revert \`use rayon::prelude::*;\` and \`use assert_cmd::prelude::*;\` back to their prelude form across the workspace. https://claude.ai/code/session_011yRgFgrJbtbneNyrvTJp32
Micro-Benchmark ResultsLinux |
Integrated-Benchmark Report (Linux)Scenario: Frozen Lockfile
BENCHMARK_REPORT.json{
"results": [
{
"command": "pacquet@HEAD",
"mean": 2.74543376758,
"stddev": 0.0987427909630241,
"median": 2.72575009068,
"user": 2.40732956,
"system": 3.3714287,
"min": 2.62265091168,
"max": 2.96533378868,
"times": [
2.84590579868,
2.96533378868,
2.66741255668,
2.7192995406800002,
2.77939443768,
2.62265091168,
2.72276121368,
2.72873896768,
2.73000512668,
2.67283533368
]
},
{
"command": "pacquet@main",
"mean": 2.7000403427800004,
"stddev": 0.03510313480930069,
"median": 2.70316648868,
"user": 2.432765559999999,
"system": 3.3732937,
"min": 2.6551671696800003,
"max": 2.7676898916800003,
"times": [
2.7676898916800003,
2.71050639768,
2.70223837668,
2.73030188668,
2.6610197546800003,
2.7225693406800002,
2.67494340668,
2.6551671696800003,
2.67187260268,
2.70409460068
]
},
{
"command": "pnpm",
"mean": 6.7047177410800005,
"stddev": 0.03561168911841339,
"median": 6.70436473068,
"user": 9.334614559999999,
"system": 4.5212504,
"min": 6.64962199568,
"max": 6.7659590736799995,
"times": [
6.73900432468,
6.68070549468,
6.69460699068,
6.71412247068,
6.7659590736799995,
6.64962199568,
6.6799110086799995,
6.72423131568,
6.67092060468,
6.72809413168
]
}
]
}Scenario: Frozen Lockfile (Hot Cache)
BENCHMARK_REPORT.json{
"results": [
{
"command": "pacquet@HEAD",
"mean": 0.73378334178,
"stddev": 0.02150764921385495,
"median": 0.7280884360800001,
"user": 0.25985998,
"system": 1.37291064,
"min": 0.7147868000800001,
"max": 0.7873106460800001,
"times": [
0.7873106460800001,
0.7252607430800001,
0.7355112130800001,
0.7491548020800001,
0.71764163008,
0.73091612908,
0.7356041780800001,
0.7147868000800001,
0.71788520308,
0.7237620730800001
]
},
{
"command": "pacquet@main",
"mean": 0.7476088357800001,
"stddev": 0.06410959014619248,
"median": 0.7280789410800002,
"user": 0.25205448,
"system": 1.37803544,
"min": 0.7016093410800001,
"max": 0.9081759370800001,
"times": [
0.80662091808,
0.7077979130800001,
0.74236145208,
0.7017485570800001,
0.7345619590800001,
0.7243409900800001,
0.9081759370800001,
0.71705439808,
0.7318168920800001,
0.7016093410800001
]
},
{
"command": "pnpm",
"mean": 2.73731182188,
"stddev": 0.13177823668961447,
"median": 2.70532456508,
"user": 3.3034450800000004,
"system": 2.2990776399999997,
"min": 2.6122051060800002,
"max": 3.0727902670800002,
"times": [
2.81536624708,
2.6122051060800002,
3.0727902670800002,
2.6902333520800004,
2.6558361300800004,
2.6590642410800003,
2.6567728770800003,
2.75312983708,
2.72041577808,
2.73730438308
]
}
]
} |
Address review feedback on the no-star-imports rule: * Add a second exemption for root-of-module re-exports (\`pub use submodule::*;\` in \`lib.rs\`). They are part of the public surface the crate intentionally exposes; listing each item duplicates information that already lives in the submodule. * Drop the categorical \"Never\" framing now that the rule has two carve-outs. * Drop the redundant pointer to the imports section. * Add a sentence on why \`use super::*;\` is especially harmful in tests: an unused production import stays compilable as long as a test still references it. * Restructure the prose to avoid mid-sentence em dashes per the writing style in \`CONTRIBUTING.md\`. Revert the \`pub use submodule::*;\` re-exports and \`pub use _utils::*;\` test re-exports back to their original star form across the workspace. Tighten the surrounding test-module imports in \`crates/fs/src/ensure_file.rs\` and \`crates/npmrc/src/custom_deserializer.rs\` so the platform-gated \`#[cfg(...)]\` imports sit in the trailing block per the existing import-organization rule. https://claude.ai/code/session_011yRgFgrJbtbneNyrvTJp32
|
@coderabbitai review. |
|
Tip For best results, initiate chat on the files or code changes.
[review] |
There was a problem hiding this comment.
Pull request overview
Refactors Rust modules to eliminate in-module glob imports (especially use super::*;) in favor of explicit imports, and codifies this as a documented style rule to prevent hidden name origins and tests masking unused production imports.
Changes:
- Replaced
use super::*;/ other glob imports in many unit-test modules with explicit item lists. - Added/updated documented guidance in
CODE_STYLE_GUIDE.mdandAGENTS.mdto ban star imports inside module bodies with two explicit exceptions. - Adjusted a few test-only imports (std / crate deps) to keep tests compiling under explicit import lists.
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/tarball/src/tests.rs | Replace use super::* and add explicit imports for tarball tests. |
| crates/store-dir/src/store_index.rs | Replace use super::* in tests with explicit imports. |
| crates/store-dir/src/store_dir.rs | Replace use super::* in tests with explicit imports. |
| crates/store-dir/src/msgpackr_records.rs | Replace glob imports in tests with explicit item lists. |
| crates/store-dir/src/check_pkg_files_integrity.rs | Replace use super::* in tests; add explicit std/sha2 imports. |
| crates/store-dir/src/cas_file.rs | Replace use super::* in tests with explicit imports. |
| crates/registry/src/package.rs | Replace use super::* in tests with explicit type import. |
| crates/package-manifest/src/lib.rs | Replace use super::* in tests; add explicit std::io::Write import. |
| crates/package-manager/src/link_file.rs | Replace use super::* in tests with explicit imports. |
| crates/package-manager/src/install_package_from_registry.rs | Replace use super::* in tests with explicit imports. |
| crates/package-manager/src/install.rs | Replace use super::* in tests with explicit imports. |
| crates/package-manager/src/build_snapshot.rs | Replace use super::* in tests with explicit imports. |
| crates/npmrc/src/workspace_yaml.rs | Replace use super::* in tests with explicit imports. |
| crates/npmrc/src/npmrc_auth.rs | Replace use super::* in tests with explicit imports. |
| crates/npmrc/src/lib.rs | Replace use super::* in tests with explicit imports. |
| crates/npmrc/src/custom_deserializer.rs | Replace use super::* in tests with explicit imports (incl. cfg(windows) items). |
| crates/lockfile/src/snapshot_dep_ref.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/save_lockfile.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/resolution.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/project_snapshot.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/pkg_ver_peer.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/pkg_name_ver_peer.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/pkg_name_ver.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/pkg_name.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/lockfile_version.rs | Replace use super::* in tests with explicit imports. |
| crates/lockfile/src/comver.rs | Replace use super::* in tests with explicit imports. |
| crates/fs/src/ensure_file.rs | Replace use super::* in tests with explicit imports (incl. cfg(unix) items). |
| crates/cli/src/state.rs | Replace use super::* in tests with explicit imports. |
| crates/cli/src/cli_args/install.rs | Replace use super::* in tests with explicit imports. |
| crates/cli/src/cli_args/add.rs | Replace use super::* in tests with explicit imports. |
| CODE_STYLE_GUIDE.md | Document “No star imports” rule + allowed exceptions and examples. |
| AGENTS.md | Add “No star imports inside module bodies” to contributor guidelines (points to style guide). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Apply the no-star-imports guideline added in #364: > Avoid star (glob) imports inside the bodies of regular modules. > Import items explicitly by name everywhere except external-crate > preludes and root-of-module re-exports. The only star inside the new module's body was `use super::*;` in `crates/modules-yaml/src/edge_cases.rs`. Listed the four helpers the file actually uses (`derive_hoisted_dependencies`, `drop_empty_hoist_fields`, `is_empty_or_null`, `sort_skipped`) so a future stale import here surfaces as an unused-import warning.
Apply the "No star imports" rule from CODE_STYLE_GUIDE.md (added by PR #364) to the test modules introduced by this PR. Each test module now imports its parent's items by name instead of via the glob, so a removed parent item is caught by the compiler instead of silently shadowing an unrelated re-export. Affected: * crates/cmd-shim/src/bin_resolver/tests.rs * crates/cmd-shim/src/link_bins/tests.rs * crates/cmd-shim/src/shim/tests.rs * crates/package-manager/src/link_bins/tests.rs * crates/package-manager/src/symlink_direct_dependencies/tests.rs The four `pub use submodule::*;` re-exports in `crates/cmd-shim/src/lib.rs` stay as-is — those are the root-of-module re-export form the guide explicitly allows.
PR #364's narrowed test imports in `crates/package-manager/src/install.rs` left out the symbols that the stage-event smoke test from PR #349 references (`LogEvent`, `Reporter`, `Stage`, `StageLog`, plus `Lockfile`). The test module compiles cleanly with `--tests` enabled once the imports are restored — without this, `cargo check --tests` fails on the merged main even before this branch's changes are applied. Found while applying PR #364's "No star imports" rule to my own test files; surfacing the fix here instead of leaving main red.
Follow the new "No star imports" rule from #364 across the test modules in this PR's new and modified files: `crates/network/src/auth.rs`, `crates/npmrc/src/env_replace.rs`, `crates/npmrc/src/lib.rs`, `crates/npmrc/src/npmrc_auth.rs`, `crates/registry/src/package.rs`, `crates/registry/src/package_version.rs`, and `crates/tarball/src/tests.rs`. Each `use super::*;` is replaced with the minimal set of names the test bodies actually reference; intra-doc links to other module-private items resolve without an import.
…action The unit-test module in `crates/package-manager/src/install.rs` was left referencing five symbols not in scope after PRs #349 and #364 landed on main in succession. #349 added a test that uses `Lockfile`, `Reporter`, `LogEvent`, `StageLog`, and `Stage`; #364 (no star imports) replaced the previous `use super::*;` with `use super::{Install, InstallError};` and didn't carry over the additional symbols the new test needed. The result is `cargo test -p pacquet-package-manager --lib` failing to compile on `main` itself across all three CI platforms. Restore the missing items by name in the test imports so the no-star-imports rule still holds: use pacquet_lockfile::Lockfile; use pacquet_reporter::{LogEvent, Reporter, SilentReporter, Stage, StageLog}; Verified pre-existing on `origin/main` (`git switch --detach origin/main && cargo build --tests -p pacquet-package-manager` produces the same nine errors). Per AGENTS.md ("If a test was already broken on main, fix it as part of your work"), fixing here.
…370) #364's `use super::*;` removal stripped the test module's view of `LogEvent`, `Reporter`, `Stage`, `StageLog`, and `Lockfile`, but the test bodies still reference them — so `cargo nextest run -p pacquet-package-manager` failed to compile on `main`. Promote the inline `use pacquet_lockfile::Lockfile` from `frozen_lockfile_flag_overrides_config_lockfile_false` to the module- level `use` block, and add the missing reporter symbols alongside the existing `SilentReporter` import. The two `Lockfile`-using tests now share one import.
The no-star-imports sweep in #364 dropped symbols the reporter smoke test in #349 needs. `crates/package-manager/src/install.rs::tests` references `Stage`, `LogEvent`, `Reporter`, `StageLog`, and `Lockfile` in its body, but the explicit-import list only kept `Install` and `InstallError`. Restore the missing names so the crate compiles. Confirmed pre-existing on `origin/main` (`cargo check --tests -p pacquet-package-manager` reproduces). Same drive-by fix is in flight as part of #332. Picking it up here so this PR's CI is not blocked by the upstream regression.
Summary by CodeRabbit
Documentation
*) imports within module bodies, with documented exceptions for external crate preludes.Refactor