Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/build-vsix/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ runs:
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-build-vsix-${{ hashFiles('**/requirements.txt') }}
key: ${{ runner.os }}-pip-build-vsix-${{ hashFiles('noxfile.py') }}
restore-keys: |
${{ runner.os }}-pip-build-vsix-

Expand Down
2 changes: 1 addition & 1 deletion .github/actions/lint/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ runs:
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-lint-${{ hashFiles('**/requirements.txt') }}
key: ${{ runner.os }}-pip-lint-${{ hashFiles('noxfile.py') }}
restore-keys: |
${{ runner.os }}-pip-lint-

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules
.venv/
.nox/
bundled/libs/
bundled/.debugpy-build/
**/__pycache__
**/.pytest_cache
**/.vs
Expand Down
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ vsc-extension-quickstart.md
**/__pycache__/**
**/.pyc
bundled/libs/bin/**
bundled/.debugpy-build/**
noxfile.py
.pytest_cache/**
.pylintrc
Expand Down
88 changes: 88 additions & 0 deletions build/test_bundled_libs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import tempfile
import unittest
import zipfile
from pathlib import Path
from unittest.mock import Mock, patch

import noxfile


def download_test_wheel(*args):
dest = Path(args[args.index("--dest") + 1])
with zipfile.ZipFile(dest / "debugpy-test.whl", "w") as wheel:
wheel.writestr("debugpy/new.py", b"new")


class BundleReplacementTests(unittest.TestCase):
def test_failed_install_restores_previous_bundle_and_allows_retry(self):
with tempfile.TemporaryDirectory() as tmp, patch.object(
Path, "cwd", return_value=Path(tmp)
):
libs = Path(tmp) / "bundled" / "libs"
libs.mkdir(parents=True)
old = libs / "old.py"
old.write_bytes(b"old")
requests = noxfile._build_debugpy_wheel_requests(
"", noxfile.DEBUGPY_VERSION
)

session = Mock()
session.run.side_effect = download_test_wheel
rename = Path.rename

def fail_install(source, target):
if source.name == "libs" and target == libs:
raise PermissionError("install blocked")
return rename(source, target)

with patch.object(Path, "rename", fail_install):
with self.assertRaisesRegex(PermissionError, "install blocked"):
noxfile.download_debugpy_via_pip(session, requests)

self.assertEqual(old.read_bytes(), b"old")
noxfile.download_debugpy_via_pip(session, requests)
self.assertFalse(old.exists())
self.assertEqual((libs / "debugpy" / "new.py").read_bytes(), b"new")

def test_retry_cleans_leftover_backup_when_bundle_is_installed(self):
with tempfile.TemporaryDirectory() as tmp, patch.object(
Path, "cwd", return_value=Path(tmp)
):
libs = Path(tmp) / "bundled" / "libs"
libs.mkdir(parents=True)
(libs / "installed.py").write_bytes(b"installed")
backup = libs.parent / ".debugpy-build" / "previous"
backup.mkdir(parents=True)
(backup / "locked.py").write_bytes(b"left over")

session = Mock()
session.run.side_effect = download_test_wheel
noxfile.download_debugpy_via_pip(
session,
noxfile._build_debugpy_wheel_requests("", noxfile.DEBUGPY_VERSION),
)

self.assertFalse(backup.exists())
self.assertEqual((libs / "debugpy" / "new.py").read_bytes(), b"new")

def test_retry_preserves_only_remaining_bundle(self):
with tempfile.TemporaryDirectory() as tmp, patch.object(
Path, "cwd", return_value=Path(tmp)
):
backup = Path(tmp) / "bundled" / ".debugpy-build" / "previous"
backup.mkdir(parents=True)
old = backup / "old.py"
old.write_bytes(b"recover me")

session = Mock()
with self.assertRaises(FileExistsError):
noxfile.download_debugpy_via_pip(
session,
noxfile._build_debugpy_wheel_requests("", noxfile.DEBUGPY_VERSION),
)

session.run.assert_not_called()
self.assertEqual(old.read_bytes(), b"recover me")
58 changes: 38 additions & 20 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
import pathlib
import re
import shutil
import sys
import tempfile
import zipfile

Expand Down Expand Up @@ -119,29 +121,23 @@ def lint(session: nox.Session) -> None:
@nox.session()
def tests(session: nox.Session) -> None:
"""Runs all the tests for the extension."""
session.install("-r", "./requirements.txt")
session.run(
sys.executable,
"-m",
"unittest",
"discover",
"-s",
"build",
"-p",
"test_bundled_libs.py",
external=True,
)
session.run("npm", "run", "test")


@nox.session()
def install_bundled_libs(session):
"""Installs the libraries that will be bundled with the extension."""
session.install("wheel")
session.install(
"-t",
"./bundled/libs",
"--no-cache-dir",
"--implementation",
"py",
"--no-deps",
"--require-hashes",
"--only-binary",
":all:",
"-r",
"./requirements.txt",
)
session.install("packaging")

target = os.environ.get("VSCETARGET", "")
Comment thread
dmitrivMS marked this conversation as resolved.
Resolved
print("target:", target)

Expand All @@ -156,13 +152,21 @@ def download_debugpy_via_pip(session: nox.Session, requests: list[dict]) -> None
index URLs (e.g., Azure Artifacts feed) instead of direct PyPI URLs.
"""
libs_dir = pathlib.Path.cwd() / "bundled" / "libs"
libs_dir.mkdir(parents=True, exist_ok=True)

if not requests:
raise ValueError("No debugpy wheel requests were provided.")
version = requests[0]["version"]

with tempfile.TemporaryDirectory(prefix="debugpy_wheels_") as tmp_dir:
work_dir = libs_dir.parent / ".debugpy-build"
work_dir.mkdir(parents=True, exist_ok=True)
previous_libs = work_dir / "previous"
if previous_libs.exists():
if not libs_dir.is_dir():
raise FileExistsError(
f"Recover or remove the previous debugger bundle at {previous_libs} before retrying."
)
shutil.rmtree(previous_libs)
with tempfile.TemporaryDirectory(prefix="debugpy_wheels_", dir=work_dir) as tmp_dir:
tmp_path = pathlib.Path(tmp_dir)

for req in requests:
Expand Down Expand Up @@ -223,12 +227,26 @@ def download_debugpy_via_pip(session: nox.Session, requests: list[dict]) -> None
f"pip download produced no debugpy wheels for version {version}."
)

staged_libs = tmp_path / "libs"
for wheel_path in wheel_paths:
print("Downloaded:", wheel_path.name)
with zipfile.ZipFile(wheel_path, "r") as wheel:
for zip_info in wheel.infolist():
print("\t" + zip_info.filename)
wheel.extract(zip_info.filename, libs_dir)
wheel.extract(zip_info.filename, staged_libs)

# Keep the backup outside tmp_dir so even a failed rollback can't discard it.
if not libs_dir.exists():
staged_libs.rename(libs_dir)
return

libs_dir.rename(previous_libs)
try:
staged_libs.rename(libs_dir)
except OSError:
previous_libs.rename(libs_dir)
raise
shutil.rmtree(previous_libs)


@nox.session()
Expand Down
Loading
Loading