3

Part 3 of RFC2906 - Add support for inheriting `license-path`, and `depednency.p...

 2 years ago
source link: https://github.com/rust-lang/cargo/pull/10538
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

src/cargo/core/workspace.rs

Outdated Show resolved

src/cargo/core/workspace.rs

Outdated Show resolved

src/cargo/core/workspace.rs

Outdated Show resolved

@@ -1021,6 +1022,12 @@ impl<T> MaybeWorkspace<T> {

)),

}

}

fn get_defined(&self) -> CargoResult<&T> {

match self {

MaybeWorkspace::Workspace(_) => Err(anyhow!("MaybeWorkspace was Workspace")),

Copy link

Contributor

@epage epage 10 days ago

nit: I think I'd just do this as an as_defined(&self) -> Option<&T> and let the error message be completely controlled by the caller

Copy link

Contributor

Author

@Muscraft Muscraft 10 days ago

I could go either way on this. If you feel its better I'll change it to something like this

src/cargo/util/toml/mod.rs

Outdated Show resolved

if let Some(diff_str) = diff_path.to_str() {

self.path = Some(diff_str.to_owned());

}

}

Copy link

Contributor

@epage epage 10 days ago

If I'm reading this correctly, we are leaving the incorrect relative path in if it doesn't work?

Wonder if we should have a function shared between the the path rewriting so we can be consistent on error reporting.

Copy link

Contributor

Author

@Muscraft Muscraft 10 days ago

You mean something like

pub fn resolve_relative_path(label: &str, root_path: Pathbuf, package_root: PathBuf) -> CargoResult<String> {
    match diff_paths(root_path, package_root) {
        None => Err(anyhow!("`workspace.{}` was defined but could not be resolved with {}", label, package_root.display())),
        Some(path) => Ok(
            path
            .to_str()
            .ok_or_else(|| anyhow!("`{}` resolved to non-UTF value (`{}`)", label, path.display()))?
            .to_owned()
        ),
    }
}

Copy link

Contributor

@epage epage 10 days ago

  • I'd recommend having old_root, new_root, and rel_path as parameters so it can also handle join + normalize_path for you
  • workspace. doesn't quite work for path dependencies. Maybe just leave it as {label} and re-word to the first to include both paths

Copy link

Contributor

Author

@Muscraft Muscraft 10 days ago

pub fn resolve_relative_path(
    label: &str,
    old_root: &PathBuf,
    new_root: &PathBuf,
    rel_path: &str,
) -> CargoResult<String> {
    let joined_path = normalize_path(&old_root.join(rel_path));
    match diff_paths(joined_path, new_root) {
        None => Err(anyhow!(
            "`{}` was defined in {} but could not be resolved with {}",
            label,
            old_root.display(),
            package_root.display()
        )),
        Some(path) => Ok(path
            .to_str()
            .ok_or_else(|| {
                anyhow!(
                    "`{}` resolved to non-UTF value (`{}`)",
                    label,
                    path.display()
                )
            })?
            .to_owned()),
    }
}

Copy link

Contributor

@epage epage 10 days ago

Seems like that'd work

fn resolve_path(&mut self, root_path: &Path, package_root: &Path, config: &Config) {

if let Some(from_root) = &self.path {

let resolved = from_root.resolve(config);

if let Some(diff_path) = diff_paths(root_path.join(resolved), package_root) {

Copy link

Contributor

@epage epage 10 days ago

Does diff_paths gracefully handle diffing /foo/bar/../baz with /foo/bee? Do we need to call cargo_utils::paths::normlize_path after doing the join?

I remember having to be particular about that in cargo-add but I don't remember if it was related to diff_paths or other stuff

(maybe another reason for a shared function, see below)

Copy link

Contributor

Author

@Muscraft Muscraft 10 days ago

I don't know if it handles that well or not. I have a feeling that we will need to call cargo_utils::paths::normlize_path as well. I'll change it to this.

"license": null,

"license_file": null,

"license": "MIT",

"license_file": "[..]LICENSE",

Copy link

Contributor

@epage epage 10 days ago

I feel like we'll get a better idea if the tests are doing what we expect if they were to use [ROOT] instead of [..]

Copy link

Contributor

Author

@Muscraft Muscraft 10 days ago

I'll change it to [ROOT], I agree that it gives a better idea of what is going on

Copy link

Contributor

Author

@Muscraft Muscraft 10 days ago

I tried to do this and it didn't work for some reason. I just changed it for ../LICENSE

Copy link

Contributor

@epage epage 10 days ago

Oh right, its relative now. Thats what I'm looking for, thanks!

}

impl TomlDependency {

Copy link

Contributor

@epage epage 10 days ago

Why the new block?

Copy link

Contributor

Author

@Muscraft Muscraft 10 days ago

Same reasons as for DetailedTomlDependency

src/cargo/core/workspace.rs

Outdated Show resolved


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK