Inline getBUILDPathAndBUILDFile. Also move responsibility for determining the workspace-relative BUILD path from readBUILD to getBUILDPath. PiperOrigin-RevId: 269361049
diff --git a/ts_auto_deps/updater/test_register.go b/ts_auto_deps/updater/test_register.go index 19529c9..291e564 100644 --- a/ts_auto_deps/updater/test_register.go +++ b/ts_auto_deps/updater/test_register.go
@@ -50,7 +50,7 @@ // declaration var err error var buildPath string - g3root, buildPath, err = getBUILDPath(ctx, path) + g3root, buildPath, _, err = getBUILDPath(ctx, path) if err != nil { return false, nil, err } @@ -108,7 +108,7 @@ } func (reg *buildRegistry) readBUILD(ctx context.Context, workspaceRoot, buildFilePath string) (*build.File, error) { - normalizedG3Path, err := getAbsoluteBUILDPath(workspaceRoot, buildFilePath) + normalizedG3Path, err := getWorkspaceRelativePath(workspaceRoot, buildFilePath) if err != nil { return nil, err } @@ -117,7 +117,7 @@ return bld, nil } - bld, err := readBUILD(ctx, workspaceRoot, buildFilePath) + bld, err := readBUILD(ctx, buildFilePath, normalizedG3Path) if err != nil { return nil, err }
diff --git a/ts_auto_deps/updater/updater.go b/ts_auto_deps/updater/updater.go index e6585d2..f121c44 100644 --- a/ts_auto_deps/updater/updater.go +++ b/ts_auto_deps/updater/updater.go
@@ -173,36 +173,18 @@ return done } -// getAbsoluteBUILDPath relativizes the absolute build path so that buildFilePath -// is definitely below workspaceRoot. -func getAbsoluteBUILDPath(workspaceRoot, buildFilePath string) (string, error) { - absPath, err := filepath.Abs(buildFilePath) - if err != nil { - return "", err - } - g3Path, err := filepath.Rel(workspaceRoot, absPath) - if err != nil { - return "", err - } - return platform.Normalize(g3Path), nil -} - // readBUILD loads the BUILD file, if present, or returns a new empty one. -// workspaceRoot must be an absolute path and buildFilePath is interpreted as -// relative to CWD, and must be underneath workspaceRoot. -func readBUILD(ctx context.Context, workspaceRoot, buildFilePath string) (*build.File, error) { - normalizedG3Path, err := getAbsoluteBUILDPath(workspaceRoot, buildFilePath) - if err != nil { - return nil, fmt.Errorf("failed to resolve workspace relative path: %s", err) - } +// buildFilePath is relative to CWD, and workspaceRelativePath is relative to +// the workspace root (the directory containing the WORKSPACE file). +func readBUILD(ctx context.Context, buildFilePath, workspaceRelativePath string) (*build.File, error) { data, err := platform.ReadFile(ctx, buildFilePath) if err != nil { if os.IsNotExist(err) { - return &build.File{Path: normalizedG3Path, Type: build.TypeBuild}, nil + return &build.File{Path: workspaceRelativePath, Type: build.TypeBuild}, nil } return nil, fmt.Errorf("reading %q: %s", buildFilePath, err) } - bld, err := build.ParseBuild(normalizedG3Path, data) + bld, err := build.ParseBuild(workspaceRelativePath, data) if err != nil { if parseErr, ok := err.(build.ParseError); ok { return nil, &AnalysisFailedError{ @@ -487,33 +469,45 @@ return true, nil } -func getBUILDPath(ctx context.Context, path string) (string, string, error) { - path = strings.TrimSuffix(path, "/BUILD") // Support both package paths and BUILD files - if _, err := platform.Stat(ctx, path); os.IsNotExist(err) { - return "", "", err - } - buildFilePath := filepath.Join(path, "BUILD") - g3root, err := workspace.Root(buildFilePath) +// getWorkspaceRelativePath takes a buildFilePath that's relative to the working +// directory, and returns a path to the BUILD file that's relative to the +// workspaceRoot (the absolute path of the directory containing the WORKSPACE +// file). +func getWorkspaceRelativePath(workspaceRoot, buildFilePath string) (string, error) { + absPath, err := filepath.Abs(buildFilePath) if err != nil { - return "", "", err + return "", err } + workspaceRelativePath, err := filepath.Rel(workspaceRoot, absPath) + if err != nil { + return "", err + } + platform.Normalize(workspaceRelativePath) - return g3root, buildFilePath, nil + return workspaceRelativePath, nil } -func getBUILDPathAndBUILDFile(ctx context.Context, path string) (string, string, *build.File, error) { - g3root, buildFilePath, err := getBUILDPath(ctx, path) +// getBUILDPath takes in a package or BUILD file path, and returns the path of +// the workspace root (the absolute path of the directory containing the +// WORKSPACE file), the BUILD file path relative to the working directory, and +// the BUILD file path relative to the workspace root. +func getBUILDPath(ctx context.Context, path string) (string, string, string, error) { + path = strings.TrimSuffix(path, "/BUILD") // Support both package paths and BUILD files + if _, err := platform.Stat(ctx, path); os.IsNotExist(err) { + return "", "", "", err + } + buildFilePath := filepath.Join(path, "BUILD") + workspaceRoot, err := workspace.Root(buildFilePath) if err != nil { - return "", "", nil, err + return "", "", "", err } - bld, err := readBUILD(ctx, g3root, buildFilePath) + workspaceRelativePath, err := getWorkspaceRelativePath(workspaceRoot, buildFilePath) if err != nil { - platform.Infof("Error reading building file!") - return "", "", nil, err + return "", "", "", err } - return g3root, buildFilePath, bld, nil + return workspaceRoot, buildFilePath, workspaceRelativePath, nil } // isTazeDisabledInPackage checks the BUILD file, or if the BUILD doesn't exist, @@ -652,11 +646,17 @@ // IsTazeDisabledForDir checks if ts_auto_deps is disabled in the BUILD file in the dir, // or if no BUILD file exists, in the closest ancestor BUILD func IsTazeDisabledForDir(ctx context.Context, dir string) (bool, error) { - g3root, buildFilePath, bld, err := getBUILDPathAndBUILDFile(ctx, dir) + g3root, buildFilePath, workspaceRelativePath, err := getBUILDPath(ctx, dir) if err != nil { return false, err } + bld, err := readBUILD(ctx, buildFilePath, workspaceRelativePath) + if err != nil { + platform.Infof("Error reading building file!") + return false, err + } + return isTazeDisabledInPackage(ctx, g3root, buildFilePath, bld), nil } @@ -703,12 +703,18 @@ latencyReport := &LatencyReport{} start := time.Now() - g3root, buildFilePath, bld, err := getBUILDPathAndBUILDFile(ctx, path) - latencyReport.GetBUILD = time.Since(start) + g3root, buildFilePath, workspaceRelativePath, err := getBUILDPath(ctx, path) if err != nil { return false, nil, err } + bld, err := readBUILD(ctx, buildFilePath, workspaceRelativePath) + if err != nil { + platform.Infof("Error reading building file!") + return false, nil, err + } + latencyReport.GetBUILD = time.Since(start) + start = time.Now() ts_auto_depsDisabled := isTazeDisabledInPackage(ctx, g3root, buildFilePath, bld) latencyReport.TazeDisabled = time.Since(start) @@ -1194,7 +1200,7 @@ _, err := platform.Stat(ctx, buildPath) var bld *build.File if err == nil { - bld, err = readBUILD(ctx, workspaceRoot, buildPath) + bld, err = readBUILD(ctx, buildPath, filepath.Join(packagePath, "BUILD")) } else if os.IsNotExist(err) { // Recursively search parent package and cache its location below if found. bld, err = FindBUILDFile(ctx, pkgToBUILD, workspaceRoot, filepath.Dir(packagePath))
diff --git a/ts_auto_deps/updater/updater_test.go b/ts_auto_deps/updater/updater_test.go index b62ef20..613b99f 100644 --- a/ts_auto_deps/updater/updater_test.go +++ b/ts_auto_deps/updater/updater_test.go
@@ -37,7 +37,7 @@ if err != nil { t.Fatal(err) } - bld, err := readBUILD(context.Background(), filepath.Join(testTmpDir, "google3"), p) + bld, err := readBUILD(context.Background(), p, "foo/bar/BUILD") if err != nil { t.Fatal(err) }