question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Windows: backslashes passed to subprocess are unnecessarily unescaped.

See original GitHub issue

Please 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:closed
  • Created 6 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
meteorcloudycommented, Dec 11, 2017
0reactions
meteorcloudycommented, Dec 6, 2017

@yilei Sorry for the delay. @laszlocsomor Sure, I can take over. 😉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found