Skip to content

Commit f0caf9f

Browse files
committed
Add runtime check for trimmed path
Add a check that tests whether paths are trimmed at runtime using call frame. This utility can then be used by downstream dependencies.
1 parent 8dd2534 commit f0caf9f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

check/runtime.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Package check provides utilities to inspect properties of the running Go
2+
// binary, with a focus on detecting whether certain build-time flags were used
3+
// during compilation.
4+
//
5+
// For example, using -trimpath can remove absolute file paths from the compiled
6+
// executable, which affects stack traces and error reporting. This package helps
7+
// detect that condition at runtime, which can be useful for debugging, logging,
8+
// or reproducibility checks that otherwise can result in chain halt.
9+
package check
10+
11+
import (
12+
"runtime"
13+
"strings"
14+
)
15+
16+
// RuntimePathTrimmed reports whether the Go binary was likely built with
17+
// the `-trimpath` flag by inspecting the file path of the current call stack.
18+
//
19+
// It checks if the file path of the caller frame contains the original module
20+
// path ("github.com/CosmWasm/wasmd"). If the path contains this prefix, it
21+
// likely means that `-trimpath` was used to remove or rewrite file system paths
22+
// in the compiled executable.
23+
//
24+
// Note that building with `-trimpath` can remove absolute paths from stack
25+
// traces, which may affect reproducibility of error reporting.
26+
//
27+
// Returns:
28+
// - trimmed: true if the file path indicates that the module path was trimmed.
29+
// - ok: true if runtime caller information could be retrieved successfully.
30+
func RuntimePathTrimmed() (trimmed, ok bool) {
31+
switch _, file, _, success := runtime.Caller(0); {
32+
case !success:
33+
return false, false
34+
default:
35+
const modulePathPrefix = "github.com/CosmWasm/wasmd"
36+
return strings.HasPrefix(file, modulePathPrefix), true
37+
}
38+
}

0 commit comments

Comments
 (0)