Skip to content

Reduce rustc -L args used in the new build-dir layout#17168

Merged
epage merged 4 commits into
rust-lang:masterfrom
ranger-ross:reduce-path-length
Jul 8, 2026
Merged

Reduce rustc -L args used in the new build-dir layout#17168
epage merged 4 commits into
rust-lang:masterfrom
ranger-ross:reduce-path-length

Conversation

@ranger-ross

Copy link
Copy Markdown
Member

What does this PR try to resolve?

With the new build-dir enabled in rust-lang/rust there are have been a few reported issues due to long commands / PATH.
This PR attempts to mitigate this by doing the following:

  1. Fix a bug where the current build unit's out dir would be added the rustc args even though it is never needed. (due to the first iteration in the recursive add_dep_arg fn).
    • This probably wont make much different for long PATHs but it should not passed.
  2. Avoid passing build-script build unit's out dir which contains the build-script binary as it would very odd to link against a build script. This also has the effect of not recursively including other [build-dependencies] the build-script unit depended on.

I included this both in the PR as they are small and related but happy to split into dedicated PRs if desired :)

How to test and review this PR?

See the included tests. Best reviewed commit by commit

cc: @Kobzol

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 2, 2026
@rustbot

rustbot commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

r? @epage

rustbot has assigned @epage.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ehuss, @epage, @weihanglo
  • @ehuss, @epage, @weihanglo expanded to ehuss, epage, weihanglo
  • Random selection from ehuss, epage, weihanglo

Comment thread src/cargo/core/compiler/mod.rs Outdated
Comment thread src/cargo/core/compiler/mod.rs Outdated
@Kobzol

Kobzol commented Jul 2, 2026

Copy link
Copy Markdown
Member

Maybe as another optimization (because each dependency with a build script will generate two -Ls even after this PR), Cargo could check if the build script's $OUT_DIR is empty, and if yes, in that case do not generate the -L for the out directory of the build script's execution? 🤔

@ranger-ross
ranger-ross force-pushed the reduce-path-length branch from a6fc9f2 to b6b1b73 Compare July 2, 2026 14:06
@Kobzol

Kobzol commented Jul 2, 2026

Copy link
Copy Markdown
Member

I think that another -L that we could skip is the out directory of each direct dependency of the compiled crate. Consider a hello world crate foo that depends e.g. on log and env_logger, using the new build dir layout. It generates this final rustc call (I omitted some flags):

rustc --crate-name foo --edition=2024 src/main.rs --crate-type bin
--emit=dep-info,link -C embed-bitcode=no -C debuginfo=2
--out-dir /tmp/foo/target/debug/build/foo/ea1ee4071e58254b/out
-C incremental=/tmp/foo/target/debug/incremental
-L dependency=/tmp/foo/target/debug/build/aho-corasick/c53d038be9b9715d/out
-L dependency=/tmp/foo/target/debug/build/anstream/4bdda5cd8d2978b2/out
-L dependency=/tmp/foo/target/debug/build/log/a7c7b57a5f6aeb19/out
-L dependency=/tmp/foo/target/debug/build/env_logger/c5ba55a17507a07e/out 
--extern env_logger=/tmp/foo/target/debug/build/env_logger/c5ba55a17507a07e/out/libenv_logger-c5ba55a17507a07e.rlib
--extern log=/tmp/foo/target/debug/build/log/a7c7b57a5f6aeb19/out/liblog-a7c7b57a5f6aeb19.rlib

Notice how cargo passes both -L dependency=log and --extern log=log.rlib. Is that actually necessary? It shouldn't be for the .rlib file itself, because when you pass something via --extern, it does not actually need -L (see this comment).

So if that is the case, we could skip each -L flag for a dependency that is already passed through --extern, and only use -L for indirect dependencies. That being said, I'm not sure if there can be something else in the direct dependency's out directory that rustc might have to depend on through -L. Do you know? I'll also try to ask bjorn or Vadim.

@Kobzol

Kobzol commented Jul 2, 2026

Copy link
Copy Markdown
Member

I tested it both with depending on .rlib and dylib (.so), with and without -Zno-embed-metadata, and everything seems to work even without the -L for direct dependencies.

@rustbot

This comment has been minimized.

@ranger-ross
ranger-ross force-pushed the reduce-path-length branch from 11e2dd5 to 62ecc64 Compare July 8, 2026 13:08
@rustbot

rustbot commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ranger-ross

Copy link
Copy Markdown
Member Author

dont merge this yet, I am still testing it and I think there might be some issues hidden in other tests since -Zbuild-dir-new-layout no longer defaults to ON.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 8, 2026
@ranger-ross
ranger-ross force-pushed the reduce-path-length branch from 62ecc64 to c28162c Compare July 8, 2026 13:48
@ranger-ross

Copy link
Copy Markdown
Member Author

Notice how cargo passes both -L dependency=log and --extern log=log.rlib. Is that actually necessary? It shouldn't be for the .rlib file itself, because when you pass something via --extern, it does not actually need -L (see this comment).

So if that is the case, we could skip each -L flag for a dependency that is already passed through --extern, and only use -L for indirect dependencies. That being said, I'm not sure if there can be something else in the direct dependency's out directory that rustc might have to depend on through -L. Do you know? I'll also try to ask bjorn or Vadim.

Oh interesting, I am not sure. That sounds like a reasonable optimization we could make if we indeed don't need to depend on anything through -L for direct deps

@ranger-ross

Copy link
Copy Markdown
Member Author

I tried removing -L args from direct dependencies and the Cargo testsuite passed (minus a few tests due to cmd line arg snapshots)
So might be possible to do, though maybe better to do that in its own PR.


I finished checking the remaining tests and I believe the latest version is now correct and all of the tests pass.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. labels Jul 8, 2026
Comment thread src/cargo/core/compiler/mod.rs Outdated
@ranger-ross
ranger-ross force-pushed the reduce-path-length branch from c28162c to 73f20c2 Compare July 8, 2026 16:00
@epage
epage added this pull request to the merge queue Jul 8, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 8, 2026
@epage
epage added this pull request to the merge queue Jul 8, 2026
Merged via the queue into rust-lang:master with commit dd56971 Jul 8, 2026
29 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 8, 2026
weihanglo added a commit that referenced this pull request Jul 12, 2026
Backports:

* #17191
* #17168
* #17179

Was only planning to backport #17191 but it builds on tests added in
#17168 so was getting merge conflicts. Given they are both bug fixes and
the new build-dir layout is still unstable, I figured it would be
simpler to backport both fixes.

r? @weihanglo
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Jul 18, 2026
Update cargo submodule

29 commits in 59800466c5c41c444d264b1010b4d57e85a7117f..3efb1f477e99b42974b982d939fd100303cdf7db
2026-07-07 15:52:22 +0000 to 2026-07-17 23:53:19 +0000
- refactor(context): normalize in `homedir` instead (rust-lang/cargo#17222)
- chore(ci): reflect doc folder move in book deployment (rust-lang/cargo#17235)
- refactor(ops): Have cargo-metadata's ops match the command name (rust-lang/cargo#17233)
- chore: Flatten src (rust-lang/cargo#17231)
- chore: Flatten `src` (rust-lang/cargo#17230)
- chore(ci): remove stale libsecret packages (rust-lang/cargo#17229)
- perf: Lazily initialize git2 fetch transports  (rust-lang/cargo#17226)
- test(trim-paths): re-enable lldb debugger tests  (rust-lang/cargo#17223)
- Include SBOM outputs in fingerprints (rust-lang/cargo#17216)
- Update cfg_aliases to 0.2.2 (rust-lang/cargo#17225)
- test(trim-paths): exercise GDB on windows-gnu (rust-lang/cargo#17221)
- feat(profile): Disable incremental compilation under CI by default (rust-lang/cargo#17220)
- Remove myself from review rotation (rust-lang/cargo#17219)
- Fix typo in comment in sync (rust-lang/cargo#17217)
- docs(ref): Improve handing of built-in profiles (rust-lang/cargo#17213)
- fix: dont apply host-config gating to stable behavior (rust-lang/cargo#17198)
- chore(deps): update msrv (rust-lang/cargo#17192)
- test: fix race in cargo_compile_with_invalid_code_in_deps (rust-lang/cargo#17203)
- Rename `-Zno-embed-metadata` to `-Zembed-metadata=no` (rust-lang/cargo#17149)
- fix(source): incorrect duplicate package warning (rust-lang/cargo#17204)
- Fix manifest schema generation: `TomlDebugInfo` enum-variants doesn't renamed (rust-lang/cargo#17202)
- Reduce library search path length in new build dir layout (rust-lang/cargo#17191)
- fix(install): Move --debug to Compilation options (rust-lang/cargo#17199)
- chore(ci): dogfood `build.warnings` (rust-lang/cargo#17195)
- docs(lints): Better match clippy in lint section titles (rust-lang/cargo#17190)
- chore: bump to 0.100.0; update changelog (rust-lang/cargo#17189)
- docs(ref): Clarify MSRV for lints (rust-lang/cargo#17184)
- docs(report): add missing entry for `cargo report future-incompatibilities` (rust-lang/cargo#17188)
- Reduce rustc `-L` args used in the new `build-dir` layout (rust-lang/cargo#17168)
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Jul 18, 2026
Update cargo submodule

29 commits in 59800466c5c41c444d264b1010b4d57e85a7117f..3efb1f477e99b42974b982d939fd100303cdf7db
2026-07-07 15:52:22 +0000 to 2026-07-17 23:53:19 +0000
- refactor(context): normalize in `homedir` instead (rust-lang/cargo#17222)
- chore(ci): reflect doc folder move in book deployment (rust-lang/cargo#17235)
- refactor(ops): Have cargo-metadata's ops match the command name (rust-lang/cargo#17233)
- chore: Flatten src (rust-lang/cargo#17231)
- chore: Flatten `src` (rust-lang/cargo#17230)
- chore(ci): remove stale libsecret packages (rust-lang/cargo#17229)
- perf: Lazily initialize git2 fetch transports  (rust-lang/cargo#17226)
- test(trim-paths): re-enable lldb debugger tests  (rust-lang/cargo#17223)
- Include SBOM outputs in fingerprints (rust-lang/cargo#17216)
- Update cfg_aliases to 0.2.2 (rust-lang/cargo#17225)
- test(trim-paths): exercise GDB on windows-gnu (rust-lang/cargo#17221)
- feat(profile): Disable incremental compilation under CI by default (rust-lang/cargo#17220)
- Remove myself from review rotation (rust-lang/cargo#17219)
- Fix typo in comment in sync (rust-lang/cargo#17217)
- docs(ref): Improve handing of built-in profiles (rust-lang/cargo#17213)
- fix: dont apply host-config gating to stable behavior (rust-lang/cargo#17198)
- chore(deps): update msrv (rust-lang/cargo#17192)
- test: fix race in cargo_compile_with_invalid_code_in_deps (rust-lang/cargo#17203)
- Rename `-Zno-embed-metadata` to `-Zembed-metadata=no` (rust-lang/cargo#17149)
- fix(source): incorrect duplicate package warning (rust-lang/cargo#17204)
- Fix manifest schema generation: `TomlDebugInfo` enum-variants doesn't renamed (rust-lang/cargo#17202)
- Reduce library search path length in new build dir layout (rust-lang/cargo#17191)
- fix(install): Move --debug to Compilation options (rust-lang/cargo#17199)
- chore(ci): dogfood `build.warnings` (rust-lang/cargo#17195)
- docs(lints): Better match clippy in lint section titles (rust-lang/cargo#17190)
- chore: bump to 0.100.0; update changelog (rust-lang/cargo#17189)
- docs(ref): Clarify MSRV for lints (rust-lang/cargo#17184)
- docs(report): add missing entry for `cargo report future-incompatibilities` (rust-lang/cargo#17188)
- Reduce rustc `-L` args used in the new `build-dir` layout (rust-lang/cargo#17168)
@rustbot rustbot added this to the 1.99.0 milestone Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants