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.23k stars 137 forks source link

Prevent errors when both env WAKATIME_HOME and HOME are not set #238

Closed LeeHyKu closed 2 years ago

LeeHyKu commented 2 years ago

In a special case where environment variables cannot be set at will, wakatime may not be executed because even env HOME is not set. Even if the file location is set in the argument, it cannot be executed because wakatime-cli refers to the env HOME.

This Pull Request adds a code for setting an environment variable when it is not set. After this commit, It will be able to run smoothly even in special environment like online vscode.

alanhamlett commented 2 years ago

What's the error message you see without this fix? Is the error coming from wakatime-cli or from vscode?

LeeHyKu commented 2 years ago

yes, error is coming from wakatime-cli:

{
  "caller":"/Users/runner/work/wakatime-cli/wakatime-cli/cmd/legacy/run.go:194",
  "func":"runCmd",
  "level":"error",
  "message":"failed to run command: sending heartbeat(s) failed: failed to load offline queue filepath: failed getting user's home directory: $HOME is not defined",
  "now":"2021-09-17T03:48:04Z",
  "version":"v1.26.0"
}

but this fix will also be needed because wakatime is installed in cwd

alanhamlett commented 2 years ago

That error comes from:

https://github.com/golang/go/blob/6602c86a38ff0d8889257e908489162de38ebbd8/src/os/user/lookup_stubs.go#L64

I think we should fallback to an alternate way of detecting $HOME in wakatime-cli instead of failing, similar to how the legacy Python wakatime-cli did it:

https://github.com/wakatime/wakatime/blob/9b64548b16ab5ef16603d9a6c2620a16d0df8d46/wakatime/configs.py#L41

When testing, the legacy Python wakatime-cli works without $HOME set but new Go wakatime-cli fails.

LeeHyKu commented 2 years ago

ok, Come to think of it, This is not a problem to be solved here

Thank you for solving this problem!

alanhamlett commented 2 years ago

Fixed with https://github.com/wakatime/wakatime-cli/pull/547.

alanhamlett commented 2 years ago

Does it work now with v1.26.1 of wakatime-cli?

LeeHyKu commented 2 years ago

@alanhamlett A little work(move .wakatime.cfg(from pwd) to user directory) is required, but it works well without modifying wakatime or wakatime plugin!

in wakatime vsc plugin, if env HOME and WAKATIME_HOME are not defined, config file and wakatime are installed at vsc pwd. but the new version refers to the user's home directory if env not defined.

I think it will be solved by changing this to refer to PWD, or change the constant return value of following function to user home directory: https://github.com/wakatime/vscode-wakatime/blob/9225d779e7788ea6a49293b59833cf81e47734a5/src/dependencies.ts#L65-L70

Thank you for your kindness!

alanhamlett commented 2 years ago

We're now defaulting to CWD in vscode when $HOME not set with v17.0.7.