Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ by real Git Bash) passes 100%. Supported:

- Paths: `/c/foo` ↔ `C:\foo`, `/tmp` → `%TEMP%`, `/usr/bin`, `/bin`, `/ucrt64` → Git's
install dir, `/dev/null`, `/dev/stdin|stdout|stderr|fd/N`, `/dev/tty`. Windows paths
(`C:\x`, `C:/x`) work everywhere too. `$PWD` and `pwd` show the MSYS form.
(`C:\x`, `C:/x`) work everywhere too. `$PWD` and `pwd` show the MSYS form, `pwd -W` the
Windows one (`C:/...`).
- PATH is kept in MSYS form inside the shell and converted when launching programs.
- Arguments to native programs get MSYS2-style path conversion (`/c/x` → `C:/x`),
honoring `MSYS_NO_PATHCONV` and `MSYS2_ARG_CONV_EXCL`.
honoring `MSYS_NO_PATHCONV` and `MSYS2_ARG_CONV_EXCL` when they are exported or given as
a command prefix (`MSYS_NO_PATHCONV=1 cmd ...`), as in Git Bash.
- Arguments to MSYS programs are quoted so the MSYS runtime doesn't glob/brace/tilde-expand
or `@file`-expand them.
- Executables are found with or without `.exe`/`.com`/`.bat`/`.cmd`; extensionless files with a
Expand Down
39 changes: 30 additions & 9 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,25 +393,46 @@ fn cd(sh: &mut Shell, args: &[String]) -> R {
}

pub fn physical_pwd(sh: &Shell) -> String {
path::to_posix(&physical_cwd(sh))
}

/// The current directory with symlinks resolved, as a Windows path.
fn physical_cwd(sh: &Shell) -> std::path::PathBuf {
match std::fs::canonicalize(&sh.cwd) {
Ok(c) => {
let s = c.to_string_lossy().into_owned();
let s = s.strip_prefix(r"\\?\").unwrap_or(&s).to_string();
path::to_posix(std::path::Path::new(&s))
std::path::PathBuf::from(s.strip_prefix(r"\\?\").unwrap_or(&s))
}
Err(_) => path::to_posix(&sh.cwd),
Err(_) => sh.cwd.clone(),
}
}

fn pwd(sh: &mut Shell, args: &[String]) -> R {
let physical = args.iter().any(|a| a == "-P") || (sh.o[O_PHYSICAL] && !args.iter().any(|a| a == "-L"));
let p = if physical {
physical_pwd(sh)
} else {
match sh.get_str("PWD") {
// -L, -P and MSYS bash's -W (the physical directory in Windows form, `C:/...`) each choose a
// form; as in Git Bash, the last one given wins.
let mut form = if sh.o[O_PHYSICAL] { 'P' } else { 'L' };
for a in args {
if a == "--" || a == "-" || !a.starts_with('-') {
break;
}
for c in a[1..].chars() {
match c {
'L' | 'P' | 'W' => form = c,
_ => {
sh.eprint(&format!("pwd: -{}: invalid option", c));
sh.eprint("pwd: usage: pwd [-LPW]");
return Ok(2);
}
}
}
}
let p = match form {
'P' => physical_pwd(sh),
'W' => path::win_fwd(&physical_cwd(sh)),
_ => match sh.get_str("PWD") {
Some(p) if !p.is_empty() && path::to_win(&p, &sh.cwd).is_dir() => p,
_ => path::to_posix(&sh.cwd),
}
},
};
sh.out(1, format!("{}\n", p).as_bytes())?;
Ok(0)
Expand Down
19 changes: 16 additions & 3 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,9 +1494,22 @@ impl Shell {
let mut cmd;
let native;
let mut msys_child = false;
let no_conv = self.get_str("MSYS_NO_PATHCONV").map(|v| !v.is_empty()).unwrap_or(false);
let excl: Vec<String> = self
.get_str("MSYS2_ARG_CONV_EXCL")
// As in Git Bash, these count where the MSYS runtime would see them: in the environment
// the program starts with, so when exported or given as a prefix (`MSYS_NO_PATHCONV=1 cmd`).
let env_var = |name: &str| -> Option<String> {
match assigns.iter().rev().find(|(k, _)| k == name) {
Some((_, v)) => Some(v.clone()),
None => match self.vars.get(name) {
Some(v) if v.has(EXPORT) => match &v.value {
Some(Value::Str(s)) => Some(s.clone()),
_ => None,
},
_ => None,
},
}
};
let no_conv = env_var("MSYS_NO_PATHCONV").map(|v| !v.is_empty()).unwrap_or(false);
let excl: Vec<String> = env_var("MSYS2_ARG_CONV_EXCL")
.map(|v| v.split(';').filter(|s| !s.is_empty()).map(|s| s.to_string()).collect())
.unwrap_or_default();
let conv = |a: &str| -> String {
Expand Down
1 change: 1 addition & 0 deletions tests/scripts/072_pathconv_env_pwd_w.code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
12 changes: 12 additions & 0 deletions tests/scripts/072_pathconv_env_pwd_w.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'C:/x'
'/c/x'
'C:/x'
'/c/x'
'/c/x' 'D:/y'
'C:/x' '/d/y' '/e/z'
C:/Windows/System32
C:/Windows/System32
/c/Windows/System32
/c/Windows/System32
/c/Windows/System32
pwd -X: rc=2
24 changes: 24 additions & 0 deletions tests/scripts/072_pathconv_env_pwd_w.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# MSYS_NO_PATHCONV and MSYS2_ARG_CONV_EXCL count when exported or given as a command prefix,
# as in Git Bash. pwd -W prints the current directory in Windows form.
# Start from no conversion settings, whatever the environment exports (Claude Code sets one).
unset MSYS_NO_PATHCONV MSYS2_ARG_CONV_EXCL
git rev-parse --sq-quote /c/x

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Test isn't hermetic. If MSYS_NO_PATHCONV=1 is already exported when the suite runs, this line gets no conversion and the script fails. Claude Code exports it, and some Git for Windows setups do too. With the variable unset, it passes. Suggestion: start the script with unset MSYS_NO_PATHCONV MSYS2_ARG_CONV_EXCL. Or, to fix this for every script, add both names to REMOVED_ENV in tests/compat.rs and to regen_expected.ps1.

With the variable exported, lines 3 and 5 of the script both print '/c/x' where 'C:/x' is expected.

MSYS_NO_PATHCONV=1 git rev-parse --sq-quote /c/x
MSYS_NO_PATHCONV=1
git rev-parse --sq-quote /c/x
export MSYS_NO_PATHCONV
git rev-parse --sq-quote /c/x
unset MSYS_NO_PATHCONV
MSYS2_ARG_CONV_EXCL=/c git rev-parse --sq-quote /c/x /d/y
export MSYS2_ARG_CONV_EXCL='/d;/e'
git rev-parse --sq-quote /c/x /d/y /e/z
unset MSYS2_ARG_CONV_EXCL

cd /c/Windows/System32 || exit 1
pwd -W
pwd -LW
pwd -WP
pwd -P
pwd -L
pwd -X 2>/dev/null
echo "pwd -X: rc=$?"
Loading