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

Pass PTVSD folder name to the new debug adapter executable #7152

Merged
merged 49 commits into from
Sep 23, 2019

Conversation

kimadeline
Copy link

For #7001

  • Pull request represents a single change (i.e. not fixing disparate/unrelated things in a single PR)
  • Title summarizes what is changing
  • Has a news entry file (remember to thank yourself!)
  • Appropriate comments and documentation strings in the code
  • Has sufficient logging.
  • Has telemetry for enhancements.
  • Unit tests & system/integration tests are added/updated
  • Test plan is updated as appropriate
  • package-lock.json has been regenerated by running npm install (if dependencies have changed)
  • The wiki is updated with any design decisions/details.

@kimadeline kimadeline added the no-changelog No news entry required label Aug 29, 2019
// traceVerbose(`Using Python Debug Adapter with PTVSD ${ptvsdPathToUse}`);
// return new DebugAdapterExecutable(pythonPath, [path.join(ptvsdPathToUse, 'adapter'), ...logArgs]);
return new DebugAdapterExecutable(pythonPath);
const ptvsdPathToUse = await this.getPtvsdFolder(pythonPath).then(output => output.stdout.trim());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we get the ptvsd launcher to call the ptvsd-folder script. That's it's one less process that's started for every debug session, hence faster launching the debugger, etc .. (I.e move the code into the same Python process)

Copy link
Author

@kimadeline kimadeline Aug 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So ptvsd_launcher.py would look like this?

if '--experiment' flag:
   # import get_folder_name from ptvsd_folder_name.py first
   folder_name = get_folder_name()
   ptvsd = import folder_name/ptvsd/adapter/__main__.py
   ptvsd.main(ptvsd_args) 
else:
   launch_old_ptvsd() # the old code

Since the folder name contains periods (for example ptvsd-5.0.0a3-cp37-cp37m-macosx_10_13_x86_64) I can't seem to be able to use import(folder_name) or importlib.import_module(folder_name). So far I am using imp.load_source locally, however this displays a warning (doesn't prevent the code from running though):

DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses

There's also this snippet in adapter/__main__.py that makes it sound like when executing the file there is some setup & arg parsing first (it parses sys.argv but we have our ptvsd_launcher args in it), which we should then do if we call main() ourselves in ptvsd_launcher.py:

if __name__ == "__main__":
    # ptvsd can also be invoked directly rather than via -m. In this case, the first
    # entry on sys.path is the one added automatically by Python for the directory
    # containing this file. This means that import ptvsd will not work, since we need
    # the parent directory of ptvsd/ to be in sys.path, rather than ptvsd/adapter/.
    #
    # The other issue is that many other absolute imports will break, because they
    # will be resolved relative to ptvsd/adapter/ - e.g. `import state` will then try
    # to import ptvsd/adapter/state.py.
    #
    # To fix both, we need to replace the automatically added entry such that it points
    # at parent directory of ptvsd/ instead of ptvsd/adapter, import ptvsd with that
    # in sys.path, and then remove the first entry entry altogether, so that it doesn't
    # affect any further imports we might do. For example, suppose the user did:
    #
    #   python /foo/bar/ptvsd/adapter ...
    #
    # At the beginning of this script, sys.path will contain "/foo/bar/ptvsd/adapter"
    # as the first entry. What we want is to replace it with "/foo/bar', then import
    # ptvsd with that in effect, and then remove the replaced entry before any more
    # code runs. The imported ptvsd module will remain in sys.modules, and thus all
    # future imports of it or its submodules will resolve accordingly.
    if "ptvsd" not in sys.modules:
        # Do not use dirname() to walk up - this can be a relative path, e.g. ".".
        sys.path[0] = sys.path[0] + "/../../"
        __import__("ptvsd")
        del sys.path[0]

    # Load locale settings.
    locale.setlocale(locale.LC_ALL, "")

    main(_parse_argv())

Using the imp package & cleaning up sys.argv manually (sys.argv.pop(1)) feels icky, is that the correct way to proceed?

(i could also replace the .s in the folder name with - or _ and call it a day...)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericsnowcurrently suggested not using ptvsd_launcher.py to call the ptvsd-folder script, but instead call it only once and cache the result.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern was two fold, we're exposing unnecessary information into another layer, and unnecessary perf cost.
Fetching the information in the python process is next to negligable. Now running a separate python process, caching, etc introduces more moving parts and more room for error.

I.e. launcher script isn't self contained.

More problems of exposing this in other layers is the fact that we now have to modify the code in launcherProvider.ts to pass this path to ptvsd. I.e. were modifying these places to pass additional arguments and do more stuff. Doesn't feel right to keep changing such code. I.e. modifying to keep same functionality.

If going down this path then we need to make more changes to get the path to ptvsd passed in other locations, etc... Feels like a lot of work (changes & leaking of concerns) when it can be done in the launcher.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please modify launchProvider.ts as well to pass this info in there.
More unnecessary changes in other places that could be avoided if it was done in one python file with just tests for one python file (lot less changes).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussing with @ericsnowcurrently , will be addressed in #7549.

@codecov-io

This comment has been minimized.

@kimadeline kimadeline marked this pull request as ready for review September 5, 2019 15:49
Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM. I've left comments where I think it's worth looking at changing something, though not all comments are for things I expect to change. I didn't find anything wrong (other than missing Python tests), but did have a number of questions and observations. I'd be glad to talk over any of it. :)

Also, for the Python script I've noted shorter names that are more typical. Keep in mind that underscores are mostly used only in constants and function names, and not in variables.

Note that Python tests go under pythonFiles/tests.

src/client/debugger/extension/adapter/factory.ts Outdated Show resolved Hide resolved
src/client/debugger/extension/adapter/factory.ts Outdated Show resolved Hide resolved
*
* Return a cached path otherwise.
*
* @private
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I don't see the value of all this extra stuff in the doc comment. The info is represented in the code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by extra stuff, the jsdoc params or the description?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdoc params

src/client/debugger/extension/adapter/factory.ts Outdated Show resolved Hide resolved
src/client/debugger/extension/adapter/factory.ts Outdated Show resolved Hide resolved
src/client/debugger/extension/adapter/factory.ts Outdated Show resolved Hide resolved
pythonFiles/ptvsd_folder_name.py Outdated Show resolved Hide resolved
pythonFiles/ptvsd_folder_name.py Outdated Show resolved Hide resolved
pythonFiles/ptvsd_folder_name.py Show resolved Hide resolved
pythonFiles/ptvsd_folder_name.py Show resolved Hide resolved
Copy link

@DonJayamanne DonJayamanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address changes (modify the other ts file) mentioned in #7152 (comment)

Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM. I have a number of comments for you to consider, but nothing critical. I'd be glad to talk about any of it with you. :)

Also, thanks for adding both unit tests and functional tests!

pythonFiles/install_ptvsd.py Show resolved Hide resolved
pythonFiles/install_ptvsd.py Show resolved Hide resolved
pythonFiles/tests/debug_adapter/test_ptvsd_folder_name.py Outdated Show resolved Hide resolved
pythonFiles/tests/debug_adapter/test_ptvsd_folder_name.py Outdated Show resolved Hide resolved
Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I've left a few comments, but feel free to do what you will with them. I'll mark this as approved.

@kimadeline kimadeline dismissed DonJayamanne’s stale review September 23, 2019 17:46

Will address remote debugging in #7549

@kimadeline kimadeline merged commit ed38ea2 into microsoft:master Sep 23, 2019
@kimadeline kimadeline deleted the 7001-new-ptvsd-folder-name branch September 23, 2019 18:55
@kimadeline kimadeline mentioned this pull request Sep 23, 2019
10 tasks
@lock lock bot locked as resolved and limited conversation to collaborators Sep 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
no-changelog No news entry required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants