From b9135b8c229a2ffb9a7f1967076491096b4a4148 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 9 Feb 2022 01:47:43 -0800 Subject: [PATCH 1/3] fix: support windows paths --- clang/compilerDb.go | 10 +++++++++- utils/fileSystem.go | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/clang/compilerDb.go b/clang/compilerDb.go index e5d5801..94e8862 100644 --- a/clang/compilerDb.go +++ b/clang/compilerDb.go @@ -3,10 +3,11 @@ package clang import ( "encoding/json" "errors" - "github.com/ejfitzgerald/clang-tidy-cache/utils" "io/ioutil" "os" "path/filepath" + + "github.com/ejfitzgerald/clang-tidy-cache/utils" ) type DatabaseEntry struct { @@ -32,6 +33,9 @@ func ExtractCompilationTarget(databaseRootPath string, target string) (*Database defer jsonFile.Close() bytes, err := ioutil.ReadAll(jsonFile) + if err != nil { + return nil, err + } var db Database err = json.Unmarshal(bytes, &db) @@ -40,6 +44,10 @@ func ExtractCompilationTarget(databaseRootPath string, target string) (*Database } for _, entry := range db { + entry.File = utils.PosixifyPath(entry.File) + entry.Directory = utils.PosixifyPath(entry.Directory) + target = utils.PosixifyPath(target) + if entry.File == target || entry.File == filepath.Join(entry.Directory, target) { return &entry, nil } diff --git a/utils/fileSystem.go b/utils/fileSystem.go index ea5f3ac..264b32e 100644 --- a/utils/fileSystem.go +++ b/utils/fileSystem.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" ) // FindInParents searches for a file named filename in searchDir or any of it's @@ -22,3 +23,13 @@ func FindInParents(searchDir string, filename string) (string, error) { } return FindInParents(parentDir, filename) } + +// Converts given path to Posix (replacing \ with /) +// +// @param {string} givenPath Path to convert +// +// @returns {string} Converted filepath +// +func PosixifyPath(givenPath string) string { + return strings.ReplaceAll(givenPath, "\\", "/") +} From ebb01308284636307cbeb66a52f7359a169723a4 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 9 Feb 2022 17:24:00 -0800 Subject: [PATCH 2/3] fix: run poxisify only on Windows --- clang/compilerDb.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clang/compilerDb.go b/clang/compilerDb.go index 94e8862..95e4a1f 100644 --- a/clang/compilerDb.go +++ b/clang/compilerDb.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "github.com/ejfitzgerald/clang-tidy-cache/utils" ) @@ -44,9 +45,11 @@ func ExtractCompilationTarget(databaseRootPath string, target string) (*Database } for _, entry := range db { - entry.File = utils.PosixifyPath(entry.File) - entry.Directory = utils.PosixifyPath(entry.Directory) - target = utils.PosixifyPath(target) + if runtime.GOOS == "windows" { + entry.File = utils.PosixifyPath(entry.File) + entry.Directory = utils.PosixifyPath(entry.Directory) + target = utils.PosixifyPath(target) + } if entry.File == target || entry.File == filepath.Join(entry.Directory, target) { return &entry, nil From 7f69159688006f06149fdbd18ce0c0087729b639 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 5 Jan 2024 11:01:39 -0800 Subject: [PATCH 3/3] fix: normalize the paths when finding entries --- clang/compilerDb.go | 14 ++++++-------- main.go | 5 +++-- utils/fileSystem.go | 21 ++++++++++++++++++++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/clang/compilerDb.go b/clang/compilerDb.go index 95e4a1f..7a48412 100644 --- a/clang/compilerDb.go +++ b/clang/compilerDb.go @@ -2,11 +2,10 @@ package clang import ( "encoding/json" - "errors" + "fmt" "io/ioutil" "os" "path/filepath" - "runtime" "github.com/ejfitzgerald/clang-tidy-cache/utils" ) @@ -44,17 +43,16 @@ func ExtractCompilationTarget(databaseRootPath string, target string) (*Database return nil, err } + // Find the entry that matches the target for _, entry := range db { - if runtime.GOOS == "windows" { - entry.File = utils.PosixifyPath(entry.File) - entry.Directory = utils.PosixifyPath(entry.Directory) - target = utils.PosixifyPath(target) - } + entry.File = utils.NormalizePath(entry.File) + entry.Directory = utils.NormalizePath(entry.Directory) + target = utils.NormalizePath(target) if entry.File == target || entry.File == filepath.Join(entry.Directory, target) { return &entry, nil } } - return nil, errors.New("Unable to locate the compiler definition") + return nil, fmt.Errorf("unable to find the compiler definition for target %v", target) } diff --git a/main.go b/main.go index 5e9527f..e380fdf 100644 --- a/main.go +++ b/main.go @@ -3,14 +3,15 @@ package main import ( "encoding/json" "fmt" - "github.com/ejfitzgerald/clang-tidy-cache/caches" - "github.com/ejfitzgerald/clang-tidy-cache/clang" "io" "io/ioutil" "os" "os/exec" "os/user" "path" + + "github.com/ejfitzgerald/clang-tidy-cache/caches" + "github.com/ejfitzgerald/clang-tidy-cache/clang" ) const VERSION = "0.3.0" diff --git a/utils/fileSystem.go b/utils/fileSystem.go index 264b32e..aa2ce9d 100644 --- a/utils/fileSystem.go +++ b/utils/fileSystem.go @@ -29,7 +29,26 @@ func FindInParents(searchDir string, filename string) (string, error) { // @param {string} givenPath Path to convert // // @returns {string} Converted filepath -// func PosixifyPath(givenPath string) string { return strings.ReplaceAll(givenPath, "\\", "/") } + +// NormalizePath normalizes the given path between Windows and POSIX +// and removes the leading "./" if present +// +// @param {string} path Path to normalize +// +// @returns {string} Normalized path +func NormalizePath(path string) string { + var normalizedPath string + + // Normalize the path to use POSIX separators + normalizedPath = PosixifyPath(path) + + // Remove the leading "./" if present + if normalizedPath[0:2] == "./" { + normalizedPath = normalizedPath[2:] + } + + return normalizedPath +}