diff --git a/main.go b/main.go index f40debf..ad9c870 100644 --- a/main.go +++ b/main.go @@ -1257,11 +1257,17 @@ func transformLink(args []string) ([]string, error) { if pkgPath == "main" { pkgPath = cache.MainImportPath } - id := cache.ListedPackages[pkgPath].GarbleActionID - newName := hashWith(id, name) + lpkg := cache.ListedPackages[pkgPath] + if lpkg == nil { + // We couldn't find the package. + // Perhaps a typo, perhaps not part of the build. + // cmd/link ignores those, so we should too. + return + } + newName := hashWith(lpkg.GarbleActionID, name) newPkg := pkg if pkg != "main" && isPrivate(pkg) { - newPkg = hashWith(id, pkg) + newPkg = hashWith(lpkg.GarbleActionID, pkg) } flags = append(flags, fmt.Sprintf("-X=%s.%s=%s", newPkg, newName, str)) }) diff --git a/testdata/scripts/ldflags.txt b/testdata/scripts/ldflags.txt index 1121447..3a7acc0 100644 --- a/testdata/scripts/ldflags.txt +++ b/testdata/scripts/ldflags.txt @@ -1,19 +1,21 @@ # Note the proper domain, since the dot adds an edge case. env GOPRIVATE=domain.test/main -garble build -ldflags='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced' +env LDFLAGS='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced -X=domain.test/missing/path.missingVar=value' + +garble build -ldflags=${LDFLAGS} exec ./main cmp stderr main.stderr ! binsubstr main$exe 'unexportedVersion' [short] stop # no need to verify this with -short -garble -tiny build -ldflags='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced' +garble -tiny build -ldflags=${LDFLAGS} exec ./main cmp stderr main.stderr ! binsubstr main$exe 'unexportedVersion' -exec go build -ldflags='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced' +exec go build -ldflags=${LDFLAGS} exec ./main cmp stderr main.stderr binsubstr main$exe 'unexportedVersion'