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 all commits
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
30 changes: 17 additions & 13 deletions private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,24 @@ func (c *controller) GetTargetImageWithConfigs(
// We did not find a buf.yaml in our current directory, and there was no config override.
// Use the defaults.
} else {
switch fileVersion := bufYAMLFile.FileVersion(); fileVersion {
case bufconfig.FileVersionV1Beta1, bufconfig.FileVersionV1:
moduleConfigs := bufYAMLFile.ModuleConfigs()
if len(moduleConfigs) != 1 {
return nil, fmt.Errorf("expected 1 ModuleConfig for FileVersion %v, got %d", len(moduleConfigs), fileVersion)
if topLevelLintConfig := bufYAMLFile.TopLevelLintConfig(); topLevelLintConfig == nil {
// Ensure that this is a v2 config
if fileVersion := bufYAMLFile.FileVersion(); fileVersion != bufconfig.FileVersionV2 {
return nil, syserror.Newf("non-v2 version with no top-level lint config: %s", fileVersion)
}
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
// breaking config to apply.
default:
return nil, syserror.Newf("unknown FileVersion: %v", fileVersion)
// v2 config without a top-level lint config, use v2 default
lintConfig = bufconfig.DefaultLintConfigV2
} else {
lintConfig = topLevelLintConfig
}
if topLevelBreakingConfig := bufYAMLFile.TopLevelBreakingConfig(); topLevelBreakingConfig == nil {
if fileVersion := bufYAMLFile.FileVersion(); fileVersion != bufconfig.FileVersionV2 {
return nil, syserror.Newf("non-v2 version with no top-level breaking config: %s", fileVersion)
}
// v2 config without a top-level breaking config, use v2 default
breakingConfig = bufconfig.DefaultBreakingConfigV2
} else {
breakingConfig = topLevelBreakingConfig
}
}
return []ImageWithConfig{
Expand Down
2 changes: 1 addition & 1 deletion private/buf/buffetch/buffetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Ref interface {
internalRef() internal.Ref
}

// MessageRef is an message file reference.
// MessageRef is a message file reference.
type MessageRef interface {
Ref
MessageEncoding() MessageEncoding
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
65 changes: 64 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,20 @@ type BufYAMLFile interface {
// All ModuleConfigs will have unique ModuleFullNames.
// Sorted by DirPath.
ModuleConfigs() []ModuleConfig
// TopLevelLintConfig returns the top-level LintConfig for the File.
//
// For v1 buf.yaml files, there is only ever a single LintConfig, so this is returned.
// For v2 buf.yaml files, if a top-level lint config exists, then it will be the top-level
// lint config. Otherwise, this will return nil, so callers should be aware this may be
// empty.
TopLevelLintConfig() LintConfig
// TopLevelBreakingConfig returns the top-level BreakingConfig for the File.
//
// For v1 buf.yaml files, there is only ever a single BreakingConfig, so this is returned.
// For v2 buf.yaml files, if a top-level breaking config exists, then it will be the top-level
// breaking config. Otherwise, this will return nil, so callers should be aware this may be
// empty.
TopLevelBreakingConfig() BreakingConfig
// ConfiguredDepModuleRefs returns the configured dependencies of the Workspace as ModuleRefs.
//
// These come from buf.yaml files.
Expand All @@ -93,7 +107,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 top-level lint config, use only module configs
nil, // Do not set top-level breaking config, use only module configs
configuredDepModuleRefs,
)
}

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

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

func (c *bufYAMLFile) TopLevelLintConfig() LintConfig {
return c.topLevelLintConfig
}

func (c *bufYAMLFile) TopLevelBreakingConfig() BreakingConfig {
return c.topLevelBreakingConfig
}

func (c *bufYAMLFile) ConfiguredDepModuleRefs() []bufmodule.ModuleRef {
return slicesext.Copy(c.configuredDepModuleRefs)
}
Expand Down Expand Up @@ -351,6 +386,8 @@ func readBufYAMLFile(
[]ModuleConfig{
moduleConfig,
},
lintConfig,
breakingConfig,
configuredDepModuleRefs,
)
case FileVersionV2:
Expand Down Expand Up @@ -464,6 +501,30 @@ func readBufYAMLFile(
}
moduleConfigs = append(moduleConfigs, moduleConfig)
}
var topLevelLintConfig LintConfig
if !defaultExternalLintConfig.isEmpty() {
topLevelLintConfig, err = getLintConfigForExternalLintV2(
fileVersion,
defaultExternalLintConfig,
".", // The top-level module config always has the root "."
false, // Not module-specific configuration
)
if err != nil {
return nil, err
}
}
var topLevelBreakingConfig BreakingConfig
if !defaultExternalBreakingConfig.isEmpty() {
topLevelBreakingConfig, 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 +533,8 @@ func readBufYAMLFile(
fileVersion,
objectData,
moduleConfigs,
topLevelLintConfig,
topLevelBreakingConfig,
configuredDepModuleRefs,
)
default:
Expand Down
2 changes: 1 addition & 1 deletion private/bufpkg/bufconfig/lint_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
false,
false,
"",
false,
true, // We default to allowing comment ignores in v2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkwarren caught an issue with the default v2 lint config, which should default to allowing comment ignores (https://buf.build/docs/configuration/v2/buf-yaml)

  # v1 configurations had an allow_comment_ignores option to opt-in to comment ignores.
  #
  # In v2, we allow comment ignores by default, and allow opt-out from comment ignores
  # with the disallow_comment_ignores option.
  disallow_comment_ignores: false

)
)

Expand Down