-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Initial support for remote debugging with VSCode #3033
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
Open
romain-intel
wants to merge
1
commit into
master
Choose a base branch
from
romain/worktree/remote_debug
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import json | ||
| import os | ||
|
|
||
| from metaflow._vendor import click | ||
|
|
||
| METAFLOW_ATTACH_CONFIG = { | ||
| "name": "Metaflow: Attach", | ||
| "type": "debugpy", | ||
| "request": "attach", | ||
| "connect": {"host": "localhost", "port": 5678}, | ||
| "justMyCode": True, | ||
| "autoAttachChildProcesses": True, | ||
| } | ||
|
|
||
|
|
||
| @click.group() | ||
| def cli(): | ||
| pass | ||
|
|
||
|
|
||
| @cli.group(help="Commands related to debugging Metaflow flows.") | ||
| def debug(): | ||
| pass | ||
|
|
||
|
|
||
| @debug.group(help="VSCode debugger integration.") | ||
| def vscode(): | ||
| pass | ||
|
|
||
|
|
||
| @vscode.command( | ||
| "install-config", | ||
| help="Install VSCode debug configuration for attaching to Metaflow tasks.", | ||
| ) | ||
| @click.option( | ||
| "--base-port", | ||
| default=5678, | ||
| type=int, | ||
| show_default=True, | ||
| help="Port number for the debugpy attach configuration.", | ||
| ) | ||
| @click.option( | ||
| "--dir", | ||
| "target_dir", | ||
| default=".", | ||
| type=click.Path(), | ||
| help="Workspace root directory where .vscode/ will be created.", | ||
| ) | ||
| @click.option( | ||
| "--overwrite", | ||
| is_flag=True, | ||
| default=False, | ||
| help="Overwrite existing launch.json instead of merging.", | ||
| ) | ||
| @click.option( | ||
| "--remote-root", | ||
| default=None, | ||
| type=str, | ||
| help="Remote container root (e.g. /root/metaflow). Adds pathMappings for remote debugging.", | ||
| ) | ||
| def install_config(base_port, target_dir, overwrite, remote_root): | ||
| target_dir = os.path.abspath(target_dir) | ||
| vscode_dir = os.path.join(target_dir, ".vscode") | ||
| launch_path = os.path.join(vscode_dir, "launch.json") | ||
|
|
||
| our_config = dict(METAFLOW_ATTACH_CONFIG) | ||
| our_config["connect"] = {"host": "localhost", "port": base_port} | ||
|
|
||
| if remote_root is not None: | ||
| import metaflow | ||
|
|
||
| # Parent dir containing the metaflow package (e.g. site-packages or repo root). | ||
| local_metaflow_src = os.path.dirname(os.path.dirname(metaflow.__file__)) | ||
| our_config["pathMappings"] = [ | ||
| { | ||
| "localRoot": "${workspaceFolder}", | ||
| "remoteRoot": remote_root, | ||
| }, | ||
| { | ||
| "localRoot": local_metaflow_src, | ||
| "remoteRoot": remote_root + "/.mf_code", | ||
| }, | ||
| ] | ||
|
|
||
| if not os.path.isdir(vscode_dir): | ||
| os.makedirs(vscode_dir) | ||
|
|
||
| if os.path.exists(launch_path) and not overwrite: | ||
| with open(launch_path, "r") as f: | ||
| try: | ||
| existing = json.load(f) | ||
| except json.JSONDecodeError: | ||
| click.echo( | ||
| "Warning: existing launch.json is not valid JSON. " | ||
| "Use --overwrite to replace it.", | ||
| err=True, | ||
| ) | ||
| return | ||
|
|
||
| configs = existing.get("configurations", []) | ||
|
|
||
| # Check if our config already exists | ||
| for i, cfg in enumerate(configs): | ||
| if cfg.get("name") == "Metaflow: Attach": | ||
| if cfg != our_config: | ||
| configs[i] = our_config | ||
| existing["configurations"] = configs | ||
| with open(launch_path, "w") as f: | ||
| json.dump(existing, f, indent=4) | ||
| f.write("\n") | ||
| click.echo("Updated 'Metaflow: Attach' config in %s" % launch_path) | ||
| else: | ||
| click.echo( | ||
| "'Metaflow: Attach' config already up to date in %s" | ||
| % launch_path | ||
| ) | ||
| return | ||
|
|
||
| # Merge: append our config | ||
| configs.append(our_config) | ||
| existing["configurations"] = configs | ||
| with open(launch_path, "w") as f: | ||
| json.dump(existing, f, indent=4) | ||
| f.write("\n") | ||
| click.echo("Added 'Metaflow: Attach' config to existing %s" % launch_path) | ||
| else: | ||
| launch_json = { | ||
| "version": "0.2.0", | ||
| "configurations": [our_config], | ||
| } | ||
| with open(launch_path, "w") as f: | ||
| json.dump(launch_json, f, indent=4) | ||
| f.write("\n") | ||
| click.echo( | ||
| "Created %s with 'Metaflow: Attach' config (port %d)" | ||
| % (launch_path, base_port) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inner function defined inside loop on every iteration
_load_cmd_clsis defined anew on every iteration offor name in set_of_commands:. While this doesn't cause a bug here (it doesn't close over the loop variable), it unnecessarily creates a new function object each time and makes the structure harder to follow.Consider hoisting
_load_cmd_clsto module level or at least to the top ofresolve_cmds()so it's defined once.