Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 3 commits into from
Closed

Conversation

LeeHyKu
Copy link

@LeeHyKu LeeHyKu commented Sep 17, 2021

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
Copy link
Member

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

if Dependencies.getHomeDirectory() returns '', wakatime is installed at './.wakatime'
@LeeHyKu
Copy link
Author

LeeHyKu commented Sep 17, 2021

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
Copy link
Member

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
Copy link
Author

LeeHyKu commented Sep 18, 2021

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

Thank you for solving this problem!

@LeeHyKu LeeHyKu closed this Sep 18, 2021
@alanhamlett
Copy link
Member

Fixed with wakatime/wakatime-cli#547.

@alanhamlett
Copy link
Member

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

@LeeHyKu
Copy link
Author

LeeHyKu commented Sep 21, 2021

@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:

public static getHomeDirectory(): string {
let home = process.env.WAKATIME_HOME;
if (home && home.trim() && fs.existsSync(home.trim())) return home.trim();
if (Dependencies.isPortable()) return process.env['VSCODE_PORTABLE'] as string;
return process.env[Dependencies.isWindows() ? 'USERPROFILE' : 'HOME'] || '';
}

Thank you for your kindness!

@alanhamlett
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants