internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: microsoft/ptvsd

Issue:
Map remoteRoot to current working directory if it is '.'



Discussion:

Resolving patch:
diff --git a/src/ptvsd/wrapper.py b/src/ptvsd/wrapper.py
--- a/src/ptvsd/wrapper.py
+++ b/src/ptvsd/wrapper.py
@@ -1577,11 +1577,19 @@ def _should_debug(self, filepath):
             return False
         return True
 
+    def _resolve_remote_root(self, local_root, remote_root):
+        if remote_root == '.':
+            cwd = os.getcwd()
+            append_pathsep = local_root.endswith('\\') or local_root.endswith('/')
+            return cwd + (os.path.sep if append_pathsep else '')
+        return remote_root
+
     def _initialize_path_maps(self, args):
         self._path_mappings = []
         for pathMapping in args.get('pathMappings', []):
             localRoot = pathMapping.get('localRoot', '')
             remoteRoot = pathMapping.get('remoteRoot', '')
+            remoteRoot = self._resolve_remote_root(localRoot, remoteRoot)
             if (len(localRoot) > 0 and len(remoteRoot) > 0):
                 self._path_mappings.append((localRoot, remoteRoot))
 


Test patch:
diff --git a/tests/func/test_path_mapping.py b/tests/func/test_path_mapping.py
--- a/tests/func/test_path_mapping.py
+++ b/tests/func/test_path_mapping.py
@@ -11,6 +11,52 @@
 from tests.helpers.timeline import Event
 
 
+def test_with_dot_remote_root(pyfile, tmpdir, run_as, start_method):
+    @pyfile
+    def code_to_debug():
+        from dbgimporter import import_and_enable_debugger
+        import_and_enable_debugger()
+        import os
+        import backchannel
+        backchannel.write_json(os.path.abspath(__file__))
+        print('done')
+
+    bp_line = 6
+    path_local = tmpdir.mkdir('local').join('code_to_debug.py').strpath
+    path_remote = tmpdir.mkdir('remote').join('code_to_debug.py').strpath
+
+    dir_local = os.path.dirname(path_local)
+    dir_remote = os.path.dirname(path_remote)
+
+    copyfile(code_to_debug, path_local)
+    copyfile(code_to_debug, path_remote)
+
+    with DebugSession() as session:
+        session.initialize(
+            target=(run_as, path_remote),
+            start_method=start_method,
+            ignore_unobserved=[Event('continued')],
+            use_backchannel=True,
+            path_mappings=[{
+                'localRoot': dir_local,
+                'remoteRoot': '.',
+            }],
+            cwd=dir_remote,
+        )
+        session.set_breakpoints(path_remote, [bp_line])
+        session.start_debugging()
+        hit = session.wait_for_thread_stopped('breakpoint')
+        frames = hit.stacktrace.body['stackFrames']
+        print('Local Path: ' + path_local)
+        print('Frames: ' + str(frames))
+        assert frames[0]['source']['path'] == Path(path_local)
+
+        remote_code_path = session.read_json()
+        assert path_remote == Path(remote_code_path)
+
+        session.send_request('continue').wait_for_response(freeze=False)
+        session.wait_for_exit()
+
 def test_with_path_mappings(pyfile, tmpdir, run_as, start_method):
     @pyfile
     def code_to_debug():
Repository: microsoft/ptvsd

Issue:
PTVSD_LOG_DIR doesn't work with VS
No logs are generated even with the environment variable set. It looks like logging initialization is missing on the VS entry point (`debugger.py`).


Discussion:
If this #1217 is done we don't need to patch `debugger.py`

Resolving patch:
diff --git a/src/ptvsd/debugger.py b/src/ptvsd/debugger.py
--- a/src/ptvsd/debugger.py
+++ b/src/ptvsd/debugger.py
@@ -4,6 +4,7 @@
 
 import sys
 
+import ptvsd.log
 from ptvsd._local import run_module, run_file, run_main
 
 
@@ -22,7 +23,10 @@
 
 def debug(filename, port_num, debug_id, debug_options, run_as,
           _runners=RUNNERS, _extra=None, *args, **kwargs):
-    # TODO: docstring
+
+    ptvsd.log.to_file()
+    ptvsd.log.info('debug{0!r}', (filename, port_num, debug_id, debug_options, run_as))
+
     if _extra is None:
         _extra = sys.argv[1:]
     address = (LOCALHOST, port_num)
@@ -39,5 +43,9 @@ def debug(filename, port_num, debug_id, debug_options, run_as,
 
 def run(filename, port_num, run_as,
         *args, **kwargs):
+
+    ptvsd.log.to_file()
+    ptvsd.log.info('run{0!r}', (filename, port_num, run_as))
+
     address = (LOCALHOST, port_num)
     run_main(address, filename, run_as, *args, **kwargs)


Test patch:
Repository: microsoft/ptvsd

Issue:
stopped event after goto should return reason as goto
Currently we return `paused`, which is not according to spec:
```
E (stopped): {"type": "event", "seq": 26, "event": "stopped", "body": {"reason": "pause", "threadId": 1, "text": null, "description": null, "preserveFocusHint": true, "allThreadsStopped": true}}
```


Discussion:
Also, stopped on entry should return 'entry' as reason.

Resolving patch:
diff --git a/src/ptvsd/wrapper.py b/src/ptvsd/wrapper.py
--- a/src/ptvsd/wrapper.py
+++ b/src/ptvsd/wrapper.py
@@ -1820,6 +1820,8 @@ def on_pydevd_thread_suspend_single_notification(self, seq, args):
             reason = 'exception'
         elif reason == pydevd_comm.CMD_SET_BREAK:
             reason = 'breakpoint'
+        elif reason == pydevd_comm.CMD_SET_NEXT_STATEMENT:
+            reason = 'goto'
         else:
             reason = 'pause'
 


Test patch:
diff --git a/tests/func/test_step.py b/tests/func/test_step.py
--- a/tests/func/test_step.py
+++ b/tests/func/test_step.py
@@ -80,7 +80,7 @@ def func():
 
         session.wait_for_next(Event('continued'))
 
-        stop = session.wait_for_thread_stopped()
+        stop = session.wait_for_thread_stopped(reason='goto')
         frames = stop.stacktrace.body['stackFrames']
         line = frames[0]['line']
         assert line == line_numbers['inner2']
Repository: microsoft/ptvsd

Issue:
Warning in ptvsd on python 3.8
## Environment data

- PTVSD version: 4.2.8
- OS and version: win10
- Python version (& distribution if applicable, e.g. Anaconda): cpython 3.8.0a3 32-bit
- Using VS Code or Visual Studio: VS

## Actual behavior

Warning prints:
```
c:\users\huvalo\appdata\local\microsoft\visualstudio\16.0_13ce7c9aexp\extensions\microsoft corporation\python\16.0.0\Packages\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_resolver.py:158: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if found.get(name) is not 1:
```

## Expected  behavior

No warning printed

## Steps to reproduce:
1. Start debugging a python console app



Discussion:

Resolving patch:
diff --git a/src/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py b/src/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py
--- a/src/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py
+++ b/src/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py
@@ -155,7 +155,7 @@ def _get_jy_dictionary(self, obj):
         try:
             d = dir(original)
             for name in d:
-                if found.get(name) is not 1:
+                if found.get(name) != 1:
                     ret[name] = getattr(original, name)
         except:
             # sometimes we're unable to do a dir


Test patch:
Repository: microsoft/ptvsd

Issue:
sys.stdin.read never returns when terminal set to raw mode 
## Environment data

- PTVSD version: 4.2.9
- OS and version: Kubuntu Linux kernel 5.0
- Python version (& distribution if applicable, e.g. Anaconda): 3.7.3
- Using VS Code or Visual Studio: VS Code

## Actual behavior

sys.stdin.read never returns, even if Enter key is pressed. This is only if the terminal is set to raw mode. Otherwise it works normally.

## Expected  behavior

sys.stdin.read should return characters that have been entered into stdin while in raw mode. This is a popular method of reading single characters without newlines in Linux as can be seen in these stackoverflow posts.

[Python read a single character from the user](https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user)
[Reading a single character (getch style) in Python is not working in Unix](https://stackoverflow.com/questions/1052107/reading-a-single-character-getch-style-in-python-is-not-working-in-unix)

## Steps to reproduce:
1. Toggle character reading via the short program below.

```import os
import sys
import termios
import tty

fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(fd)
# ch = os.read(fd, 1)           # Works
# ch = sys.stdin.buffer.read(1) # Works
ch = sys.stdin.read(1)          # Never returns!
print(ch)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
```
2. Run the program in the integrated terminal with launch.json settings as below.

```{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    }
  ]
}
```


Discussion:
I'll investigate.

Resolving patch:
diff --git a/src/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_console_utils.py b/src/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_console_utils.py
--- a/src/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_console_utils.py
+++ b/src/ptvsd/_vendored/pydevd/_pydev_bundle/pydev_console_utils.py
@@ -125,6 +125,13 @@ def readline(self, *args, **kwargs):
         self.__pydev_run_command(False)
         return result
 
+    def read(self, *args, **kwargs):
+        # Notify Java side about input and call original function
+        self.__pydev_run_command(True)
+        result = self.original_stdin.read(*args, **kwargs)
+        self.__pydev_run_command(False)
+        return result
+
 
 class CodeFragment:
     def __init__(self, text, is_single_line=True):


Test patch:
Repository: microsoft/ptvsd

Issue:
OOP Debug Adapter: setVariable, evaluate, setExpression



Discussion:

Resolving patch:
diff --git a/src/ptvsd/adapter/messages.py b/src/ptvsd/adapter/messages.py
--- a/src/ptvsd/adapter/messages.py
+++ b/src/ptvsd/adapter/messages.py
@@ -4,7 +4,7 @@
 
 from __future__ import absolute_import, print_function, unicode_literals
 
-
+import ptvsd
 from ptvsd.common import log, messaging, singleton
 from ptvsd.adapter import channels, debuggee, state
 
@@ -247,6 +247,31 @@ def disconnect(self):
                 # for us to respond, there's no rush.
                 debuggee.terminate(after=60)
 
+    @_only_allowed_while("running")
+    def pause_request(self, request):
+        request.arguments["threadId"] = "*"
+        self._server.delegate(request)
+        return {}
+
+    @_only_allowed_while("running")
+    def continue_request(self, request):
+        request.arguments["threadId"] = "*"
+        self._server.delegate(request)
+        return {"allThreadsContinued": True}
+
+    def on_ptvsd_systemInfo(self, request):
+        sys_info = {"ptvsd": {"version": ptvsd.__version__}}
+
+        try:
+            result = self._server.send_request("pydevdSystemInfo").wait_for_response()
+            sys_info.update(result)
+        except messaging.MessageHandlingError as exc:
+            request.cant_handle(
+                "System info request to server failed with: " + str(exc)
+            )
+
+        return sys_info
+
 
 class ServerMessages(Messages):
     """Message handlers and the associated global state for the server channel.


Test patch:
Repository: microsoft/ptvsd

Issue:
attach_socket_import tests hang waiting for "process" event
(https://github.com/microsoft/ptvsd/tree/dbg_adapter_refactor)

When running multiple tests with `start_method` set to `attach_socket_cmdline` or `attach_socket_import`, subsequent tests fail because they cannot reuse the port number. It looks like ptvsd is not getting shut down properly, or the next test starts running before ptvsd exits. 

Estimate is uncertain, because this needs to be investigated to determine the root cause.

Only happens on Python 2.7.


Discussion:
I misindentified the root cause. This is actually a consistent issue in all tests that use `start_method="attach_socket_import"`. For some as yet unclear reason, this gets ptvsd into such a condition whence the "process" DAP event never gets reported. And the implementation of `debug.Session` expects that event in `handshake()`.

Thus, the test hangs, and eventually times out. But debuggee process is still out there, and it still has the debug server in it listening on a port. Thus, the next test that does any form of "attach_socket" will fail because the port it needs is hogged.

The problem itself is specific to "attach_socket_import", though - "attach_socket_cmdline" works fine by itself. The only reason why cmdline was failing is because of the port being in use.


As a note, on pydevd I use a new port for each new test... Seeing the way that the port is gotten on ptvsd tests (`get_unique_port` in `helpers.py`), I think it's possible that the port being gotten from the `PYTEST_XDIST_WORKER` could be a port we can't really use (and it could be making tests fail).

As a reference, the way that `pydevd` gets new ports is in: `pydev_localhost.py - get_socket_name`.
The port logic itself shouldn't result in any clashes, because every worker uses its own distinct port, and every worker runs tests sequentially on that port.

This bears out in practice - DebugSession can connect to ptvsd, and starts talking to it. It also starts talking to pydevd, and e.g. successfully sets protocol to JSON. It breaks down sometime after, once it gets to handling "attach". 

Resolving patch:
diff --git a/src/ptvsd/__init__.py b/src/ptvsd/__init__.py
--- a/src/ptvsd/__init__.py
+++ b/src/ptvsd/__init__.py
@@ -24,6 +24,14 @@
 __file__ = path.abspath(__file__)
 del path
 
+# Preload encodings that we're going to use to avoid import deadlocks on Python 2.
+import codecs
+codecs.lookup('ascii')
+codecs.lookup('utf8')
+codecs.lookup('utf-8')
+codecs.lookup('latin1')
+codecs.lookup('latin-1')
+
 from ptvsd import _version
 __version__ = _version.get_versions()["version"]
 del _version


Test patch:
Repository: microsoft/ptvsd

Issue:
 'NoneType' object has no attribute 'pydev_notify_kill' on Flask with multiprocess implementation
## Environment data

- PTVSD version: 4.2.10
- OS and version: Ubuntu 18.04
- Python version (& distribution if applicable, e.g. Anaconda): Flask 0.12.2 with Python 3.6.8
- Using VS Code or Visual Studio: VS Code (1.36.0)

## Actual behavior
in `notify_thread_not_alive`:
`was_notified = thread.additional_info.pydev_notify_kill`
`'NoneType' object has no attribute 'pydev_notify_kill'`


## Expected  behavior

Should continue

## Steps to reproduce:
1. Using `multiprocessing.managers` class BaseManager

## Tried these:
1. 
`
import ptvsd
ptvsd.debug_this_thread()
`
2. 
`
ptvsd.enable_attach(address=('127.0.0.1', 7777), redirect_output=True)
ptvsd.wait_for_attach()
`
Sorry if this is a dumb report


Discussion:
Can you check if your debug configuration in launch.json is using all the necessary command line switches for Flask?

https://code.visualstudio.com/docs/python/debugging#_flask-debugging
@lorieaiah Can you provide details on the OS and python version etc? Also, any sort of repro steps would be helpful as well.

Resolving patch:
diff --git a/src/ptvsd/_vendored/pydevd/pydevd.py b/src/ptvsd/_vendored/pydevd/pydevd.py
--- a/src/ptvsd/_vendored/pydevd/pydevd.py
+++ b/src/ptvsd/_vendored/pydevd/pydevd.py
@@ -1240,9 +1240,10 @@ def notify_thread_not_alive(self, thread_id, use_lock=True):
             if thread is None:
                 return
 
-            was_notified = thread.additional_info.pydev_notify_kill
+            additional_info = set_additional_thread_info(thread)
+            was_notified = additional_info.pydev_notify_kill
             if not was_notified:
-                thread.additional_info.pydev_notify_kill = True
+                additional_info.pydev_notify_kill = True
 
         self.writer.add_command(self.cmd_factory.make_thread_killed_message(thread_id))
 


Test patch:
Repository: microsoft/ptvsd

Issue:
Test run times out on Python 2.7
(dbg_adapter_refactor branch only)

It's not just a CI issue - it repros locally on Linux, as well. The tests themselves complete fine, but something prevents the main pytest process from exiting - it gets stuck waiting for child nodes. Most likely, our test infrastructure does something on exit that blocks.


Discussion:

Resolving patch:
diff --git a/src/ptvsd/common/messaging.py b/src/ptvsd/common/messaging.py
--- a/src/ptvsd/common/messaging.py
+++ b/src/ptvsd/common/messaging.py
@@ -99,8 +99,16 @@ def close(self):
         """Closes the stream, the reader, and the writer.
         """
         self._is_closing = True
-        self._reader.close()
-        self._writer.close()
+
+        # Close the writer first, so that the other end of the connection has its
+        # message loop waiting on read() unblocked. If there is an exception while
+        # closing the writer, we still want to try to close the reader - only one
+        # exception can bubble up, so if both fail, it'll be the one from reader.
+        try:
+            self._writer.close()
+        finally:
+            if self._reader is not self._writer:
+                self._reader.close()
 
     def _log_message(self, dir, data, logger=log.debug):
         format_string = "{0} {1} " + (


Test patch:
diff --git a/tests/watchdog/__init__.py b/tests/watchdog/__init__.py
--- a/tests/watchdog/__init__.py
+++ b/tests/watchdog/__init__.py
@@ -107,10 +107,16 @@ def timeout():
     else:
         assert not timeout.occurred, str(timeout.occurred)
 
+
 def stop():
     if _stream is None:
         return
-    _invoke("stop")
+
+    try:
+        _invoke("stop")
+        _stream.close()
+    except Exception:
+        log.exception()
 
 
 def register_spawn(pid, name):
diff --git a/tests/watchdog/worker.py b/tests/watchdog/worker.py
--- a/tests/watchdog/worker.py
+++ b/tests/watchdog/worker.py
@@ -102,9 +102,29 @@ def main(tests_pid):
         raise log.exception()
 
     finally:
-        stream.close()
-        sys.stdout.close()
-        tests_process.wait()
+        try:
+            stream.close()
+        except Exception:
+            log.exception()
+
+        # If the test runner becomes a zombie process, it is still considered alive,
+        # and wait() will block indefinitely. Poll status instead.
+        while True:
+            try:
+                status = tests_process.status()
+            except Exception:
+                # If we can't even get its status, assume that it's dead.
+                break
+
+            # If it's dead or a zombie, time to clean it up.
+            if status in (psutil.STATUS_DEAD, psutil.STATUS_ZOMBIE):
+                break
+
+            # Otherwise, let's wait a bit to see if anything changes.
+            try:
+                tests_process.wait(0.1)
+            except Exception:
+                pass
 
         leftover_processes = {proc for proc, _ in spawned_processes.values()}
         for proc, _ in spawned_processes.values():
Repository: microsoft/ptvsd

Issue:
Deferred server events are not always propagated
Handlers for "launch" and "attach" requests in the adapter don't lock the session correctly. As a result, there's a race condition between the code that replays server events after responding to those requests, and the code that queues up server events for replaying - and it's possible for an event to be deferred after events have already been replayed, which results in it never being propagated.

"process" event is the one this affects most often, and when that happens, it breaks the debug session.


Discussion:

Resolving patch:
diff --git a/src/ptvsd/adapter/ide.py b/src/ptvsd/adapter/ide.py
--- a/src/ptvsd/adapter/ide.py
+++ b/src/ptvsd/adapter/ide.py
@@ -130,8 +130,7 @@ def initialize_request(self, request):
     # See https://github.com/microsoft/vscode/issues/4902#issuecomment-368583522
     # for the sequence of request and events necessary to orchestrate the start.
     def _start_message_handler(f):
-        f = components.Component.message_handler(f)
-
+        @components.Component.message_handler
         def handle(self, request):
             assert request.is_request("launch", "attach")
             if self._initialize_request is None:


Test patch:
Repository: microsoft/ptvsd

Issue:
Build failure on Windows when running setup_cython.py
```console
Traceback (most recent call last):

  File "setup_cython.py", line 134, in <module>


    build_extension("_pydevd_bundle", extension_name, target_pydevd_name, force_cython, extension_folder, True)

  File "setup_cython.py", line 95, in build_extension

    c_files,

UnboundLocalError: local variable 'c_files' referenced before assignment

```


Discussion:

Resolving patch:
diff --git a/src/ptvsd/_vendored/pydevd/setup_cython.py b/src/ptvsd/_vendored/pydevd/setup_cython.py
--- a/src/ptvsd/_vendored/pydevd/setup_cython.py
+++ b/src/ptvsd/_vendored/pydevd/setup_cython.py
@@ -64,7 +64,13 @@ def build_extension(dir_name, extension_name, target_pydevd_name, force_cython,
         assert os.path.exists(pyx_file)
 
     try:
+        c_files = [os.path.join(dir_name, "%s.c" % target_pydevd_name), ]
         if force_cython:
+            for c_file in c_files:
+                try:
+                    os.remove(c_file)
+                except:
+                    pass
             from Cython.Build import cythonize  # @UnusedImport
             # Generate the .c files in cythonize (will not compile at this point).
             cythonize([
@@ -74,17 +80,23 @@ def build_extension(dir_name, extension_name, target_pydevd_name, force_cython,
             # This is needed in CPython 3.8 to access PyInterpreterState.eval_frame.
             # i.e.: we change #include "pystate.h" to also #include "internal/pycore_pystate.h"
             # if compiling on Python 3.8.
-            c_files = [os.path.join(dir_name, "%s.c" % target_pydevd_name), ]
             for c_file in c_files:
                 with open(c_file, 'r') as stream:
                     c_file_contents = stream.read()
 
-                c_file_contents = c_file_contents.replace('#include "pystate.h"', '''#include "pystate.h"
+                if '#include "internal/pycore_pystate.h"' not in c_file_contents:
+                    c_file_contents = c_file_contents.replace('#include "pystate.h"', '''#include "pystate.h"
 #if PY_VERSION_HEX >= 0x03080000
 #include "internal/pycore_pystate.h"
 #endif
 ''')
+
+                # We want the same output on Windows and Linux.
                 c_file_contents = c_file_contents.replace('\r\n', '\n').replace('\r', '\n')
+                c_file_contents = c_file_contents.replace(r'_pydevd_frame_eval\\release_mem.h', '_pydevd_frame_eval/release_mem.h')
+                c_file_contents = c_file_contents.replace(r'_pydevd_frame_eval\\pydevd_frame_evaluator.pyx', '_pydevd_frame_eval/pydevd_frame_evaluator.pyx')
+                c_file_contents = c_file_contents.replace(r'_pydevd_bundle\\pydevd_cython.pxd', '_pydevd_bundle/pydevd_cython.pxd')
+                c_file_contents = c_file_contents.replace(r'_pydevd_bundle\\pydevd_cython.pyx', '_pydevd_bundle/pydevd_cython.pyx')
 
                 with open(c_file, 'w') as stream:
                     stream.write(c_file_contents)


Test patch: