Windows: backslashes passed to subprocess are unnecessarily unescaped.
See original GitHub issuePlease provide the following information. The more we know about your system and use case, the more easily and likely we can help.
Description of the problem / feature request / question:
I am running the generated .exe file of a py_binary, which is a data deps of another py_test. The backslashes in the arguments passed to the .exe file end up unnecessarily unescaped in the py_binary.
If possible, provide a minimal example to reproduce the problem:
BUILD:
py_binary(
name = "sub",
srcs = ["sub.py"],
)
py_test(
name = "main",
srcs = ["main.py"],
data = [":sub"],
)
main.py:
import os
import subprocess
import unittest
class MainTest(unittest.TestCase):
def testMain(self):
test_srcdir = os.getenv('TEST_SRCDIR')
with open(os.path.join(test_srcdir, 'MANIFEST'), 'r') as f:
for line in f:
tokens = line.strip().split(' ')
if len(tokens) == 2 and tokens[0] == '__main__/sub.exe':
sub_path = tokens[1]
break
subprocess.check_call([sub_path, r'path\to\dir'])
if __name__ == '__main__':
unittest.main()
sub.py:
import sys
if sys.argv[1] != r'path\to\dir':
raise RuntimeError('Unexpected sys.argv[1]: {}\n'.format(sys.argv[1]))
bazel test :main
fails, because sys.argv[1]
is actually r'path\\to\\dir'
instead of r'path\to\dir'
.
Environment info
-
Operating System: Windows Server 2016
-
Bazel version (output of
bazel info release
): release 0.7.0
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (14 by maintainers)
Top Results From Across the Web
Struggling with backslashes in subprocess.run - Stack Overflow
I am using shell=True because the documentation says I should when using command built into the shell.
Read more >PowerShell stripping double quotes from command line ...
To pass a one or more backslashes followed by a double quote, escape each backslash with another backslash and escape the quote: \\\\\"...
Read more >[Python-Dev] subprocess not escaping "^" on Windows
CMD doesn't recognize backslash as an escape character, which gives rise to a quoting conflict between CMD and the application. Some ...
Read more >Dockerfile reference - Docker Documentation
Dockerfile reference. Docker can build images automatically by reading the instructions from a Dockerfile . A Dockerfile is a text document that contains ......
Read more >GNU make
Comments within a recipe are passed to the shell, just as with any ... Read in a full logical line, including backslash-escaped lines...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just sent a fix to review: https://bazel-review.googlesource.com/#/c/bazel/+/26710/
@yilei Sorry for the delay. @laszlocsomor Sure, I can take over. 😉