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

feat: Stablize CARGO_RUSTC_CURRENT_DIR #13644

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

Conversation

epage
Copy link
Contributor

@epage epage commented Mar 26, 2024

This provides what cargo sets as the current_dir for the rustc process.
While std::file! is unspecified in what it is relative to, it is relatively safe, it is generally relative to rustcs current_dir.

This can be useful for snapshot testing.
For example, snapbox has been using this macro on nightly since assert-rs/snapbox#247, falling back to finding a parent of CARGO_MANIFEST_DIR, if present.
This has been in use in Cargo since #13441.

This was added in #12996.
Relevant points discussed in that issue:

  • This diverged from the original proposal from the Cargo team of having a CARGO_WORKSPACE_DIR that is the "workspace" of the package being built (ie registry packages would map to CARGO_MANIFEST_DIR). In looking at the std::file! use case, CARGO_MANIFEST_DIR, no matter how we defined it, would only sort of work because no sane definition of that maps to rustc's current_dir.a This instead focuses on the mechanism currently being used.
  • Using "current dir" in the name is meant to be consistent with std::env::current_dir.
  • I can go either way on CARGO_RUSTC vs RUSTC. Existing related variables:
    • RUSTC
    • RUSTC_WRAPPER
    • RUSTC_WORKSPACE_WRAPPER
    • RUSTFLAGS (no C)
    • CARGO_CACHE_RUSTC_INFO

Note that #3946 was overly broad and covered many use cases. One of those was for packages to look up information on their dependents.
Issue #13484 is being left open to track that.

Fixes #3946

This provides what cargo sets as the `current_dir` for the `rustc`
process.
While `std::file!` is unspecified in what it is relative to,
it is relatively safe, it is generally relative to `rustc`s
`current_dir`.

This can be useful for snapshot testing.
For example, `snapbox` has been using this macro on nightly since
assert-rs/snapbox#247, falling back to finding a parent of
`CARGO_MANIFEST_DIR`, if present.
This has been in use in Cargo since rust-lang#13441.

This was added in rust-lang#12996.
Relevant points discussed in that issue:
- This diverged from the original proposal from the Cargo team of having
  a `CARGO_WORKSPACE_DIR` that is the "workspace" of the package being
  built (ie registry packages would map to `CARGO_MANIFEST_DIR`).
  In looking at the `std::file!` use case, `CARGO_MANIFEST_DIR`, no
  matter how we defined it, would only sort of work because no sane
  definition of that maps to `rustc`'s `current_dir`.a
  This instead focuses on the mechanism currently being used.
- Using "current dir" in the name is meant to be consistent with
  `std::env::current_dir`.
- I can go either way on `CARGO_RUSTC` vs `RUSTC`.  Existing related
  variables:
  - `RUSTC`
  - `RUSTC_WRAPPER`
  - `RUSTC_WORKSPACE_WRAPPER`
  - `RUSTFLAGS` (no `C`)
  - `CARGO_CACHE_RUSTC_INFO`

Note that rust-lang#3946 was overly broad and covered many use cases.
One of those was for packages to look up information on their
dependents.
Issue rust-lang#13484 is being left open to track that.

Fixes rust-lang#3946
@epage epage added the T-cargo Team: Cargo label Mar 26, 2024
@rustbot
Copy link
Collaborator

rustbot commented Mar 26, 2024

r? @weihanglo

rustbot has assigned @weihanglo.
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-build-execution Area: anything dealing with executing the compiler A-documenting-cargo-itself Area: Cargo's documentation S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 26, 2024
@epage epage marked this pull request as draft March 26, 2024 01:35
@epage epage marked this pull request as ready for review March 26, 2024 20:42
@epage
Copy link
Contributor Author

epage commented Mar 26, 2024

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented Mar 26, 2024

Team member @epage has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Mar 26, 2024
Comment on lines +703 to +705
base.get_cwd()
.map(|c| c.display().to_string())
.unwrap_or(String::new()),
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if it could just be.

Suggested change
base.get_cwd()
.map(|c| c.display().to_string())
.unwrap_or(String::new()),
base.get_cwd().unwrap_or_default(),

(and ditto for CARGO_TARGET_TMPDIR? though that is not relevant to this PR)

@@ -265,7 +265,7 @@ corresponding environment variable is set to the empty string, `""`.
where integration tests or benchmarks are free to put any data needed by
the tests/benches. Cargo initially creates this directory but doesn't
manage its content in any way, this is the responsibility of the test code.
* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from **(nightly only)**.
* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from
Copy link
Member

Choose a reason for hiding this comment

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

Still feel like people might overlook this, as it isn't an obvious thing easy to relate to their problems. That said, I am fine with the previous explanation. Just forward it here if people have the same question: #12996 (comment).

When we talked about this, Eric was hesitant about locking down the behavior of file! so I left out that use case. It should work and I can't imagine rustc doing anything but absolute or relative to std::env::current_dir but I'm leaving that to people to make the choice that they want to make those assumptions, rather than us advertising it.

@ehuss
Copy link
Contributor

ehuss commented Apr 1, 2024

Is there some information about the use cases for this? How is it expected to be used?

@epage
Copy link
Contributor Author

epage commented Apr 1, 2024

Is there some information about the use cases for this? How is it expected to be used?

One of the requests in #3946 is a way to be able to resolve std::file!() so that the paths can be looked up at runtime. A common use for this is in snapshot testing.

For example, snapbox::path::current_dir!) is useful for

  • Finding snapshot files relative to the test mod
  • Finding the test mod for updating of inline snapshots

Possible workarounds

  • CARGO_MANIFEST_DIR: Cargo initially used this for UI tests but ran into problems when being inside of the rust workspace (when we used to do that)
    • Like in snapbox, this can be augmented by a manual path walk that mostly works
  • Defining a blank path for the CARGO_WORKSPACE_DIR in [env] config. This does find the workspace dir but that isn't always the current-dir used for rustc which is what std::file! is determined by
  • Combination of the above

As you mentioned the last time the Cargo team discussed this, std::file!s behavior is not specified.

For a summary of the team's last discussion on this, see #3946 (comment)

@ehuss
Copy link
Contributor

ehuss commented Apr 3, 2024

I am concerned that if the primary (or only?) way to use this environment variable is with file!, and file! isn't defined and primarily intended for debugging purposes that it is changing the meaning and relationship of file!. There might also be concerns that if something like trim-paths changes the behavior of file!. It also seems to provide a way to circumvent trim-paths, which seems a bit concerning.

Were there other considerations made, such as adding something like abs_file!()?

Have there been any discussions with the libs team, and what their thoughts are on making file! be used for something other than debugging?

This is another benefit to starting life as test/bench only as it lowers the barrier for abusing file!.

Is this limited to only test/bench?

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 A-documenting-cargo-itself Area: Cargo's documentation disposition-merge FCP with intent to merge proposed-final-comment-period An FCP proposal has started, but not yet signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Team: Cargo
Projects
Status: FCP merge
Development

Successfully merging this pull request may close these issues.

Environment variable for Cargo Workspace
5 participants