最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

VSCode task arguments not passing to custume extension command - Stack Overflow

matteradmin7PV0评论

I'm trying to create a simple VS Code extension that receives arguments from a task, but the arguments aren't being passed through. Here's my setup: Extension Code (extension.ts):

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    let disposable = vscodemands.registerCommand('testExtension.sendMessage', (...args) => {
        console.log('Received arguments:', args);
        
        const terminal = vscode.window.createTerminal('Test Extension');
        terminal.show();

        if (args && args.length > 0) {
            const message = args.join(' ');
            terminal.sendText(`echo "Received message: ${message}"`);
        } else {
            terminal.sendText('echo "No message received"');
        }
    });

    context.subscriptions.push(disposable);
}

package.json related sections:

  "activationEvents": [
    "onCommand:testExtension.sendMessage"
  ],
  "main": "./dist/extension.js",
  "contributes": {
    "commands": [
      {
        "command": "testExtension.sendMessage",
        "title": "Test Extension: Send Message"
      }
    ]
  },

this is the tasks.json in nother vscode instance:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Send Message",
            "type": "shell",
            "command": "${command:testExtension.sendMessage}",
            "args": [
                "hello world"
            ],
            "runOptions": {
                "reevaluateOnRerun": true
            }
        }
    ]
}

When I run the task, it always outputs "No message received". The arguments specified in tasks.json ("hello world") are not being passed to the extension command. I've Tried:

"args": ["hello", "world"]
"args": {
 "message": "hello world"
}

None of these attempts worked - the arguments from tasks.json never reach the extension command.

How can I properly pass arguments from a VS Code task to an extension command? Is there a specific format or method I need to use to make this work?

Environment:

VS Code Version: 1.94.0

OS: Windows 10

Any help would be appreciated!

Post a comment

comment list (0)

  1. No comments so far