Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions packages/task/src/browser/process/process-task-resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// *****************************************************************************
// Copyright (C) 2026 EclipseSource and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// http://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';
const disableJSDOM = enableJSDOM();

import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
FrontendApplicationConfigProvider.set({});

import { expect } from 'chai';
import { isCancelled } from '@theia/core/lib/common/cancellation';
import { TaskScope } from '../../common/task-protocol';
import { ProcessTaskConfiguration } from '../../common/process/task-protocol';
import { ProcessTaskResolver } from './process-task-resolver';

describe('ProcessTaskResolver', () => {
after(() => {
disableJSDOM();
});

it('cancels a task when resolving its arguments is cancelled', async () => {
const resolver = new ProcessTaskResolver();
(resolver as unknown as {
variableResolverService: { resolve: (value: unknown) => Promise<unknown> };
workspaceService: { getWorkspaceRootUri: () => undefined };
}).variableResolverService = {
resolve: async value => Array.isArray(value) ? undefined : value
};
(resolver as unknown as {
workspaceService: { getWorkspaceRootUri: () => undefined };
}).workspaceService = {
getWorkspaceRootUri: () => undefined
};

const task: ProcessTaskConfiguration = {
label: 'task with input variables',
type: 'shell',
command: 'node',
args: ['${input:task-argument}'],
_scope: TaskScope.Workspace
};

let error: Error | undefined;
try {
await resolver.resolveTask(task);
} catch (e) {
error = e as Error;
}

expect(isCancelled(error)).to.equal(true);
});
});
49 changes: 31 additions & 18 deletions packages/task/src/browser/process/process-task-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import { injectable, inject } from '@theia/core/shared/inversify';
import { VariableResolverService } from '@theia/variable-resolver/lib/browser';
import { TaskResolver } from '../task-contribution';
import { TaskConfiguration } from '../../common/task-protocol';
import { ProcessTaskConfiguration } from '../../common/process/task-protocol';
import { CommandProperties, ProcessTaskConfiguration } from '../../common/process/task-protocol';
import { TaskDefinitionRegistry } from '../task-definition-registry';
import URI from '@theia/core/lib/common/uri';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { cancelled } from '@theia/core/lib/common/cancellation';

@injectable()
export class ProcessTaskResolver implements TaskResolver {
Expand Down Expand Up @@ -59,25 +60,13 @@ export class ProcessTaskResolver implements TaskResolver {
}
}

const commandProperties = await this.resolveCommandProperties(processTaskConfig, variableResolverOptions);
const result: ProcessTaskConfiguration = {
...processTaskConfig,
command: await this.variableResolverService.resolve(processTaskConfig.command, variableResolverOptions),
args: processTaskConfig.args ? await this.variableResolverService.resolve(processTaskConfig.args, variableResolverOptions) : undefined,
windows: processTaskConfig.windows ? {
command: await this.variableResolverService.resolve(processTaskConfig.windows.command, variableResolverOptions),
args: processTaskConfig.windows.args ? await this.variableResolverService.resolve(processTaskConfig.windows.args, variableResolverOptions) : undefined,
options: processTaskConfig.windows.options
} : undefined,
osx: processTaskConfig.osx ? {
command: await this.variableResolverService.resolve(processTaskConfig.osx.command, variableResolverOptions),
args: processTaskConfig.osx.args ? await this.variableResolverService.resolve(processTaskConfig.osx.args, variableResolverOptions) : undefined,
options: processTaskConfig.osx.options
} : undefined,
linux: processTaskConfig.linux ? {
command: await this.variableResolverService.resolve(processTaskConfig.linux.command, variableResolverOptions),
args: processTaskConfig.linux.args ? await this.variableResolverService.resolve(processTaskConfig.linux.args, variableResolverOptions) : undefined,
options: processTaskConfig.linux.options
} : undefined,
...commandProperties,
windows: await this.resolveCommandProperties(processTaskConfig.windows, variableResolverOptions),
osx: await this.resolveCommandProperties(processTaskConfig.osx, variableResolverOptions),
linux: await this.resolveCommandProperties(processTaskConfig.linux, variableResolverOptions),
options: {
cwd: await this.variableResolverService.resolve(cwd, variableResolverOptions),
env: processTaskConfig.options?.env && await this.variableResolverService.resolve(processTaskConfig.options.env, variableResolverOptions),
Expand All @@ -86,4 +75,28 @@ export class ProcessTaskResolver implements TaskResolver {
};
return result;
}

protected async resolveCommandProperties(
properties: CommandProperties | undefined,
variableResolverOptions: { context: URI | undefined; configurationSection: string }
): Promise<CommandProperties | undefined> {
if (!properties) {
return undefined;
}
const command = properties.command === undefined
? undefined
: await this.variableResolverService.resolve(properties.command, variableResolverOptions);
const args = properties.args === undefined
? undefined
: await this.variableResolverService.resolve(properties.args, variableResolverOptions);
if ((properties.command !== undefined && command === undefined)
|| (properties.args !== undefined && args === undefined)) {
throw cancelled();
}
return {
command,
args,
options: properties.options
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// *****************************************************************************
// Copyright (C) 2026 EclipseSource and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';

let disableJSDOM = enableJSDOM();

import { expect } from 'chai';
import { InputBox, QuickInputHideReason, QuickInputService } from '@theia/core/lib/browser';
import { isCancelled } from '@theia/core/lib/common/cancellation';
import { Emitter } from '@theia/core/lib/common/event';
import URI from '@theia/core/lib/common/uri';
import { CommonVariableContribution } from './common-variable-contribution';
import { VariableRegistry } from './variable';

disableJSDOM();

describe('CommonVariableContribution', () => {

before(() => {
disableJSDOM = enableJSDOM();
});

after(() => {
disableJSDOM();
});

function createInputBox(): { inputBox: InputBox; accept: Emitter<void>; hide: Emitter<{ reason: QuickInputHideReason }> } {
const accept = new Emitter<void>();
const hide = new Emitter<{ reason: QuickInputHideReason }>();
const inputBox = {
onDidAccept: accept.event,
onDidHide: hide.event,
dispose: () => undefined,
hide: () => undefined,
show: () => undefined
} as unknown as InputBox;
return { inputBox, accept, hide };
}

it('keeps task prompt string inputs open when focus is lost', async () => {
const contribution = new CommonVariableContribution();
const { inputBox, accept } = createInputBox();

(contribution as unknown as { env: { getExecPath: () => Promise<string> } }).env = {
getExecPath: async () => ''
};
(contribution as unknown as { preferences: { get: () => unknown } }).preferences = {
get: () => ({
inputs: [{
id: 'task-argument',
type: 'promptString',
description: 'Enter a task argument',
default: 'default-value'
}]
})
};
(contribution as unknown as { quickInputService: Pick<QuickInputService, 'createInputBox'> }).quickInputService = {
createInputBox: () => inputBox
};

const variables = new VariableRegistry();
await contribution.registerVariables(variables);

const input = variables.getVariable('input');
const resolving = input?.resolve(new URI('file:///workspace'), 'task-argument', 'tasks');
expect(inputBox.prompt).to.equal('Enter a task argument');
expect(inputBox.value).to.equal('default-value');
expect(inputBox.ignoreFocusOut).to.equal(true);
inputBox.value = 'task-argument';
accept.fire();
const resolved = await resolving;

expect(resolved).to.equal('task-argument');
});

it('cancels task prompt string inputs when dismissed', async () => {
const contribution = new CommonVariableContribution();
const { inputBox, hide } = createInputBox();

(contribution as unknown as { env: { getExecPath: () => Promise<string> } }).env = {
getExecPath: async () => ''
};
(contribution as unknown as { preferences: { get: () => unknown } }).preferences = {
get: () => ({
inputs: [{
id: 'task-argument',
type: 'promptString',
description: 'Enter a task argument'
}]
})
};
(contribution as unknown as { quickInputService: Pick<QuickInputService, 'createInputBox'> }).quickInputService = {
createInputBox: () => inputBox
};

const variables = new VariableRegistry();
await contribution.registerVariables(variables);

let error: Error | undefined;
try {
const resolving = variables.getVariable('input')?.resolve(new URI('file:///workspace'), 'task-argument', 'tasks');
hide.fire({ reason: QuickInputHideReason.Gesture });
await resolving;
} catch (e) {
error = e as Error;
}
expect(isCancelled(error)).to.equal(true);
});

it('cancels task pick string inputs when dismissed', async () => {
const contribution = new CommonVariableContribution();
let pickOptions: { placeholder?: string; ignoreFocusOut?: boolean } | undefined;

(contribution as unknown as { env: { getExecPath: () => Promise<string> } }).env = {
getExecPath: async () => ''
};
(contribution as unknown as { preferences: { get: () => unknown } }).preferences = {
get: () => ({
inputs: [{
id: 'task-argument-choice',
type: 'pickString',
description: 'Choose a task argument',
options: ['first', 'second']
}]
})
};
(contribution as unknown as { quickInputService: Pick<QuickInputService, 'showQuickPick'> }).quickInputService = {
showQuickPick: async (_items, options) => {
pickOptions = options;
return undefined;
}
};

const variables = new VariableRegistry();
await contribution.registerVariables(variables);

let error: Error | undefined;
try {
await variables.getVariable('input')?.resolve(new URI('file:///workspace'), 'task-argument-choice', 'tasks');
} catch (e) {
error = e as Error;
}
expect(isCancelled(error)).to.equal(true);
expect(pickOptions).to.deep.equal({
placeholder: 'Choose a task argument',
ignoreFocusOut: true
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { VariableInput } from './variable-input';
import { QuickInputService, QuickPickValue } from '@theia/core/lib/browser';
import { MaybeArray, RecursivePartial } from '@theia/core/lib/common/types';
import { cancelled } from '@theia/core/lib/common/cancellation';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
import URI from '@theia/core/lib/common/uri';

@injectable()
Expand Down Expand Up @@ -108,10 +109,7 @@ export class CommonVariableContribution implements VariableContribution {
if (typeof input.description !== 'string') {
return undefined;
}
return this.quickInputService?.input({
prompt: input.description,
value: input.default
});
return this.resolvePromptStringInput(input.description, input.default);
}
if (input.type === 'pickString') {
if (typeof input.description !== 'string' || !Array.isArray(input.options)) {
Expand All @@ -135,8 +133,14 @@ export class CommonVariableContribution implements VariableContribution {
});
}
}
const selectedPick = await this.quickInputService?.showQuickPick(elements, { placeholder: input.description });
return selectedPick?.value;
const selectedPick = await this.quickInputService?.showQuickPick(elements, {
placeholder: input.description,
ignoreFocusOut: true
});
if (!selectedPick) {
throw cancelled();
}
return selectedPick.value;
}
if (input.type === 'command') {
if (typeof input.command !== 'string') {
Expand All @@ -148,4 +152,34 @@ export class CommonVariableContribution implements VariableContribution {
}
});
}

protected resolvePromptStringInput(description: string, defaultValue: string | undefined): Promise<string> {
const inputBox = this.quickInputService?.createInputBox();
if (!inputBox) {
throw cancelled();
}
return new Promise((resolve, reject) => {
const toDispose = new DisposableCollection();
toDispose.push(inputBox.onDidAccept(() => {
const value = inputBox.value;
toDispose.dispose();
inputBox.hide();
inputBox.dispose();
if (value === undefined) {
reject(cancelled());
} else {
resolve(value);
}
}));
toDispose.push(inputBox.onDidHide(() => {
toDispose.dispose();
inputBox.dispose();
reject(cancelled());
}));
inputBox.prompt = description;
inputBox.value = defaultValue;
inputBox.ignoreFocusOut = true;
inputBox.show();
});
}
}
Loading