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
Conversation
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.
There was a problem hiding this 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 (
starship/tests/testsuite/directory.rs
Lines 127 to 138 in d7acffa
#[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(()) | |
} |
Also I'm not sure but it looks like this will also fix #1106, is this correct? |
Sorry for the formatting errors & test failures ("cargo test" did not fail on my machine for a reason I still ignore). |
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. |
I don't think it works as expected yet. The root folder works fine by displaying 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 Getting rid of the 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. |
Hi davidkna, I noticed that behavior but I do not think it is a regression of this PR. Are you suggesting I add you to the members of my forked repo? Jean |
The removal of |
Yes, but the behavior showed with anything not c:\ like d:\ ... z:\ :-) Jean |
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). |
This PR tries to be as close as the previous behavior as possible while fixing annoying bugs for Windows Powershell users. |
@jeanga Thanks for your work. |
…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>
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.
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.
Checklist: