Skip to content

Commit 507bee2

Browse files
committed
docs: address PR review feedback on API manual
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2f79caeb-d789-4eb6-898b-4bddd9d3292a
1 parent 6a5e0ea commit 507bee2

3 files changed

Lines changed: 63 additions & 7 deletions

File tree

‎api/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ export async function activate() {
3535
}
3636
```
3737

38+
## Full API reference
39+
40+
📘 **[Python Environments API reference](https://github.com/microsoft/vscode-python-environments/blob/main/docs/README.md)**
41+
42+
The complete manual documents every method and data type, organized by domain -
43+
environments, packages, projects, execution, environment variables, and
44+
extensibility - with field tables, parameter tables, return types, and examples.
45+
46+
- [Environments](https://github.com/microsoft/vscode-python-environments/blob/main/docs/README.md#environments) - discover, resolve, select, create, and remove interpreters
47+
- [Packages](https://github.com/microsoft/vscode-python-environments/blob/main/docs/README.md#packages) - list, install, uninstall, and look up versions
48+
- [Projects](https://github.com/microsoft/vscode-python-environments/blob/main/docs/README.md#projects) - the folders the extension tracks
49+
- [Execution](https://github.com/microsoft/vscode-python-environments/blob/main/docs/README.md#execution) - run Python in terminals, tasks, and background processes
50+
- [Environment variables](https://github.com/microsoft/vscode-python-environments/blob/main/docs/README.md#environment-variables) - resolved variables for a scope
51+
- [Extensibility](https://github.com/microsoft/vscode-python-environments/blob/main/docs/README.md#extensibility) - register your own environment manager, package manager, or project creator
52+
53+
See [`CHANGELOG.md`](https://github.com/microsoft/vscode-python-environments/blob/main/api/CHANGELOG.md) for API changes between versions.
54+

‎docs/README.md‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,17 @@ refreshEnvironments(scope: RefreshEnvironmentsScope): Promise<void>;
379379
| --- | --- | --- | --- |
380380
| `scope` | [`RefreshEnvironmentsScope`](#scope-types) | Yes (may be `undefined`) | `Uri` refreshes discovery for that project or folder; `undefined` refreshes global and workspace discovery. |
381381

382-
**Returns** `Promise<void>`, resolving when discovery completes. Results arrive
383-
through [`onDidChangeEnvironments`](#ondidchangeenvironments); subscribe before
384-
refreshing if you need the deltas.
382+
**Returns** `Promise<void>`, resolving when the managers finish discovery.
383+
384+
Read the results with [`getEnvironments`](#getenvironments) once the promise
385+
settles. Do not rely on [`onDidChangeEnvironments`](#ondidchangeenvironments)
386+
to deliver them: that event is optional on `EnvironmentManager`, and
387+
`refreshEnvironments` does not synthesize one, so whether a refresh produces
388+
deltas is up to the provider.
385389

386390
```typescript
387391
await api.refreshEnvironments(undefined);
392+
// Authoritative: read the list rather than waiting for an event.
388393
const refreshed = await api.getEnvironments('all');
389394
```
390395

@@ -439,6 +444,29 @@ const active = await api.getEnvironment(
439444
);
440445
```
441446

447+
> [!IMPORTANT]
448+
> **This call can return a stale value.** It is deliberately non-blocking: it
449+
> races the real resolution against a one-second timeout so that slow initial
450+
> discovery cannot stall callers. If resolution has not finished in time, it
451+
> returns the *last-known* environment for the scope - which may be `undefined`
452+
> on a first call - while resolution continues in the background.
453+
>
454+
> The resolved value is published through
455+
> [`onDidChangeEnvironment`](#ondidchangeenvironment) once it settles. If your
456+
> feature needs the authoritative selection, subscribe to that event and treat
457+
> the value from `getEnvironment` as a fast first guess:
458+
>
459+
> ```typescript
460+
> let current = await api.getEnvironment(projectUri); // May be last-known.
461+
> context.subscriptions.push(
462+
> api.onDidChangeEnvironment((e) => {
463+
> if (e.uri?.toString() === projectUri.toString()) {
464+
> current = e.new; // Authoritative once resolution settles.
465+
> }
466+
> }),
467+
> );
468+
> ```
469+
442470
#### `setEnvironment`
443471
444472
Selects - or clears - the environment for one or more scopes, and persists the
@@ -1236,15 +1264,15 @@ const module: PythonTerminalExecutionOptions = {
12361264
| `name` | `string` | Yes | Name of the task, shown in the task UI. |
12371265
| `args` | `string[]` | Yes | Arguments passed to the Python executable. |
12381266
| `project` | [`PythonProject`](#pythonproject) | No | Project the task belongs to. |
1239-
| `cwd` | `string` | No | Working directory. Defaults to the project directory of the script being run. |
1267+
| `cwd` | `string` | No | Working directory for the task's shell execution. When omitted, VS Code resolves it from the task scope - the workspace folder containing `project`, or the global scope when `project` is not supplied. |
12401268
| `env` | `{ [key: string]: string }` | No | Additional environment variables for the task. |
12411269

12421270
#### `PythonBackgroundRunOptions`
12431271

12441272
| Field | Type | Required | Description |
12451273
| --- | --- | --- | --- |
12461274
| `args` | `string[]` | Yes | Arguments passed to the Python executable. |
1247-
| `cwd` | `string` | No | Working directory. Defaults to the project directory of the script being run. |
1275+
| `cwd` | `string` | No | Working directory, passed straight to the spawned process. When omitted, the process inherits the extension host's working directory, which is **not** your project folder - always supply `cwd` (for example `project.uri.fsPath`) if the script resolves relative paths. |
12481276
| `env` | `{ [key: string]: string \| undefined }` | No | Additional environment variables. An `undefined` value unsets a variable. |
12491277

12501278
#### `PythonProcess`
@@ -1391,11 +1419,20 @@ runInBackground(
13911419
| Parameter | Type | Required | Description |
13921420
| --- | --- | --- | --- |
13931421
| `environment` | [`PythonEnvironment`](#pythonenvironment) | Yes | Environment used to start the process. |
1394-
| `options` | [`PythonBackgroundRunOptions`](#pythonbackgroundrunoptions) | Yes | `args` for Python, plus optional `cwd` and `env`. |
1422+
| `options` | [`PythonBackgroundRunOptions`](#pythonbackgroundrunoptions) | Yes | `args` for Python, plus optional `cwd` and `env`. Supply `cwd` - it is not inferred. |
13951423

13961424
**Returns** `Promise<PythonProcess>` with `stdin`, `stdout`, `stderr`, `kill()`,
13971425
and `onExit()`.
13981426

1427+
> [!IMPORTANT]
1428+
> `cwd` is forwarded to the spawned process unchanged. There is no project
1429+
> context to infer it from, so when you omit it the process inherits the
1430+
> extension host's working directory rather than your project folder. Pass
1431+
> `cwd` explicitly whenever the script resolves relative paths.
1432+
>
1433+
> You own the process lifetime: call `kill()` when your feature is done, and
1434+
> tie it to your disposables so it does not outlive deactivation.
1435+
13991436
```typescript
14001437
const proc = await api.runInBackground(env, {
14011438
args: ['-c', 'import sys; print(sys.version)'],

‎src/types.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,9 @@ export interface PythonBackgroundRunOptions {
14231423
args: string[];
14241424

14251425
/**
1426-
* Current working directory for the script or module. Default is the project directory for the script being run.
1426+
* Current working directory for the script or module. This is passed directly to the spawned
1427+
* process; when it is omitted the process inherits the extension host's working directory,
1428+
* which is not the project directory. Supply this when the script resolves relative paths.
14271429
*/
14281430
cwd?: string;
14291431

0 commit comments

Comments
 (0)