diff --git a/src/docker/ContextManager.ts b/src/docker/ContextManager.ts index e9caf6c013..f7c859dc44 100644 --- a/src/docker/ContextManager.ts +++ b/src/docker/ContextManager.ts @@ -131,7 +131,7 @@ export class DockerContextManager implements ContextManager, Disposable { } private async loadContexts(): Promise { - return callWithTelemetryAndErrorHandling(ext.dockerClient ? 'docker-context.change' : 'docker-context.initialize', async (actionContext: IActionContext) => { + let loadResult = await callWithTelemetryAndErrorHandling(ext.dockerClient ? 'docker-context.change' : 'docker-context.initialize', async (actionContext: IActionContext) => { try { // docker-context.initialize and docker-context.change should be treated as "activation events", in that they aren't real user action actionContext.telemetry.properties.isActivationEvent = 'true'; @@ -200,5 +200,18 @@ export class DockerContextManager implements ContextManager, Disposable { throw err; } }); + + // If the load failed or is otherwise empty, return the default + // That way a returned value is ensured by this method + if (!loadResult) { + loadResult = [{ + ...defaultContext, + Current: true, + DockerEndpoint: os.platform() === 'win32' ? WindowsLocalPipe : UnixLocalPipe, + Type: 'moby', + } as DockerContext]; + } + + return loadResult; } } diff --git a/src/docker/DockerodeApiClient/DockerodeApiClient.ts b/src/docker/DockerodeApiClient/DockerodeApiClient.ts index b23e8b9958..a87458753b 100644 --- a/src/docker/DockerodeApiClient/DockerodeApiClient.ts +++ b/src/docker/DockerodeApiClient/DockerodeApiClient.ts @@ -65,7 +65,7 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneContainers(), token); return { ...result, - ObjectsDeleted: result.ContainersDeleted.length, + ObjectsDeleted: result.ContainersDeleted?.length ?? 0, }; } @@ -131,13 +131,13 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneImages(), token); return { ...result, - ObjectsDeleted: result.ImagesDeleted.length, + ObjectsDeleted: result.ImagesDeleted?.length ?? 0, }; } public async tagImage(context: IActionContext, ref: string, fullTag: string, token?: CancellationToken): Promise { const repo = fullTag.substr(0, fullTag.lastIndexOf(':')); - const tag = fullTag.substr(fullTag.lastIndexOf(':')); + const tag = fullTag.substr(fullTag.lastIndexOf(':') + 1); const image = this.dockerodeClient.getImage(ref); await this.callWithErrorHandling(context, async () => image.tag({ repo: repo, tag: tag }), token); } @@ -174,7 +174,7 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneNetworks(), token); return { SpaceReclaimed: 0, - ObjectsDeleted: result.NetworksDeleted.length, + ObjectsDeleted: result.NetworksDeleted?.length ?? 0, }; } @@ -216,7 +216,7 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneVolumes(), token); return { ...result, - ObjectsDeleted: result.VolumesDeleted.length, + ObjectsDeleted: result.VolumesDeleted?.length ?? 0, }; }