wakatime/wakatime-cli

`.wakatime-project` in a parent directory caused incorrect project detection

josephlou5 opened this issue · 2 comments

Hello!

I recently spent a lot of time in my home directory $HOME configuring files such as ~/.bash_profile, ~/.bashrc, ~/.config/** files, etc. I was using VS Code, and Wakatime was (correctly) naming the project josephlou, which is the name of my user account. I decided to add a ~/.wakatime-project file to rename the project to josephlou (home), just so it was clear to future me that this was my home directory. That was working fine, and I happily continued working on other projects.

Actual behavior (what went wrong):

I got a weekly report email and went to check my dashboard, and was extremely surprised to find that I had over 30 hours logged for my josephlou (home) project over the past week, with multiple hours every day. I had only spent 2 days with VS Code open in $HOME, so that was already wrong. I checked the project page for the project that I was actually working on over the past week and saw a flat graph. So it seems that Wakatime went searching for a .wakatime-project file in any parent directory, found the one in my home directory, and is now renaming all my projects to josephlou (home).

Expected behavior (what should have happened):

It seems that Wakatime will first search for a .wakatime-project file in any parent directory, then if one isn't found, default to the name of the folder (please correct me if this is wrong). I think it would make more sense to give priority to the folder currently open, especially since I was in VS Code and there was a clear root folder in the workspace. My project is also using git, which the FAQ says Wakatime also uses to detect projects, but that didn't seem to be the case (or had lower priority than the .wakatime-project file).

I've deleted my ~/.wakatime-project file, so this shouldn't be a problem moving forward. Thankfully there's a way to transfer file times between projects, so I'll still have my stats accurate. Thanks for the great tool!

Environment:

  • OS: Mac
  • Platform: arm64 (2021 M1 Macbook Pro)
  • VS Code: v1.74.2
  • Wakatime extension (for Visual Studio Code): v22.1.0
  • Not sure if this is relevant, but I have the environment variable $WAKATIME_HOME set to $HOME/.config/wakatime instead of the default $HOME. I don't have any other settings changed except for debug=true.

Logs:

A few logs confirmed that files in the project I am working on were sent as part of the project named josephlou (HOME).

So it seems that Wakatime went searching for a .wakatime-project file in any parent directory, found the one in my home directory, and is now renaming all my projects to josephlou (home).

That's the intended behavior for .wakatime-project files. They control the name of the project in the current folder where the .wakatime-project file is located, and any child folders.

My project is also using git, which the FAQ says Wakatime also uses to detect projects, but that didn't seem to be the case (or had lower priority than the .wakatime-project file).

Yes, the .wakatime-project file overwrites any auto-detected project including the one detected from Git.

The priority of projects is:

  1. .wakatime-project file
  2. project map from config file
  3. auto detected project (Git)
  4. alternate project, if available... usually this is your IDE project

What you should use is the project map, which uses a regex to match a file's path and then set a custom project. You would use a regex matching only files in your home folder, and not any child folders. Try adding this to your ~/.wakatime.cfg file:

[projectmap]
^/Users/[^/]+/[^/]+$ = josephlou (home)

The $ means no more forward slashes are allowed, and only matches files in your home folder.

Thanks for the quick reply! That makes sense.