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

Shell expansion variables %~f0, %~p0, %~x0, %~s0, %~a0, %~t0, %~z0, %~dp0, %~nx0, %~fs0 and %~ftza0 are broken when script file is enclosed in quotes #15212

Closed
juj opened this issue Apr 20, 2023 · 4 comments
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Product-Cmd.exe The issue is related to the legacy command interpreter, CMD.exe. Resolution-By-Design It's supposed to be this way. Sometimes for compatibility reasons.

Comments

@juj
Copy link

juj commented Apr 20, 2023

Windows Terminal version

all

Windows build number

10.0.19041.508

Other Software

No response

Steps to reproduce

  1. Uncompress windows_terminal_broken_variable_expansion.zip
  2. Run correct.bat contained in the zip file and observe the generated print.
  3. Then run broken.bat and observe the generated print.

The zip file contains the following bat scripts:

testscript.bat

@echo off
echo.
:: these seem to work ok
echo Path of current script without quotes: %~0
echo Drive letter of current script: %~d0
echo Filename of current script: %~n0
:: the following all fail
echo Full path of current script: %~f0
echo Path of current script: %~p0
echo File extension of current script: %~x0
echo Short name path of current script: %~s0
echo File attributes of current script: %~a0
echo Date/time of current script: %~t0
echo Byte size of current script: %~z0
echo Drive + Absolute path of current script: %~dp0
echo Filename + extension of current script: %~nx0
echo Full short path name of current script: %~fs0
echo Curdir listing of current script: %~ftza0

correct.bat

@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
testscript

broken.bat

@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
"testscript"

Expected Behavior

Both correct.bat and broken.bat should print out exact same output.

Running correct.bat prints the expected behavior:

E:\code\emsdk\19207>correct.bat

Path of current script without quotes: testscript
Drive letter of current script: E:
Filename of current script: testscript
Full path of current script: E:\code\emsdk\19207\subdir\testscript.bat
Path of current script: \code\emsdk\19207\subdir\
File extension of current script: .bat
Short name path of current script: E:\code\emsdk\19207\subdir\TESTSC~1.BAT
File attributes of current script: --a--------
Date/time of current script: 04/20/2023 02:17 PM
Byte size of current script: 699
Drive + Absolute path of current script: E:\code\emsdk\19207\subdir\
Filename + extension of current script: testscript.bat
Full short path name of current script: E:\code\emsdk\19207\subdir\TESTSC~1.BAT
Curdir listing of current script: --a-------- 04/20/2023 02:17 PM 699 E:\code\emsdk\19207\subdir\testscript.bat

Actual Behavior

broken.bat will print out incorrect expansion of the shell expression variables ~f0, %~p0, %~x0, %~s0, %~a0, %~t0, %~z0, %~dp0, %~nx0, %~fs0 and %~ftza0. In that script, the invocation of the test script is enclosed in quotes, which is sometimes necessary e.g. when a path contains spaces in it.

In other words, calling testscript.bat works, but calling "testscript.bat" does not.

Running broken.bat prints the incorrect behavior:

E:\code\emsdk\19207>broken.bat

Path of current script without quotes: testscript
Drive letter of current script: E:
Filename of current script: testscript
Full path of current script: E:\code\emsdk\19207\testscript
Path of current script: \code\emsdk\19207\
File extension of current script:
Short name path of current script: E:\code\emsdk\19207\testscript
File attributes of current script:
Date/time of current script:
Byte size of current script:
Drive + Absolute path of current script: E:\code\emsdk\19207\
Filename + extension of current script: testscript
Full short path name of current script: E:\code\emsdk\19207\testscript
Curdir listing of current script: E:\code\emsdk\19207\testscript

Note the contents of correct.bat:

@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
testscript

and broken.bat is identical, but it just encloses the invocation of testscript in quotes:

@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
"testscript"

which should not have any functional difference, yet the behavior of the scripts differ.

@juj juj added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Apr 20, 2023
@zadjii-msft
Copy link
Member

Wait, just to confirm - does this repro the same way in both conhost (the vintage console window), and the Windows Terminal/?

If so, I'd be inclined to say that this is just the way cmd.exe works...

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Apr 20, 2023
@juj
Copy link
Author

juj commented Apr 20, 2023

Wait, just to confirm - does this repro the same way in both conhost (the vintage console window), and the Windows Terminal/?

Yes

If so, I'd be inclined to say that this is just the way cmd.exe works...

I don't know if Windows Terminal developers would be the same as cmd.exe developers, but if this is not the right place, would you be able to escalate the issue to the relevant developers of cmd.exe inside Microsoft?

It does not seem that this should be shrugged off as a case of "broken by design".

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Apr 20, 2023
@zadjii-msft
Copy link
Member

Oh I'm so sorry to be the person that lets you know, but yea, cmd.exe isn't actively maintained anymore (beyond security patches). Yea, the Terminal team does own cmd.exe, but we're not gonna be making any changes to it anytime soon. You can read some of our reasoning here: Why do we avoid changing CMD.exe?. It's literally never worked for us without secretly breaking someone else.

If you'd like a modern shell, PowerShell is really quite good. Yori and Clink are some other team favorites that are more cmd-like.

Sorry 😕

@zadjii-msft zadjii-msft closed this as not planned Won't fix, can't repro, duplicate, stale Apr 20, 2023
@zadjii-msft zadjii-msft added Product-Cmd.exe The issue is related to the legacy command interpreter, CMD.exe. Resolution-By-Design It's supposed to be this way. Sometimes for compatibility reasons. labels Apr 20, 2023
juj added a commit to juj/emscripten that referenced this issue Apr 20, 2023
…n is broken, by attempting to look up emcc first in EMSCRIPTEN, and then in PATH if it cannot be expanded to via %~dp0. Add a new test other.test_windows_batch_file_dp0_expansion_bug to check for this scenario.
juj added a commit to emscripten-core/emscripten that referenced this issue Apr 22, 2023
* Work around Windows bug microsoft/terminal#15212 where %~dp0 expansion is broken, by attempting to look up emcc first in EMSCRIPTEN, and then in PATH if it cannot be expanded to via %~dp0. Add a new test other.test_windows_batch_file_dp0_expansion_bug to check for this scenario.

* Update bat scripts to avoid ':' inside if() blocks.
@SilvoWeiss
Copy link

When calling TestScript why are you not using full filename with included extension. Calling both TestScript.bat and "TestScript.bat" works exactly the same as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Product-Cmd.exe The issue is related to the legacy command interpreter, CMD.exe. Resolution-By-Design It's supposed to be this way. Sometimes for compatibility reasons.
Projects
None yet
Development

No branches or pull requests

3 participants