1515package librarian
1616
1717import (
18+ "context"
1819 _ "embed"
20+ "errors"
1921 "fmt"
2022 "runtime/debug"
2123 "strings"
@@ -25,6 +27,11 @@ import (
2527 "github.com/googleapis/librarian/internal/yaml"
2628)
2729
30+ var (
31+ errNoConfigVersion = errors .New ("librarian.yaml does not specify a version" )
32+ errVersionMismatch = errors .New ("version mismatch" )
33+ )
34+
2835//go:embed version.txt
2936var versionString string
3037
@@ -67,31 +74,34 @@ func version(info *debug.BuildInfo) string {
6774// loadConfig reads librarian.yaml and verifies that the librarian binary
6875// version matches the version specified in the configuration file. It returns
6976// the config and an error if the versions do not match. The check is skipped
70- // if the binary version is "not available", which occurs during local
71- // development without VCS info.
72- func loadConfig () (* config.Config , error ) {
77+ // if the -f flag is set or if the binary version is "not available", which
78+ // occurs during local development without VCS info.
79+ func loadConfig (ctx context. Context ) (* config.Config , error ) {
7380 cfg , err := yaml.Read [config.Config ](librarianConfigPath )
7481 if err != nil {
7582 return nil , fmt .Errorf ("%w: %w" , errConfigNotFound , err )
7683 }
77- if err := compareVersions (cfg .Version , Version ()); err != nil {
78- return nil , err
84+ if ! skipVersionCheck (ctx ) {
85+ if err := compareVersions (cfg .Version , Version ()); err != nil {
86+ return nil , err
87+ }
7988 }
8089 return cfg , nil
8190}
8291
8392func compareVersions (configVersion , binaryVersion string ) error {
8493 if configVersion == "" {
85- return fmt . Errorf ( "librarian.yaml does not specify a version" )
94+ return errNoConfigVersion
8695 }
8796 // Skip check for local builds, which have no version info.
8897 if binaryVersion == versionNotAvailable {
8998 return nil
9099 }
91100 if configVersion != binaryVersion {
92- return fmt .Errorf (`binary version %s does not match librarian.yaml version %s
93- go run github.com/googleapis/librarian/cmd/librarian@%s` ,
94- binaryVersion , configVersion , configVersion )
101+ return fmt .Errorf (`%w: binary version %s does not match librarian.yaml version %s
102+ go run github.com/googleapis/librarian/cmd/librarian@%s
103+ or use -f to skip this check` ,
104+ errVersionMismatch , binaryVersion , configVersion , configVersion )
95105 }
96106 return nil
97107}
@@ -134,3 +144,9 @@ func newPseudoVersion(info *debug.BuildInfo) string {
134144 }
135145 return buf .String ()
136146}
147+
148+ // skipVersionCheck returns true if the -f flag was set to skip version checking.
149+ func skipVersionCheck (ctx context.Context ) bool {
150+ v , _ := ctx .Value (skipVersionCheckKey {}).(bool )
151+ return v
152+ }
0 commit comments