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

Ensure command string is escaped correctly when executing in windows cmd shell #1111

Conversation

afrocircus
Copy link

Problem:

The following does not execute correctly when run in a windows cmd prompt.

rez-env python -- echo "<hey>"
The system cannot find the file specified.

This is because the batch file that is written out is without the escaped chars.

set REZ_ENV_PROMPT=%REZ_ENV_PROMPT%$G
echo %PATHEXT%|C:\Windows\System32\findstr.exe /i /c:".PY">nul || set PATHEXT=%PATHEXT%;.PY
(call )
call C:\..\AppData\Local\Temp\rez_context_fiqpunrh\context.bat
set REZ_STORED_PROMPT_CMD=$P$G
set PROMPT=%REZ_ENV_PROMPT% $P$G
echo <Hey>
exit %errorlevel%

what we need is the batch file to contain.
echo ^<Hey^>

Solution:

Ensure that the command run via rez-env is escaped correctly when executing in a windows cmd shell.

@nerdvegas
Copy link
Contributor

Ok I've looked into this, it is more nuanced that it appears.

I think escaping the command like this is not the correct approach. The problem is that you cannot know which part of any given command may need to be escaped. Consider the command on linux: rez-env -- bash -c 'anything-in-here'. If we escape any part of anything-in-here, we could break the command, or backwards compatibility, or both.

Furthermore, the existing bash shell works even without escaping. Ie this works: rez-env -- echo "!hey" (but running echo !hey directly will fail).

The reason this works is because the Shell class's join function is applied to the command, see https://github.com/nerdvegas/rez/blob/master/src/rez/resolved_context.py#L1388. I think the problem is cmd's implementation of this, see https://github.com/nerdvegas/rez/blob/master/src/rezplugins/shell/cmd.py#L356. I think this function is not adding quotes to strings that need it, such as <hey>.

I don't recall why cmd has a different join implementation and why it doesn't use shlex_join as other shells do. Interestingly though I did find a bug in the unix shell impl, this fails: rez-env -- echo '$>'.

I'm going to close this PR due to the issues I've raised here, but the issue isn't resolved, I think Cmd.join needs fixing (I don't work on Windows though so my own attempt at a fix will probably be hamfisted).

@nerdvegas nerdvegas closed this Aug 3, 2021
@nerdvegas nerdvegas mentioned this pull request Aug 3, 2021
@nerdvegas
Copy link
Contributor

#1115

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.

2 participants