wakatime / vscode-wakatime

Visual Studio Code plugin for automatic time tracking and metrics generated from your programming activity.
https://wakatime.com/vs-code
BSD 3-Clause "New" or "Revised" License
1.2k stars 133 forks source link

`api_key_vault_cmd` configuration option for shell command to get api key from vault fails with ENOENT #387

Closed vorburger closed 3 months ago

vorburger commented 7 months ago

The api_key_vault_cmd configuration option which according to https://github.com/wakatime/wakatime-cli/blob/develop/USAGE.md can be used to specify a "shell command to get api key from vault" fails for me, on Linux, for example for a simple test like this in .wakatime.cfg:

api_key_vault_cmd = /usr/bin/echo hi

I'm getting this in the VSC Developer Tools:

ERR spawn /usr/bin/echo hi ENOENT: Error: spawn /usr/bin/echo hi ENOENT
    at ChildProcess._handle.onexit (node:internal/child_process:283:19)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

I'm intentionally opening this as a separate bug from wakatime/vscode-wakatime#295 which seems to be Windows with specific restricted policy.

This is with WakaTime.vscode-wakatime@24.4.0 (from code --list-extensions --show-versions).

kisaragi-hiu commented 3 months ago

First, this package vscode-wakatime is treating the option's entire string as one program to run:

https://github.com/wakatime/vscode-wakatime/blob/e0a7f36c785c35007c98897ba4b018bcb30212c3/src/options.ts#L240-L246

child_process.spawn treats the first argument as the program's executable name, and only treats it a shell command (ie. interpreted by sh with sh syntax) if the shell option was true, which it isn't.

Second, that line in the documentation is quite misleading, at least in regards to wakatime-cli's behavior. In reality the string is not a shell command, it is a space-separated list of an executable and its arguments. This extension should probably mimick this behavior.

Edit: I thought this was wakatime/vscode-wakatime. Corrected.

Edit: Oh, it was moved.

gandarez commented 3 months ago

Hi @vorburger that's not the way it should be used api_key_vault_cmd = /usr/bin/echo hi. So, instead you might replace with

api_key_vault_cmd = pass any_defined_key
kisaragi-hiu commented 3 months ago

@gandarez with vscode-wakatime, that will not work - not even the pass command would work. vscode-wakatime treats the whole value as one executable name (including the spaces).

This is a problem in vscode-wakatime, specifically here:

  public async getApiKeyFromVaultCmd(): Promise<string> {
    try {
      const apiKeyCmd = await this.getSettingAsync<string>('settings', 'api_key_vault_cmd');
      if (!apiKeyCmd) return '';

      const options = Desktop.buildOptions();
      const proc = child_process.spawn(apiKeyCmd, options);

where it treats the whole string as one executable. I've filed https://github.com/wakatime/vscode-wakatime/pull/386 to fix it.

The /usr/bin/echo hi is just fact a placeholder to refer to some command returning a proper API key.

gandarez commented 3 months ago

I see. Let's just wait for @alanhamlett input.