Skip to content

Commit c9ae4c7

Browse files
committed
Update docs
1 parent 960104a commit c9ae4c7

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎api/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- Added the optional `PackageManager.createForProject` factory for package managers whose operations depend on the calling Python project.
12+
- Added the optional `PackageManager.createForProject` factory for package managers whose operations depend on the calling Python project. Explicit project contexts are used directly; environment-only operations use a scoped manager only when exactly one tracked project matches.
1313

1414
## [1.4.0]
1515

‎docs/README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,44 @@ Reports and changes the packages of an environment.
16211621
| `formatInstallSpec(packageName, version)` | `(packageName: string, version: string) => string` | No | Formats a pinned specifier for this tool, for example `requests==2.31.0` for pip or `requests=2.31.0` for conda. Callers default to `name==version` when absent. |
16221622
| `onDidChangePackages` | `Event<DidChangePackagesEventArgs>` | No | Fire when packages change. |
16231623

1624+
##### Project-scoped package managers
1625+
1626+
Implement `createForProject` when package operations depend on project files or
1627+
the process working directory, as they do for tools such as Poetry. Callers that
1628+
already have a `PythonProject` use that project directly. For environment-only
1629+
operations, the extension selects a project-scoped manager only when exactly one
1630+
tracked project uses the environment; it does not choose arbitrarily when no
1631+
project or multiple projects match.
1632+
1633+
Environment-only command and API paths may still use the originally registered
1634+
manager as an unbound fallback, while package views can suppress operations when
1635+
there is no unique project. Project-sensitive methods on the root manager must
1636+
therefore fail clearly or report that data is unavailable rather than running
1637+
from the extension host's working directory. Keep project-specific caches and
1638+
mutable state on the manager returned by `createForProject`.
1639+
1640+
```typescript
1641+
class ProjectPackageManager implements PackageManager {
1642+
readonly name = 'project-pm';
1643+
1644+
constructor(private readonly project?: PythonProject) {}
1645+
1646+
createForProject(project: PythonProject): PackageManager {
1647+
return new ProjectPackageManager(project);
1648+
}
1649+
1650+
async manage(
1651+
environment: PythonEnvironment,
1652+
options: PackageManagementOptions,
1653+
): Promise<void> {
1654+
if (!this.project) {
1655+
throw new Error('Package management requires a Python project.');
1656+
}
1657+
await runPackageCommand(options, { cwd: this.project.uri.fsPath });
1658+
}
1659+
}
1660+
```
1661+
16241662
```typescript
16251663
class MyPackageManager implements PackageManager {
16261664
readonly name = 'my-pm';

‎src/types.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,14 @@ export interface PackageManager {
721721
/**
722722
* Creates a package manager bound to a Python project.
723723
*
724-
* Project-independent package managers can omit this method.
724+
* The extension uses the explicit project supplied by project-based callers. When a caller
725+
* provides only an environment, the extension uses a project-bound manager only if exactly
726+
* one tracked project uses that environment. The registered root manager may still receive
727+
* environment-only operations when no project can be selected safely, so project-sensitive
728+
* operations must handle an unbound manager without running in an arbitrary working directory.
729+
*
730+
* Project-independent package managers can omit this method. Implementations should keep
731+
* project-specific caches and mutable state on the returned manager rather than the root.
725732
*
726733
* @param project - The project to bind to the package manager.
727734
* @returns A package manager that uses the project for project-sensitive operations.

0 commit comments

Comments
 (0)