Skip to content

Commit b192cdf

Browse files
edvilmeCopilotCopilot
authored
Generate published API entry point from src/api.ts (#1619)
## Summary The npm package `@vscode/python-environments` previously duplicated the **entire** API definition in `api/src/main.ts` — a near-copy of `src/api.ts` that was hand-maintained and had already drifted (dozens of differing lines). This PR makes **`src/api.ts` the single source of truth**, stops committing the duplicate, and adds CI guardrails so public API changes are versioned and documented. ## What changed - **`src/api.ts`** — folded in the publish-only runtime helper so it is now the complete public surface: the `PythonEnvironments.api()` helper and `EXTENSION_ID`. The extension itself never calls this helper (it *implements* the API), so it is harmless dead code in the extension bundle; a bonus is that consumers who copy `src/api.ts` (per `examples/README.md`) now also get the helper. - **`api/src/main.ts`** — removed. It is now generated at publish time and ignored via **`api/.gitignore`**. - **`build/azure-pipeline.npm.yml`** — added a step that copies `src/api.ts` to `api/src/main.ts` before compiling the package. - **`api/package.json`** / **`api/package-lock.json`** — bumped to `1.37.0` to match the extension version. - **`api/CHANGELOG.md`** — new changelog for the published package (Keep a Changelog format). - **`scripts/compare_package_versions.py`** — new script that verifies the extension (`package.json`) and API package (`api/package.json`) declare the same version. - **`.github/workflows/pr-file-check.yml`** — added CI guardrails (see below). - **`CONTRIBUTING.md`** — documents the copy model and the versioning/changelog rules. ## How it works now The publish pipeline runs: `npm install` -> **`cp ../src/api.ts src/main.ts`** -> `npm run compile` -> `npm pack --ignore-scripts`. `src/api.ts` is validated on every PR by the extension's own lint + `tsc`. ## CI guardrails - **Version match** — a `version-match` job runs `scripts/compare_package_versions.py` and fails if `package.json` and `api/package.json` versions differ. - **API change requires a version bump** — if a PR edits `src/api.ts`, `api/package.json` must also change. Bypass with the `skip api version` label. - **API change requires a changelog entry** — if a PR edits `src/api.ts`, `api/CHANGELOG.md` must also change. Bypass with the `skip api changelog` label. ## Trade-off The api package now builds **only in the Linux publish pipeline**. To build it locally, copy the file first (`cp src/api.ts api/src/main.ts`) — documented in CONTRIBUTING.md. ## Validation - `npm run lint` and `tsc -p . --noEmit` pass with the helper folded into `src/api.ts`. - The `cp` step produces a byte-identical `api/src/main.ts`. - `scripts/compare_package_versions.py` verified for both matching (exit 0) and mismatching (exit 1) versions. - Built old vs. new npm tarballs and ran an isolated consumer against each (CJS + ESM type-check and runtime smoke): behavior is identical. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 2bfc9ef commit b192cdf

10 files changed

Lines changed: 170 additions & 1438 deletions

File tree

‎.github/workflows/pr-file-check.yml‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,36 @@ jobs:
4141
src/**/*.unit.test.ts
4242
skip-label: 'skip tests'
4343
failure-message: 'TypeScript code was edited without also editing a test file (the ${skip-label} label can be used to pass this check)'
44+
45+
- name: 'Public API changes require a version bump'
46+
uses: brettcannon/check-for-changed-files@871d7b8b5917a4f6f06662e2262e8ffc51dff6d1 # v1.2.1
47+
with:
48+
prereq-pattern: 'src/api.ts'
49+
file-pattern: 'api/package.json'
50+
skip-label: 'skip api version'
51+
failure-message: 'The public API (${prereq-pattern}) was changed without bumping the package version in ${file-pattern} (the ${skip-label} label can be used to pass this check)'
52+
53+
- name: 'Public API changes require a changelog entry'
54+
uses: brettcannon/check-for-changed-files@871d7b8b5917a4f6f06662e2262e8ffc51dff6d1 # v1.2.1
55+
with:
56+
prereq-pattern: 'src/api.ts'
57+
file-pattern: 'api/CHANGELOG.md'
58+
skip-label: 'skip api changelog'
59+
failure-message: 'The public API (${prereq-pattern}) was changed without a changelog entry in ${file-pattern} (the ${skip-label} label can be used to pass this check)'
60+
61+
version-match:
62+
name: 'Extension and API package versions match'
63+
runs-on: ubuntu-latest
64+
permissions:
65+
contents: read
66+
steps:
67+
- name: Checkout
68+
uses: actions/checkout@v4
69+
70+
- name: Set up Python
71+
uses: actions/setup-python@v5
72+
with:
73+
python-version: '3.x'
74+
75+
- name: 'Compare package.json versions'
76+
run: python scripts/compare_package_versions.py

‎CONTRIBUTING.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ This project requires contributors to sign a Contributor License Agreement (CLA)
8989

9090
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions.
9191

92+
## Public API package (`@vscode/python-environments`)
93+
94+
The npm package under [`api/`](./api) is the public API facade other extensions consume. Its entry point, `api/src/main.ts`, is a **copy** of [`src/api.ts`](./src/api.ts) — the single source of truth — and is **not committed** (see [`api/.gitignore`](./api/.gitignore)).
95+
96+
- Edit the API only in `src/api.ts`. This file contains the full public surface, including the runtime `PythonEnvironments.api()` helper and `EXTENSION_ID`. `api/src/main.ts` is a build artifact — never edit or commit it.
97+
- `api/src/main.ts` is produced by the publish pipeline ([`build/azure-pipeline.npm.yml`](./build/azure-pipeline.npm.yml)), which copies `src/api.ts` to `api/src/main.ts` before compiling. The api package is therefore built in CI only; to build it locally, copy the file first (e.g. `cp src/api.ts api/src/main.ts`).
98+
- `src/api.ts` itself is validated on every PR by the extension's own lint and TypeScript compile.
99+
- **Versioning:** the published package version in [`api/package.json`](./api/package.json) must always match the extension version in [`package.json`](./package.json). CI enforces this via [`scripts/compare_package_versions.py`](./scripts/compare_package_versions.py). Additionally, any PR that edits `src/api.ts` must bump `api/package.json` (use the `skip api version` label to bypass) and add an entry to [`api/CHANGELOG.md`](./api/CHANGELOG.md) (use the `skip api changelog` label to bypass). When bumping, update both `package.json` files so they stay in sync.
100+
92101
## Questions or Issues?
93102

94103
- **Questions**: Start a [discussion](https://github.com/microsoft/vscode-python/discussions/categories/q-a)

‎api/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copied from ../src/api.ts by the publish pipeline (build/azure-pipeline.npm.yml).
2+
# This is the published package entry point; it is produced at publish time and
3+
# intentionally NOT committed. src/api.ts is the single source of truth.
4+
src/main.ts

‎api/CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
All notable changes to the `@vscode/python-environments` API package are documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [1.37.0]
9+
10+
- Aligned the API package version with the Python Environments extension version.

‎api/package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@vscode/python-environments",
33
"description": "An API facade for the Python Environments extension in VS Code",
4-
"version": "1.0.0",
4+
"version": "1.37.0",
55
"author": {
66
"name": "Microsoft Corporation"
77
},

0 commit comments

Comments
 (0)