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

fix(directory): avoid confusing modules with PowerShell paths #1114

Merged
merged 7 commits into from Apr 30, 2020
Merged

fix(directory): avoid confusing modules with PowerShell paths #1114

merged 7 commits into from Apr 30, 2020

Conversation

jeanga
Copy link
Contributor

@jeanga jeanga commented Apr 16, 2020

Description

Powershell supports PSDrives (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-7) that allows to create "logical" drives mapped to actual Windows drives.

Motivation and Context

When using PSDrives, you can use paths that are related to a logical drive.
For example, test: can be "mapped" to c:\test folder.
You can then use "cd test:" to change the current location.

Many other drives exist be default (env, function, variable etc...).

These "Powershell" paths confuse starship modules that fail to return the correct result.

Changing the --path argument to a Windows path (instead of the Powershell one) fixes the issue.

In our example, with the fix, when "cd test:" is used, starship is called with --path C:\test (instead of test:).

Closes:

Types of changes

This change introduces a call to Convert-Path in startship.ps1 to convert the variable $PWD (a "PowerShell" path to a Windows path. This change avoids modules (like Rust, Git, ...) being confused by a PowerShell path.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Screenshots (if appropriate):

How Has This Been Tested?

Created a PSDrive:
New-PSDrive -Scope 1 -Name test -PSProvider FileSystem -root c:\test -Description "PSDrive test with starship"
Checked basic modules (Rust&Git) are now working properly.

  • I have tested using MacOS
  • I have tested using Linux
  • I have tested using Windows
  • I have tested using Windows Subsystem for Linux

Checklist:

  • I have updated the documentation accordingly.
  • I have updated the tests accordingly.

@jeanga jeanga changed the title Avoid confusing modules with PowerShell paths fix: avoid confusing modules with PowerShell paths Apr 16, 2020
@jimleroyer
Copy link
Member

Being a PowerShell user myself, I gave this a spin locally. It does improve the behavior with PSDrive. Some examples (would have been nice to have these SS submitted with the PR -- before / after images of behavior):

The root is properly displayed within a PSDrive:
jeanga-proper-root

This is what's the best about this fix to me though. We can see that the master build breaks on the information display when we are in git repository with modules that should be detected (Rust+git in this case):
jeanga-full-info

Looks good to me! 👏

Copy link
Member

@andytom andytom left a comment

Choose a reason for hiding this comment

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

I don't use Windows so can't test this but in general this LGTM, there are a few little fixes that I think you need to make, running cargo fmt should fix the broken formatting (https://github.com/starship/starship/pull/1114/checks?check_run_id=595902950) and it looks like there is another test that needs to be updated (

#[test]
#[cfg(target_os = "windows")]
fn directory_in_root() -> io::Result<()> {
let output = common::render_module("directory")
.arg("--path=C:\\")
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = format!("in {} ", Color::Cyan.bold().paint("/c"));
assert_eq!(expected, actual);
Ok(())
}
).

@andytom
Copy link
Member

andytom commented Apr 20, 2020

Also I'm not sure but it looks like this will also fix #1106, is this correct?

@jeanga
Copy link
Contributor Author

jeanga commented Apr 20, 2020

Sorry for the formatting errors & test failures ("cargo test" did not fail on my machine for a reason I still ignore).
My only excuse is to be new to Rust&Starship.
Thank you for your understanding!

@jeanga jeanga changed the title fix: avoid confusing modules with PowerShell paths fix(directory): avoid confusing modules with PowerShell paths Apr 20, 2020
@andytom
Copy link
Member

andytom commented Apr 20, 2020

Sorry for the formatting errors & test failures ("cargo test" did not fail on my machine for a reason I still ignore).
My only excuse is to be new to Rust&Starship.
Thank you for your understanding!

Not a problem, we have a few tests that are ignored locally because they depend on the machine running the tests being setup in a particular way (e.g specific versions of an application being installed or some particular files existing).

@jimleroyer and @ylor can you have a quick look at this to confirm it works as expected and I'll get this merged in for the next release.

@jimleroyer
Copy link
Member

jimleroyer commented Apr 23, 2020

I don't think it works as expected yet. The root folder works fine by displaying C:/ but getting into a folder shows double forward slashes, which some of the old code prevented to do (and now does again).

image

I would suggest a) to remove the double forward slashes as it seems like a regression and b) to add a test that triggers this behavior by getting into a folder after a root, so we are sure it does not repeat again.

I can help with that if @jeanga wants me to submit a PR fixing that on his repo.

@davidkna
Copy link
Member

I don't think it works as expected yet. The root folder works fine by displaying C:/ but getting into a folder shows double forward slashes, which some of the old code prevented to do (and now does again).

image

I would suggest a) to remove the double forward slashes as it seems like a regression and b) to add a test that triggers this behavior by getting into a folder after a root, so we are sure it does not repeat again.

I can help with that if @jeanga wants me to submit a PR fixing that on his repo.

The C:\ to /c is broken anyway because it only works with C:\. e.g. D://foo is broken. This brokenness is at least consistent.

Getting rid of the // should be possible by replacing path_slash
e.g. with

use std::path::Path;

fn to_slash(path: &Path) -> Option<String> {
    assert!(path.is_absolute());

    let size = path.as_os_str().len();
    let mut out = String::with_capacity(size);

    let mut iterator = path.iter();

    // Don't append / before root directory
    out.push_str(&iterator.next()?.to_string_lossy());
    
    // skip prevents double //
    for component in iterator.skip(1) {
        out.push('/');
        out.push_str(&component.to_string_lossy());
    }

    Some(out)
}

Obviously windows-only.

@jeanga
Copy link
Contributor Author

jeanga commented Apr 23, 2020

Hi davidkna,

I noticed that behavior but I do not think it is a regression of this PR.
It was already behaving this way before.

Are you suggesting I add you to the members of my forked repo?
If so, I can certainly do that :-)

Jean

@davidkna
Copy link
Member

davidkna commented Apr 23, 2020

Hi davidkna,

I noticed that behavior but I do not think it is a regression of this PR.
It was already behaving this way before.

Are you suggesting I add you to the members of my forked repo?
If so, I can certainly do that :-)

Jean

The removal of replace_c_dir() makes this something of a regression, but I'm not sure that if this change would be better of in a seperate PR.

@jeanga
Copy link
Contributor Author

jeanga commented Apr 23, 2020

Yes, but the behavior showed with anything not c:\ like d:\ ... z:\ :-)

Jean

@ylor
Copy link

ylor commented Apr 28, 2020

I don't think it works as expected yet. The root folder works fine by displaying C:/ but getting into a folder shows double forward slashes, which some of the old code prevented to do (and now does again).

image

I would suggest a) to remove the double forward slashes as it seems like a regression and b) to add a test that triggers this behavior by getting into a folder after a root, so we are sure it does not repeat again.

I can help with that if @jeanga wants me to submit a PR fixing that on his repo.

And the slashes are still going the wrong way. Not sure if this PR is for solving that though. Please see the behavior I expect when working in Powershell (that is to say not WSL).

Annotation 2020-04-27 204940

@jeanga
Copy link
Contributor Author

jeanga commented Apr 28, 2020

This PR tries to be as close as the previous behavior as possible while fixing annoying bugs for Windows Powershell users.
As previously stated, the double slash (D://Windows) and replacing the backslash with a slash was the previous way it behaved.
If there is a consensus on preserving \ and removing these double //, I'll be happy to submit a fix in another PR.

@ylor
Copy link

ylor commented Apr 28, 2020

@jeanga Thanks for your work.

@andytom
Copy link
Member

andytom commented Apr 30, 2020

@jeanga thank you for your contribution and thank you to everyone else for your help testing this. I've left #1106 open and maybe that would be a good place to start discussing how to display paths in Powershell on Windows.

@andytom andytom merged commit 02edad0 into starship:master Apr 30, 2020
dagbrown pushed a commit to dagbrown/starship that referenced this pull request Oct 22, 2021
…ip#1114)

* Avoid confusing modules with PowerShell paths

* Avoid confusing modules with PowerShell paths

Powershell supports PSDrives (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-7) that allow to create "logical" drives mapped to actual Windows drives.

* Preserve Windows directories

* Preserve logical paths for Powershell

* Fix formating with cargo fmt

* Fix directory_in_root test

Co-authored-by: Jean Gautier <jean.gautier@ssi.gouv.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants