|
1 | 1 | import * as ch from 'child_process'; |
| 2 | +import * as fsapi from 'fs-extra'; |
| 3 | +import * as path from 'path'; |
2 | 4 | import { |
3 | 5 | CancellationError, |
4 | 6 | CancellationToken, |
@@ -192,24 +194,55 @@ export class PoetryPackageManager implements PackageManager, Disposable { |
192 | 194 | } |
193 | 195 |
|
194 | 196 | private async refreshPackages(environment: PythonEnvironment): Promise<Package[]> { |
| 197 | + let cwd = process.cwd(); |
| 198 | + const projects = this.api.getPythonProjects(); |
| 199 | + if (projects.length === 1) { |
| 200 | + const stat = await fsapi.stat(projects[0].uri.fsPath); |
| 201 | + if (stat.isDirectory()) { |
| 202 | + cwd = projects[0].uri.fsPath; |
| 203 | + } else { |
| 204 | + cwd = path.dirname(projects[0].uri.fsPath); |
| 205 | + } |
| 206 | + } else if (projects.length > 1) { |
| 207 | + const dirs = new Set<string>(); |
| 208 | + await Promise.all( |
| 209 | + projects.map(async (project) => { |
| 210 | + const e = await this.api.getEnvironment(project.uri); |
| 211 | + if (e?.envId.id === environment.envId.id) { |
| 212 | + const stat = await fsapi.stat(projects[0].uri.fsPath); |
| 213 | + const dir = stat.isDirectory() ? projects[0].uri.fsPath : path.dirname(projects[0].uri.fsPath); |
| 214 | + if (dirs.has(dir)) { |
| 215 | + dirs.add(dir); |
| 216 | + } |
| 217 | + } |
| 218 | + }), |
| 219 | + ); |
| 220 | + if (dirs.size > 0) { |
| 221 | + // ensure we have the deepest directory node picked |
| 222 | + cwd = Array.from(dirs.values()).sort((a, b) => (a.length - b.length) * -1)[0]; |
| 223 | + } |
| 224 | + } |
| 225 | + |
195 | 226 | const poetryPackages: { name: string; version: string; displayName: string; description: string }[] = []; |
196 | 227 |
|
197 | 228 | try { |
198 | 229 | this.log.info(`Running: ${await getPoetry()} show --no-ansi`); |
199 | | - const result = await runPoetry(['show', '--no-ansi'], undefined, this.log); |
| 230 | + const result = await runPoetry(['show', '--no-ansi'], cwd, this.log); |
200 | 231 |
|
201 | 232 | // Parse poetry show output |
202 | 233 | // Format: name version description |
203 | 234 | const lines = result.split('\n'); |
204 | 235 | for (const line of lines) { |
205 | | - const match = line.match(/^([^\s]+)\s+([^\s]+)\s+(.*)/); |
| 236 | + // Updated regex to properly handle lines with the format: |
| 237 | + // "package (!) version description" |
| 238 | + const match = line.match(/^(\S+)(?:\s+\([!]\))?\s+(\S+)\s+(.*)/); |
206 | 239 | if (match) { |
207 | 240 | const [, name, version, description] = match; |
208 | 241 | poetryPackages.push({ |
209 | 242 | name, |
210 | 243 | version, |
211 | 244 | displayName: name, |
212 | | - description: description?.trim() || version, |
| 245 | + description: `${version} - ${description?.trim() || ''}`, |
213 | 246 | }); |
214 | 247 | } |
215 | 248 | } |
|
0 commit comments