rustup icon indicating copy to clipboard operation
rustup copied to clipboard

Fix duplicated PATH entries

Open chansuke opened this issue 4 years ago • 15 comments

Fixes #2848

chansuke avatar Sep 18 '21 05:09 chansuke

This is going to need a test case, I suggest a unit test of some sort close to the code, since the external contract is only changing in the manner of the duplication, and getting at this will be a little tricky.

rbtcollins avatar Sep 19 '21 20:09 rbtcollins

@rbtcollins Thanks for your comment. I will add some unit test.

chansuke avatar Sep 20 '21 01:09 chansuke

I am trying to add a unit test in tests/cli-misc.rs but it gives panicked at 'No process instance' error. How can I get the result of cmd.env(name, new_value); in the test case?

    let a = OsString::from("/home/a/.cargo/bin");
    let path_a = PathBuf::from(a);
    let b = OsString::from("/home/b/.cargo/bin");
    let path_b = PathBuf::from(b);
    let c = OsString::from("/home/c/.cargo/bin");
    let path_c = PathBuf::from(c);

    let path_entries = vec![path_a, path_b, path_c];
    let mut cmd = Command::new("test");
    // add duplicated path
    env_var::prepend_path("/home/c/.cargo/bin", path_entries, &mut cmd);
    // trying to get the result of `prepend_path()` with `get_envs()` because `prepend_path` itself doesn't return any value.
    let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
    // gives `panicked at 'No process instance'` error
    assert_eq!(envs, &[(OsStr::new("wip"), Some(OsStr::new("wip")))]);

chansuke avatar Sep 20 '21 14:09 chansuke

Unit tests belong in the crate, not in tests/ which is where we have our integration tests. There is the TestProcess struct in the currentprocess module which is provided for this purpose. You can see its use in a number of unit tests throughout the src/ tree. It should be fairly easy to deduce how to construct an appropriate test, but if you can't, then I'm sure Robert (or I) can help further.

kinnison avatar Sep 21 '21 07:09 kinnison

I had a wrong understanding. I will try to fix it. Thanks so much for your comment.

chansuke avatar Sep 22 '21 13:09 chansuke

I'm using cmd.get_envs(), an unstable function to check the result of prepend_path, but it fails clippy check on CI. Is there any workaround for this?

chansuke avatar Sep 24 '21 10:09 chansuke

I think this will work but I have a couple of suggestions:

  • append_path should be changed likewise to prevent a regression in future.
  • value is itself a vec but not deduped
  • perhaps a helper function that takes two vecs and returns a unified vec taken in-order from vec 1 and vec 2 and with all duplicates not added to the output.

rbtcollins avatar Sep 27 '21 20:09 rbtcollins

@rbtcollins Thanks so much for your suggestion. I will fix this.

chansuke avatar Sep 28 '21 12:09 chansuke

Thanks for the support. I am stuck for about a month and would like to close it.

chansuke avatar Dec 01 '21 15:12 chansuke

Sorry I didn't realise you're stuck. https://internals.rust-lang.org/t/generic-methods-over-object-safe-traits/6774 is the thing to read to understand this - and the reason you needed trait objects is because you didn't make prepend_path generic over an implementation of ProcessEnvs. I've got a fix - should be pushed up in a minute.

However the patch fails tests now :)

rbtcollins avatar Dec 01 '21 20:12 rbtcollins

I've also rebased all the incremental commits together - there isn't enough here for a large commit history. @chansuke do you want to finish this off, or should someone else do that?

rbtcollins avatar Dec 01 '21 20:12 rbtcollins

@chansuke @rbtcollins Hi, I don't have much experience with rustup's internals nor the time to dig too deeply into them right now, but I have been affected by https://github.com/PyO3/pyo3/issues/1708 and would like to help push this across the finish line if possible.

If the only thing blocking this PR from being merged is the failing test on Windows, I submitted a PR with a fix here (though I'm not sure whether it's considered okay to call .unwrap() as I did within tests). Let me know if I can do anything else to help get this merged in!

dmartin avatar Feb 07 '22 06:02 dmartin

@rbtcollins @dmartin sorry for the late reply I'd like to finish this.

chansuke avatar Feb 07 '22 16:02 chansuke

Wondering what the status of this is -- seems to be blocking https://github.com/PyO3/pyo3/issues/1708 (among other things)

gussmith23 avatar Apr 03 '22 22:04 gussmith23

I have requested the review to @rbtcollins

chansuke avatar Apr 05 '22 02:04 chansuke