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

Relative URLs in subdirectory readmes #3484

Closed
clehner opened this issue Apr 2, 2021 · 3 comments · Fixed by #3973
Closed

Relative URLs in subdirectory readmes #3484

clehner opened this issue Apr 2, 2021 · 3 comments · Fixed by #3973
Assignees
Labels
A-backend ⚙️ C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works

Comments

@clehner
Copy link

clehner commented Apr 2, 2021

During readme markdown rendering, links with relative URLs in a crate's readme are resolved relative to the repository root, even if the crate and its readme file are in a subdirectory of the repository. This breaks consistency with how relative URLs are resolved in the source site - relative to the file in the subdirectory.

An example of a repository with multiple crates in subdirectories (using absolute URLs in the readmes):
https://github.com/RustCrypto/elliptic-curves/

An example of a crate published from a subdirectory using relative URLs in the readme which are rendered broken:
https://crates.io/crates/ssi-contexts/0.0.2
while in the source code they are working:
https://github.com/spruceid/ssi/blob/ssi-contexts%2F/v0.0.2/contexts/README.md

It would be nice if relative URLs could be used in subdirectory readmes. Possible solutions:

  1. Resolve relative to the homepage property rather than the repository property.
  2. Add a readme_url metadata field, to use as the base URL for resolution.
  3. Add a repository_path metadata field that would be the path of the crate in the repository - where the readme is assumed to reside - for use in evaluating relative URLs in the readme.
@Turbo87 Turbo87 added C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works A-backend ⚙️ labels Apr 4, 2021
slowli added a commit to slowli/term-transcript that referenced this issue Jun 2, 2021
Relative links are not rendered properly on crates.io;
see rust-lang/crates.io#3484
@nipunn1313
Copy link
Contributor

@rustbot claim

I started looking at this. Unfortunately, as @clehner determined, it seems that crates.io doesn't have enough information at time of render to determine the subdirectory that the README is in.

relevant code:

render::render_and_upload_readme(

and relevant code where "readme_file" is determined in cargo
https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/registry.rs#L284

Resolve relative to the homepage property rather than the repository property.

Not sure this helps here - as the homepage isn't necessarily on github/etc showing code.

Add a readme_url metadata field, to use as the base URL for resolution.

This definitely doesn't seem like something we'd want to expose as something to fill in the Cargo.toml. Seems that cargo should be able to infer it. Perhaps it could be inferred by cargo and added here. Still seems like if it's possible to do this with less complexity, we should see if we can.
https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/registry.rs#L284

Add a repository_path metadata field that would be the path of the crate in the repository - where the readme is assumed to reside - for use in evaluating relative URLs in the readme.

Again - something we could have cargo infer and send on the network to crates.io

Proposal:

crates.io already gets the readme_file which it interprets as a filename in order to use the extension. It is actually passed the relative path to the README from the pkg root (from the Cargo.toml) here

  • Update crates.io to use the readme filename's directory in the relative URL generated here. Essentially generate "{repo}/blob/HEAD/{relative_path_to_readme}/{relative_url}"

This would handle readmes not in root (eg docs/README.md), but only if the Cargo.toml was in the repo root

As a bonus, if we want to handle Cargo.toml that aren't in the repo root, we can

  • Update cargo to give the relative path to the README from the REPO root rather than pkg root. AFAICT, crates.io only uses the readme path (currently) for the extension - to decide how to render.

nipunn1313 added a commit to nipunn1313/crates.io that referenced this issue Aug 24, 2021
Added unit tests to confirm things work.
Manually tested end to end and confirmed that the rendered
page has a properly working relative link as long as the Cargo.toml
is in the root of the repo.

We'll need to update cargo as mentioned in rust-lang#3484 in order to
support Cargo.toml that aren't in the root of the repo
nipunn1313 added a commit to nipunn1313/crates.io that referenced this issue Aug 24, 2021
Added unit tests to confirm things work.
Manually tested end to end and confirmed that the rendered
page has a properly working relative link as long as the Cargo.toml
is in the root of the repo.

We'll need to update cargo as mentioned in rust-lang#3484 in order to
support Cargo.toml that aren't in the root of the repo
@nipunn1313
Copy link
Contributor

Update crates.io to use the readme filename's directory in the relative URL generated here. Essentially generate "{repo}/blob/HEAD/{relative_path_to_readme}/{relative_url}"

I took a stab at this in #3861

@nipunn1313
Copy link
Contributor

As a bonus, if we want to handle Cargo.toml that aren't in the repo root, we can

Update cargo to give the relative path to the README from the REPO root rather than pkg root. AFAICT, crates.io only uses the readme path (currently) for the extension - to decide how to render.

Also took a stab at this in
rust-lang/cargo#9837

nipunn1313 added a commit to nipunn1313/crates.io that referenced this issue Aug 24, 2021
Added unit tests to confirm things work.
Manually tested end to end and confirmed that the rendered
page has a properly working relative link as long as the Cargo.toml
is in the root of the repo.

We'll need to update cargo as mentioned in rust-lang#3484 in order to
support Cargo.toml that aren't in the root of the repo
nipunn1313 added a commit to nipunn1313/cargo that referenced this issue Aug 28, 2021
Added unit tests for publishing with
- cargo.toml in root + readme in root
- cargo.toml in root + readme in subdir
- cargo.toml in subdir + readme next to it
- cargo.toml in subdir + readme in root (../README)

Should fix rust-lang/crates.io#3484 in
combination with rust-lang/crates.io#3861
nipunn1313 added a commit to nipunn1313/cargo that referenced this issue Sep 2, 2021
Added unit tests for publishing with
- cargo.toml in root + readme in root
- cargo.toml in root + readme in subdir
- cargo.toml in subdir + readme next to it
- cargo.toml in subdir + readme in root (../README)

Should fix rust-lang/crates.io#3484 in
combination with rust-lang/crates.io#3861
nipunn1313 added a commit to nipunn1313/crates.io that referenced this issue Sep 7, 2021
Added unit tests to confirm things work.
Manually tested end to end and confirmed that the rendered
page has a properly working relative link as long as the Cargo.toml
is in the root of the repo.

We'll need to update cargo as mentioned in rust-lang#3484 in order to
support Cargo.toml that aren't in the root of the repo
bors added a commit that referenced this issue Sep 12, 2021
Leverage readme_path to support relative paths from readme's not at root

Added unit tests to confirm things work.
Manually tested end to end and confirmed that the rendered
page has a properly working relative link as long as the Cargo.toml
is in the root of the repo.

We'll need to update cargo as mentioned in #3484 in order to
support Cargo.toml that aren't in the root of the repo
bors added a commit that referenced this issue Oct 28, 2021
controllers::krate::publish: Move reading of tarball up from uploader to publish

Pull upward hex_cksum and verify_tarball to publish
to the point before readme rendering - so that readme
can leverage information from tarball.

Additionally, it will avoid rendering readme for failure cases where
the tarball doesn't verify.

Will be useful to access cargo_vcs_info earlier on (for #3484)
@bors bors closed this as completed in 3f67acf Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-backend ⚙️ C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works
Projects
None yet
3 participants