@@ -145,21 +145,21 @@ func WithDetection(config Config) heartbeat.HandleOption {
145145 for n , h := range hh {
146146 logger .Debugf ("execute project detection for: %s" , h .Entity )
147147
148- // first, use .wakatime-project or [projectmap] section with entity path.
148+ // First use .wakatime-project or [projectmap] section with entity path.
149149 // Then, detect with project folder. This tries to use the same project name
150150 // across all IDEs instead of sometimes using alternate project when file is unsaved
151151 result , detector := Detect (ctx , config .MapPatterns ,
152152 DetecterArg {Filepath : h .Entity , ShouldRun : h .EntityType == heartbeat .FileType },
153153 DetecterArg {Filepath : h .ProjectPathOverride , ShouldRun : true },
154154 )
155155
156- // second, use project override
156+ // Project override
157157 if result .Project == "" && h .ProjectOverride != "" {
158158 result .Project = h .ProjectOverride
159159 result .Folder = h .ProjectPathOverride
160160 }
161161
162- // third, autodetect with revision control with entity path.
162+ // Autodetect with revision control from entity path.
163163 // Then, autodetect with project folder. This tries to use the same project name
164164 // across all IDEs instead of sometimes using alternate project when file is unsaved
165165 if result .Project == "" || result .Branch == "" || result .Folder == "" {
@@ -177,30 +177,40 @@ func WithDetection(config Config) heartbeat.HandleOption {
177177 result .Folder = firstNonEmptyString (result .Folder , revControlResult .Folder )
178178 }
179179
180- // fourth, use alternate project
180+ folder := h .ProjectPathOverride
181+ if runtime .GOOS == "windows" {
182+ folder = windows .FormatFilePath (folder )
183+ }
184+
185+ // Use project folder last part as project name
186+ if result .Project == "" && h .ProjectPathOverride != "" {
187+ proj := filepath .Base (folder )
188+
189+ if proj != "." && proj != "/" {
190+ result .Project = proj
191+ }
192+ result .Folder = folder
193+ }
194+
195+ // Alternate project if none auto-detected
181196 if result .Project == "" && h .ProjectAlternate != "" {
182197 result .Project = h .ProjectAlternate
183- result .Folder = firstNonEmptyString (h .ProjectPathOverride , result .Folder )
184198 }
185199
186- // fifth, use alternate branch
200+ // Alternate branch if none detected
187201 if result .Branch == "" && h .BranchAlternate != "" {
188202 result .Branch = h .BranchAlternate
189203 }
190204
191- // sixth, use project folder found or entity's path
192- result .Folder = firstNonEmptyString (result .Folder , h . ProjectPathOverride )
205+ // Make sure project folder is defined when not found from entity's path
206+ result .Folder = firstNonEmptyString (result .Folder , folder )
193207
194- // seventh, if no folder is found, use entity's directory
208+ // If no folder found, use entity's directory
195209 if h .EntityType == heartbeat .FileType && result .Folder == "" {
196210 result .Folder = filepath .Dir (h .Entity )
197211 }
198212
199- if runtime .GOOS == "windows" && result .Folder != "" {
200- result .Folder = windows .FormatFilePath (result .Folder )
201- }
202-
203- // finally, obfuscate project name if necessary
213+ // Obfuscate project name if necessary
204214 if heartbeat .ShouldSanitize (ctx , heartbeat.SanitizeCheck {
205215 Entity : h .Entity ,
206216 ProjectPath : result .Folder ,
@@ -212,7 +222,7 @@ func WithDetection(config Config) heartbeat.HandleOption {
212222
213223 result .Folder = FormatProjectFolder (ctx , result .Folder )
214224
215- // count total subfolders in project's path
225+ // Count total subfolders in project's path
216226 if result .Folder != "" && strings .HasPrefix (h .Entity , result .Folder ) {
217227 subfolders := CountSlashesInProjectFolder (result .Folder )
218228 if subfolders > 0 {
@@ -705,13 +715,14 @@ func FormatProjectFolder(ctx context.Context, fp string) string {
705715 formatted , err := filepath .Abs (fp )
706716 if err != nil {
707717 logger .Debugf ("failed to resolve absolute path for %q: %s" , fp , err )
708- return formatted
718+ return fp
709719 }
710720
711721 // evaluate any symlinks
712722 formatted , err = realpath .Realpath (formatted )
713723 if err != nil {
714724 logger .Debugf ("failed to resolve real path for %q: %s" , formatted , err )
725+ return fp
715726 }
716727
717728 return formatted
0 commit comments