From 5e4298e968007b20bf10aceadf3dc1724ff09b18 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 30 Dec 2021 11:35:52 +0100 Subject: [PATCH] Let runfiles env vars take priority This fixes https://github.com/bazelbuild/bazel/issues/14500. --- runfiles.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/runfiles.go b/runfiles.go index c57e1a5..a4dafe7 100644 --- a/runfiles.go +++ b/runfiles.go @@ -89,6 +89,18 @@ func New(opts ...Option) (*Runfiles, error) { } // See section “Runfiles discovery” in // https://docs.google.com/document/d/e/2PACX-1vSDIrFnFvEYhKsCMdGdD40wZRBX3m3aZ5HhVj4CtHPmiXKDCxioTUbYsDydjKtFDAzER5eg7OjJWs3V/pub. + // We deviate from the doc (as of 2021-12-30) in one point to work around + // https://github.com/bazelbuild/bazel/issues/14500: + // If runfiles environment variables are set, they take priority over the + // runfiles dirs and manifests that potentially exist next to the binary. + // This is necessary since the Bazel cache can contain outdated runfiles + // trees next to non-top-level binaries. + if o.manifest != "" { + return o.manifest.new() + } + if o.directory != "" { + return o.directory.new(), nil + } manifest := ManifestFile(o.program + ".runfiles_manifest") if stat, err := os.Stat(string(manifest)); err == nil && stat.Mode().IsRegular() { return manifest.new() @@ -97,12 +109,6 @@ func New(opts ...Option) (*Runfiles, error) { if stat, err := os.Stat(string(dir)); err == nil && stat.IsDir() { return dir.new(), nil } - if o.manifest != "" { - return o.manifest.new() - } - if o.directory != "" { - return o.directory.new(), nil - } return nil, errors.New("runfiles: no runfiles found") }