toPosition returns the correct position information for the given pos, removing sandbox prefix if any.
(pos token.Pos)
| 270 | // toPosition returns the correct position information for the given pos, removing sandbox prefix |
| 271 | // if any. |
| 272 | func (p *primitivizer) toPosition(pos token.Pos) token.Position { |
| 273 | // Generated files contain "//line" directives that point back to the original source file |
| 274 | // for better error reporting, and PositionFor supports reading that information and adjust |
| 275 | // the position accordingly (i.e., returning a position that points back to the original source |
| 276 | // file). However, since we are using the precise position information for correctly |
| 277 | // identifying upstream objects in our cross-package inference, such adjustment will break it |
| 278 | // the inference (downstream analysis knows nothing about the "original source file"). |
| 279 | // Therefore, here we explicitly disable the adjustment. |
| 280 | position := p.pass.Fset.PositionFor(pos, false /* adjusted */) |
| 281 | |
| 282 | // For build systems that employ sandboxing (e.g., bazel), the file names in the `Fset` may |
| 283 | // contain a random prefix. For example: |
| 284 | // <SANDBOX_PREFIX>/<WORKSPACE_UUID>/src/mypkg/mysrc1.go |
| 285 | // <SANDBOX_PREFIX>/<WORKSPACE_UUID>/src/mypkg/mysrc2.go |
| 286 | // src/upstream/mysrc1.go |
| 287 | // src/upstream/mysrc2.go |
| 288 | // Notice that the upstream files do not have this prefix, since this information is loaded |
| 289 | // from archive file (that stores the symbol information etc.), but not from the sandbox. |
| 290 | // So, we trim the `<SANDBOX_PREFIX>/<WORKSPACE_UUID>/` here, which is CWD set by bazel build. |
| 291 | // For other drivers (standard or golangci-lint), we won't even have this prefix prepended, |
| 292 | // since the file paths will always be in the form of the relative paths (e.g., |
| 293 | // `src/mypkg/mysrc1.go`). Trimming the prefixes here for them is simply a no-op. |
| 294 | position.Filename = tokenhelper.RelToCwd(position.Filename) |
| 295 | |
| 296 | return position |
| 297 | } |
no test coverage detected