Skip to content

Commit 9d1a8a7

Browse files
authored
Merge pull request #22129 from jketema/jketema/subst-fix
Introduce canonicalization library for Windows `subst` drives
2 parents 3a48972 + 726705b commit 9d1a8a7

16 files changed

Lines changed: 222 additions & 23 deletions

File tree

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ go_sdk.download(version = "1.26.5")
322322

323323
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
324324
go_deps.from_file(go_mod = "//go/extractor:go.mod")
325-
use_repo(go_deps, "com_github_stretchr_testify", "org_golang_x_mod", "org_golang_x_tools")
325+
use_repo(go_deps, "com_github_stretchr_testify", "org_golang_x_mod", "org_golang_x_sys", "org_golang_x_tools")
326326

327327
ripunzip_archive = use_repo_rule("//misc/ripunzip:ripunzip.bzl", "ripunzip_archive")
328328

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
| code/Program.cs:0:0:0:0 | code/Program.cs | |
2-
| code/obj/Debug/net9.0/.NETCoreApp,Version=v9.0.AssemblyAttributes.cs:0:0:0:0 | code/obj/Debug/net9.0/.NETCoreApp,Version=v9.0.AssemblyAttributes.cs | |
3-
| code/obj/Debug/net9.0/dotnet_build.AssemblyInfo.cs:0:0:0:0 | code/obj/Debug/net9.0/dotnet_build.AssemblyInfo.cs | |
4-
| code/obj/Debug/net9.0/dotnet_build.GlobalUsings.g.cs:0:0:0:0 | code/obj/Debug/net9.0/dotnet_build.GlobalUsings.g.cs | |
5-
| code/obj/Debug/net9.0/dotnet_build.dll:0:0:0:0 | code/obj/Debug/net9.0/dotnet_build.dll | |
1+
| code/Program.cs:0:0:0:0 | code/Program.cs | relative |
2+
| code/dotnet_build.csproj:0:0:0:0 | code/dotnet_build.csproj | relative |
3+
| code/obj/Debug/net9.0/.NETCoreApp,Version=v9.0.AssemblyAttributes.cs:0:0:0:0 | code/obj/Debug/net9.0/.NETCoreApp,Version=v9.0.AssemblyAttributes.cs | relative |
4+
| code/obj/Debug/net9.0/dotnet_build.AssemblyInfo.cs:0:0:0:0 | code/obj/Debug/net9.0/dotnet_build.AssemblyInfo.cs | relative |
5+
| code/obj/Debug/net9.0/dotnet_build.GlobalUsings.g.cs:0:0:0:0 | code/obj/Debug/net9.0/dotnet_build.GlobalUsings.g.cs | relative |
6+
| code/obj/Debug/net9.0/dotnet_build.dll:0:0:0:0 | code/obj/Debug/net9.0/dotnet_build.dll | relative |
7+
| code/obj/dotnet_build.csproj.nuget.g.props:0:0:0:0 | code/obj/dotnet_build.csproj.nuget.g.props | relative |
68
| file://:0:0:0:0 | | |
7-
| file://Z:/dotnet_build.csproj:0:0:0:0 | Z:/dotnet_build.csproj | relative |
8-
| file://Z:/obj/dotnet_build.csproj.nuget.g.props:0:0:0:0 | Z:/obj/dotnet_build.csproj.nuget.g.props | relative |

go/extractor/extractor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ func normalizedPath(ast *ast.File, fset *token.FileSet) string {
770770
if err != nil {
771771
return file
772772
}
773-
return path
773+
return util.ResolvePath(path)
774774
}
775775

776776
// extractFile extracts AST information for the given file

go/extractor/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ require (
1313
golang.org/x/tools v0.48.0
1414
)
1515

16-
require github.com/stretchr/testify v1.11.1
16+
require (
17+
github.com/stretchr/testify v1.11.1
18+
golang.org/x/sys v0.47.0
19+
)
1720

1821
require (
1922
github.com/davecgh/go-spew v1.1.1 // indirect

go/extractor/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk=
1010
golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40=
1111
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
1212
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
13+
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
14+
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
1315
golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE=
1416
golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk=
1517
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

go/extractor/util/BUILD.bazel

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/util/subst_other.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build !windows
2+
3+
package util
4+
5+
// ResolvePath is a no-op on non-Windows platforms.
6+
func ResolvePath(path string) string { return path }

go/extractor/util/subst_windows.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//go:build windows
2+
3+
package util
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"syscall"
9+
"unsafe"
10+
11+
"golang.org/x/sys/windows"
12+
)
13+
14+
var (
15+
dll *syscall.DLL
16+
procResolve *syscall.Proc
17+
procFree *syscall.Proc
18+
available bool
19+
)
20+
21+
func init() {
22+
dist := os.Getenv("CODEQL_DIST")
23+
if dist == "" {
24+
return
25+
}
26+
dllPath := filepath.Join(dist, "tools", "win64", "canonicalize.dll")
27+
d, err := syscall.LoadDLL(dllPath)
28+
if err != nil {
29+
return
30+
}
31+
p, err := d.FindProc("resolve_subst")
32+
if err != nil {
33+
d.Release()
34+
return
35+
}
36+
f, err := d.FindProc("resolve_subst_free")
37+
if err != nil {
38+
d.Release()
39+
return
40+
}
41+
dll = d
42+
procResolve = p
43+
procFree = f
44+
available = true
45+
}
46+
47+
// If "path" is an absolute path starting with a "subst"ed drive letter, return an
48+
// equivalent path with the drive letter replaced by its target. Otherwise return
49+
// "path" unchanged.
50+
func ResolvePath(path string) string {
51+
if len(path) < 3 {
52+
return path
53+
}
54+
if path[1] != ':' {
55+
return path
56+
}
57+
if path[2] != '\\' && path[2] != '/' {
58+
return path
59+
}
60+
c := path[0]
61+
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
62+
return path
63+
}
64+
65+
resolved, ok := resolveDrive(path[:3])
66+
if !ok {
67+
return path
68+
}
69+
return resolved + path[2:]
70+
}
71+
72+
// Given a drive root like "X:\" (or "X:/"), returns the path that drive is
73+
// "subst"ed to. Returns false if the drive is not "subst"ed or an error occurred.
74+
func resolveDrive(driveRoot string) (string, bool) {
75+
if !available {
76+
return "", false
77+
}
78+
driveBytes, err := windows.ByteSliceFromString(driveRoot)
79+
if err != nil {
80+
return "", false
81+
}
82+
ret, _, _ := procResolve.Call(uintptr(unsafe.Pointer(&driveBytes[0])))
83+
if ret == 0 {
84+
return "", false
85+
}
86+
result := windows.BytePtrToString((*byte)(unsafe.Pointer(ret)))
87+
if procFree != nil {
88+
procFree.Call(ret)
89+
}
90+
return result, true
91+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| file://Z:/main.go:0:0:0:0 | Z:/main.go | relative |
1+
| code/main.go:0:0:0:0 | code/main.go | relative |

java/kotlin-extractor/src/main/java/com/semmle/util/files/FileUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,13 +1237,14 @@ public static String relativePathLink (File f, File base)
12371237

12381238
/**
12391239
* Try to convert a file into a canonical file. Handles the possible IO exception by just making
1240-
* the path absolute.
1240+
* the path absolute. Also resolves subst drives on Windows.
12411241
*/
12421242
public static File tryMakeCanonical (File f)
12431243
{
12441244
try {
1245-
return f.getCanonicalFile();
1246-
}
1245+
// getCanonicalFile does not canonicalize subst drives on Windows, so do this separately. This
1246+
// is a no-op on non-Windows platforms.
1247+
return SubstResolver.resolve(f.getCanonicalFile()); }
12471248
catch (IOException ignored) {
12481249
Exceptions.ignore(ignored, "Can't log error: Could be too verbose.");
12491250
return new File(simplifyPath(f));

0 commit comments

Comments
 (0)