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

Normalize the target paths #14497

Merged
merged 2 commits into from
Nov 6, 2024
Merged

Normalize the target paths #14497

merged 2 commits into from
Nov 6, 2024

Conversation

linyihai
Copy link
Contributor

@linyihai linyihai commented Sep 5, 2024

What does this PR try to resolve?

The targets path of the normalized_toml could be relative, which isn't user-friendly.

What is this PR doing?

This PR applys the paths::normalize_path to remove the relative part.

Fixes #14227

How should we test and review this PR?

Add a test originted from the issue, and fixing it in the next commit.

Additional information

@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2024

r? @ehuss

rustbot has assigned @ehuss.
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 the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 5, 2024
@epage
Copy link
Contributor

epage commented Sep 5, 2024

This PR does it during the conversion from TomlBinTarget to Target. Previously, #13729 did this during prepare_for_publish. I wonder if we could consolidate these by moving both to the resolve phase.

Also, this needs to be applied to all targets like #13729 did.

@rustbot rustbot added the A-manifest Area: Cargo.toml issues label Sep 7, 2024
@linyihai linyihai changed the title Normalize the relative path in the bin table. Normalize the target paths Sep 7, 2024
@linyihai linyihai force-pushed the issue-14227 branch 2 times, most recently from d71aea5 to 90753b6 Compare September 7, 2024 15:07
@linyihai linyihai marked this pull request as draft September 8, 2024 07:35
@linyihai linyihai force-pushed the issue-14227 branch 7 times, most recently from ea8f33e to a15bb7a Compare September 9, 2024 10:00
@linyihai
Copy link
Contributor Author

linyihai commented Sep 9, 2024

This PR does it during the conversion from TomlBinTarget to Target. Previously, #13729 did this during prepare_for_publish. I wonder if we could consolidate these by moving both to the resolve phase.

Also, this needs to be applied to all targets like #13729 did.

In the read_manifest I had applied the paths::normalize_path to the normalized_toml.

But it is not very aceptable for custom build, see https://github.com/rust-lang/cargo/actions/runs/10760922235/job/29839507217, because custom build can be outside the current package, the .. will be stripped after normalized.

I also want to apply the normalize_path_sep to the targets in normalized_toml, but it fails. see https://github.com/rust-lang/cargo/actions/runs/10767799732/job/29855771840, the rustc will still output \\src\\lib, but the cargo is src/lib.rs

@linyihai linyihai marked this pull request as ready for review September 9, 2024 12:45
@epage
Copy link
Contributor

epage commented Sep 9, 2024

But it is not very aceptable for custom build, see https://github.com/rust-lang/cargo/actions/runs/10760922235/job/29839507217, because custom build can be outside the current package, the .. will be stripped after normalized.

Hmm, sounds like normalize_path expects the path to be absolute. We should document and maybe add an assert about that.

That also makes me question using it elsewhere. I forgot that we leave these paths relative. I wonder if we should make the absolute during normalization and relative during publish.

@linyihai
Copy link
Contributor Author

I wonder if we should make the absolute during normalization and relative during publish.

It's not work for vendor, the absolute path of the vendor contain the vendor project path, which is hard to strip it.

Previously, #13729 did this during prepare_for_publish. I wonder if we could consolidate these by moving both to the resolve phase.

This way is trickier than I thought.

@linyihai
Copy link
Contributor Author

r? @epage

I wonder if we should make the absolute during normalization and relative during publish.

See my PR description, I think this problem has been improved, please continue to review and leave valuable comments

@rustbot rustbot assigned epage and unassigned ehuss Sep 18, 2024
@epage
Copy link
Contributor

epage commented Sep 18, 2024

See my PR description, I think this problem has been improved, please continue to review and leave valuable comments

Looking at

This adds a struct AbsolutePathTomlTarget to contruct the absolute path for the TargetToml, and apply the paths::normalize to eliminate the relative part.

its telling me what its doing. What I'm wondering is why this route was chosen. AbsolutePathTomlTarget seems like a fairly invasive change for what I'd expect.

@linyihai
Copy link
Contributor Author

its telling me what its doing. What I'm wondering is why this route was chosen. AbsolutePathTomlTarget seems like a fairly invasive change for what I'd expect.

I have given up using this scheme and instead just absolutize the path in 'target' and do 'normalize_path'.

@epage
Copy link
Contributor

epage commented Oct 30, 2024

But it is not very aceptable for custom build, see https://github.com/rust-lang/cargo/actions/runs/10760922235/job/29839507217, because custom build can be outside the current package, the .. will be stripped after normalized.

FYI I posted #14750

Comment on lines 188 to 194
if let Some(PathValue(path)) = lib.path.as_ref() {
let path = package_root.join(path);
let path = paths::normalize_path(&path);
lib.path = Some(PathValue(path.into()));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're doing the joins here, should we remove the joins in the to_ functions?

@@ -98,7 +99,7 @@ pub(super) fn to_targets(
);
targets.push(Target::custom_build_target(
&name,
package_root.join(custom_build),
paths::normalize_path(package_root.join(custom_build).as_path()),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is happening during a to_ operation while all the rest are during a normalize operation

@linyihai
Copy link
Contributor Author

FYI I posted #14750

Waiting on this PR to be merged.

If the paths::normalized_path can handle .. relative path, then the fixed solution doesn't need absolutize the target path.

bors added a commit that referenced this pull request Nov 1, 2024
fix(util): Respect all `..`s in `normalize_path`

### What does this PR try to resolve?

The fact that `normalize_path` was only designed for absolute paths bit us when working out #14497 and so I decided to make sure it worked.  The other alternative I considered was having it assert that the path was absolute.

Since I did try out the assert and Cargo tests hit it, this likely fixes something but I haven't dug through to be able to say what.

### How should we test and review this PR?

### Additional information
@linyihai
Copy link
Contributor Author

linyihai commented Nov 1, 2024

This PR does it during the conversion from TomlBinTarget to Target. Previously, #13729 did this during prepare_for_publish. I wonder if we could consolidate these by moving both to the resolve phase.

This PR chose this route.

That also makes me question using it elsewhere. I forgot that we leave these paths relative. I wonder if we should make the absolute during normalization and relative during publish.

This approach requires more changes and is not ideal. If I understand you correctly, this needs absolutize the target path in the normalize_toml, the to_real_manifest then assums target path is absolute and makes to_targests do not join the package_root. This is what the read_manifest will change.

If we accept that targets path had been absolutized, then it has a big drawback in prepare_for_publish, because in prepare_for_publish, it calls prepare_toml_for_publish to yield the normalize_toml with relative path targets, this normalize_toml doesn't meet the to_real_manifest what needs a normalize_toml with absolute path.

Comment on lines 1061 to 1063
Some(StringOrBool::String(build_file)) => {
let build_file = paths::normalize_path(Path::new(build_file));
let build = build_file
.into_os_string()
.into_string()
.map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?;
Some(StringOrBool::String(build))
Copy link
Contributor

Choose a reason for hiding this comment

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

The error message is inaccurate as package.build can only be UTF-8 because StringOrBool::String is a String.

This could instead be

.expect("`build_file` started as a String and `normalize_path` shouldn't have changed that")

@epage
Copy link
Contributor

epage commented Nov 4, 2024

This approach requires more changes and is not ideal. If I understand you correctly, this needs absolutize the target path in the normalize_toml, the to_real_manifest then assums target path is absolute and makes to_targests do not join the package_root. This is what the read_manifest will change.

Thank you for your patience in this PR and working towards what is right,even if it has been a lot of churn on your side!

@epage
Copy link
Contributor

epage commented Nov 6, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Nov 6, 2024

📌 Commit a456c22 has been approved by epage

It is now in the queue for this repository.

@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 Nov 6, 2024
@bors
Copy link
Contributor

bors commented Nov 6, 2024

⌛ Testing commit a456c22 with merge d050945...

@bors
Copy link
Contributor

bors commented Nov 6, 2024

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

@bors bors merged commit d050945 into rust-lang:master Nov 6, 2024
22 checks passed
@linyihai linyihai deleted the issue-14227 branch November 7, 2024 02:07
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 10, 2024
Update cargo

16 commits in 0310497822a7a673a330a5dd068b7aaa579a265e..4a2d8dc636445b276288543882e076f254b3ae95
2024-11-01 19:27:56 +0000 to 2024-11-09 19:10:33 +0000
- test: adjust `cargo_test_env` to unblock rust submodule update (rust-lang/cargo#14803)
- feat(warnings): add build.warnings option (rust-lang/cargo#14388)
- Revert "feat: Add `CARGO_RUSTC_CURRENT_DIR`" (rust-lang/cargo#14799)
- CI: make the `lint-docs` job required (rust-lang/cargo#14797)
- Switch CI from bors to merge queue (rust-lang/cargo#14718)
- docs(test):  Document Execs assertions based on port effort (rust-lang/cargo#14793)
- fix(test): Make redactions consistent with snapbox (rust-lang/cargo#14790)
- test(gc): Update remaining unordered tests to snapbox (rust-lang/cargo#14785)
- Normalize the `target` paths (rust-lang/cargo#14497)
- rustfix: replace special-case duplicate handling with error (rust-lang/cargo#14782)
- test: Update some emaining unordered tests to snapbox (rust-lang/cargo#14781)
- Change config paths to only check CARGO_HOME for cargo-script (rust-lang/cargo#14749)
- Enable transfer feature in triagebot (rust-lang/cargo#14777)
- Add transactional semantics to `rustfix` (rust-lang/cargo#14747)
- doc: fix `GlobalContext` reference (rust-lang/cargo#14773)
- chore: update handlebars to v6, fix build error (rust-lang/cargo#14772)
@rustbot rustbot added this to the 1.84.0 milestone Nov 10, 2024
mati865 pushed a commit to mati865/rust that referenced this pull request Nov 12, 2024
Update cargo

16 commits in 0310497822a7a673a330a5dd068b7aaa579a265e..4a2d8dc636445b276288543882e076f254b3ae95
2024-11-01 19:27:56 +0000 to 2024-11-09 19:10:33 +0000
- test: adjust `cargo_test_env` to unblock rust submodule update (rust-lang/cargo#14803)
- feat(warnings): add build.warnings option (rust-lang/cargo#14388)
- Revert "feat: Add `CARGO_RUSTC_CURRENT_DIR`" (rust-lang/cargo#14799)
- CI: make the `lint-docs` job required (rust-lang/cargo#14797)
- Switch CI from bors to merge queue (rust-lang/cargo#14718)
- docs(test):  Document Execs assertions based on port effort (rust-lang/cargo#14793)
- fix(test): Make redactions consistent with snapbox (rust-lang/cargo#14790)
- test(gc): Update remaining unordered tests to snapbox (rust-lang/cargo#14785)
- Normalize the `target` paths (rust-lang/cargo#14497)
- rustfix: replace special-case duplicate handling with error (rust-lang/cargo#14782)
- test: Update some emaining unordered tests to snapbox (rust-lang/cargo#14781)
- Change config paths to only check CARGO_HOME for cargo-script (rust-lang/cargo#14749)
- Enable transfer feature in triagebot (rust-lang/cargo#14777)
- Add transactional semantics to `rustfix` (rust-lang/cargo#14747)
- doc: fix `GlobalContext` reference (rust-lang/cargo#14773)
- chore: update handlebars to v6, fix build error (rust-lang/cargo#14772)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-manifest Area: Cargo.toml issues Command-vendor 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.

The file path in the diagnostic message contains duplicate separators on windows
5 participants