Skip to content

Set top-level LintConfig and BreakingConfig and use for image inputs #3111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ func (c *controller) GetTargetImageWithConfigs(
if err != nil {
return nil, err
}
lintConfig := bufconfig.DefaultLintConfigV1
breakingConfig := bufconfig.DefaultBreakingConfigV1
lintConfig := bufconfig.DefaultLintConfigV2
breakingConfig := bufconfig.DefaultBreakingConfigV2
bufYAMLFile, err := bufconfig.GetBufYAMLFileForPrefixOrOverride(
ctx,
bucket,
Expand All @@ -402,9 +402,12 @@ func (c *controller) GetTargetImageWithConfigs(
lintConfig = moduleConfigs[0].LintConfig()
breakingConfig = moduleConfigs[0].BreakingConfig()
case bufconfig.FileVersionV2:
// Do nothing. Use the default LintConfig and BreakingConfig. With
// the new buf.yamls with multiple modules, we don't know what lint or
// Use the default LintConfig and BreakingConfig from the file. This is
// the top-level lint and/or breaking config(s) if any are set or the default v2
// configs. v2 buf.yamls may contain multiple modules, we don't know what lint or
// breaking config to apply.
lintConfig = bufYAMLFile.DefaultLintConfig()
breakingConfig = bufYAMLFile.DefaultBreakingConfig()
default:
return nil, syserror.Newf("unknown FileVersion: %v", fileVersion)
}
Expand Down
17 changes: 17 additions & 0 deletions private/buf/cmd/buf/buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,23 @@ a/v3/a.proto:7:10:Field "2" on message "Foo" changed name from "value" to "Value
"--exclude-path",
filepath.Join("a", "v3", "foo"),
)
testRunStdoutStderrNoWarn(
t,
nil,
bufctl.ExitCodeFileAnnotation,
`a/v3/a.proto:6:3:Field "1" with name "key" on message "Foo" changed type from "string" to "int32". See https://developers.google.com/protocol-buffers/docs/proto3#updating for wire compatibility rules.`,
"",
"breaking",
filepath.Join(tempDir, "current.binpb"),
"--against",
filepath.Join(tempDir, "previous.binpb"),
"--path",
filepath.Join("a", "v3"),
"--exclude-path",
filepath.Join("a", "v3", "foo"),
"--config",
`{"version":"v2","breaking":{"use":["WIRE"]}}`,
)
}

func TestVersion(t *testing.T) {
Expand Down
57 changes: 56 additions & 1 deletion private/bufpkg/bufconfig/buf_yaml_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ type BufYAMLFile interface {
// All ModuleConfigs will have unique ModuleFullNames.
// Sorted by DirPath.
ModuleConfigs() []ModuleConfig
// DefaultLintConfig returns the DefaultLintConfig for the File.
//
// For v1 buf.yaml files, this will be the default v1 lint config.
// For v2 buf.yaml files, if a top-level lint config exists, then it will be the top-level
// lint config. Otherwise it is the default v2 lint config.
DefaultLintConfig() LintConfig
// DefaultBreakingConfig returns the DefaultBreakingConfig for the File.
//
// For v1 buf.yaml, this will be the default v1 breaking config.
// For v2 buf.yaml files, if a top-level breaking config exists, then it will be the top-level
// breaking config. Otherwise it is the default v2 breaking config.
DefaultBreakingConfig() BreakingConfig
// ConfiguredDepModuleRefs returns the configured dependencies of the Workspace as ModuleRefs.
//
// These come from buf.yaml files.
Expand All @@ -93,7 +105,14 @@ func NewBufYAMLFile(
moduleConfigs []ModuleConfig,
configuredDepModuleRefs []bufmodule.ModuleRef,
) (BufYAMLFile, error) {
return newBufYAMLFile(fileVersion, nil, moduleConfigs, configuredDepModuleRefs)
return newBufYAMLFile(
fileVersion,
nil,
moduleConfigs,
nil, // Do not set default lint config, use only module configs
nil, // Do not set default breaking config, use only module configs
configuredDepModuleRefs,
)
}

// GetBufYAMLFileForPrefix gets the buf.yaml file at the given bucket prefix.
Expand Down Expand Up @@ -190,13 +209,17 @@ type bufYAMLFile struct {
fileVersion FileVersion
objectData ObjectData
moduleConfigs []ModuleConfig
defaultLintConfig LintConfig
defaultBreakingConfig BreakingConfig
configuredDepModuleRefs []bufmodule.ModuleRef
}

func newBufYAMLFile(
fileVersion FileVersion,
objectData ObjectData,
moduleConfigs []ModuleConfig,
defaultLintConfig LintConfig,
defaultBreakingConfig BreakingConfig,
configuredDepModuleRefs []bufmodule.ModuleRef,
) (*bufYAMLFile, error) {
if (fileVersion == FileVersionV1Beta1 || fileVersion == FileVersionV1) && len(moduleConfigs) > 1 {
Expand Down Expand Up @@ -255,6 +278,8 @@ func newBufYAMLFile(
fileVersion: fileVersion,
objectData: objectData,
moduleConfigs: moduleConfigs,
defaultLintConfig: defaultLintConfig,
defaultBreakingConfig: defaultBreakingConfig,
configuredDepModuleRefs: configuredDepModuleRefs,
}, nil
}
Expand All @@ -275,6 +300,14 @@ func (c *bufYAMLFile) ModuleConfigs() []ModuleConfig {
return slicesext.Copy(c.moduleConfigs)
}

func (c *bufYAMLFile) DefaultLintConfig() LintConfig {
return c.defaultLintConfig
}

func (c *bufYAMLFile) DefaultBreakingConfig() BreakingConfig {
return c.defaultBreakingConfig
}

func (c *bufYAMLFile) ConfiguredDepModuleRefs() []bufmodule.ModuleRef {
return slicesext.Copy(c.configuredDepModuleRefs)
}
Expand Down Expand Up @@ -351,6 +384,8 @@ func readBufYAMLFile(
[]ModuleConfig{
moduleConfig,
},
DefaultLintConfigV1,
DefaultBreakingConfigV1,
configuredDepModuleRefs,
)
case FileVersionV2:
Expand Down Expand Up @@ -464,6 +499,24 @@ func readBufYAMLFile(
}
moduleConfigs = append(moduleConfigs, moduleConfig)
}
defaultLintConfig, err := getLintConfigForExternalLintV2(
fileVersion,
defaultExternalLintConfig,
".", // The top-level module config always has the root "."
false, // Not module-specific configuration
)
if err != nil {
return nil, err
}
defaultBreakingConfig, err := getBreakingConfigForExternalBreaking(
fileVersion,
defaultExternalBreakingConfig,
".", // The top-level module config always has the root "."
false, // Not module-specific configuration
)
if err != nil {
return nil, err
}
configuredDepModuleRefs, err := getConfiguredDepModuleRefsForExternalDeps(externalBufYAMLFile.Deps)
if err != nil {
return nil, err
Expand All @@ -472,6 +525,8 @@ func readBufYAMLFile(
fileVersion,
objectData,
moduleConfigs,
defaultLintConfig,
defaultBreakingConfig,
configuredDepModuleRefs,
)
default:
Expand Down