Skip to content
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

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

Merged
merged 8 commits into from
Jun 27, 2024
7 changes: 5 additions & 2 deletions private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 62 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, there is only ever a single ModuleConfig, so its LintConfig is returned.
// 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 files, there is only ever a single ModuleConfig, so its BreakingConfig is returned.
// 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,20 @@ func (c *bufYAMLFile) ModuleConfigs() []ModuleConfig {
return slicesext.Copy(c.moduleConfigs)
}

func (c *bufYAMLFile) DefaultLintConfig() LintConfig {
if c.defaultLintConfig == nil {
return c.moduleConfigs[0].LintConfig()
}
return c.defaultLintConfig
}

func (c *bufYAMLFile) DefaultBreakingConfig() BreakingConfig {
if c.defaultBreakingConfig == nil {
return c.moduleConfigs[0].BreakingConfig()
Copy link
Member

Choose a reason for hiding this comment

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

This isn't actually correct - this is just choosing a single configuration among potentially many and calling it the default, which is what we've tried to avoid pretty consistently with v2. I think what you're actually looking for in this PR is some concept of "if there is a single configuration, give it to me, otherwise tell me it is not present". What this logic looks like:

SingleLintConfig() (LintConfig, bool)
  • If v1, return the v1 configuration.
  • If v2:
    • If there is one module with attached configuration, return the attached configuration. Else, return the top-level configuation.
    • If there is more than one module, return false

If you get false, do nothing, as we do right now.

Copy link
Member Author

@doriable doriable Jun 26, 2024

Choose a reason for hiding this comment

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

So if a user passes the following file to --config:

version: v2
lint:
  use:
    - MINIMAL
breaking:
  use:
    - WIRE
modules:
  - path: foo
  - path: bar
    lint:
      use:
        - DEFAULT
      except:
        - IMPORT_USED
  - path: vendor

Wouldn't we want them to pick up the top-level configs for the default in the case of:

$ buf breaking base.bin --against new.bin --config custom.yaml

Copy link
Member

Choose a reason for hiding this comment

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

We'd probably want to error, or at least print a warning along the lines of:

WARN Configuration {from flag,at $PATH was used} but this configuration had multiple modules. Buf cannot deduce which {lint,breaking} configuration to use with your image. Ignoring your configuration and using the default configuration instead.

I could see an argument to also modify the logic to "if >1 modules, return the top-level module", but that may just leave us open to even more support stuff in the long-term.

Going a different direction, we could take the position that for image inputs only, we always take the top-level configuration, and never look into specific module configs. This would mean that if you had the following:

version: v2
modules:
  - path: .
     lint: ...

The modules[0].LintConfig would not be picked up and propagated. The position here would be that module-specific configuration never applies to an image, and we treat configuration for an image as if it were a "new" module in the modules list.

This may be what moduleConfigs[0] does in practice for v1, but not for v2, but I'd also be comfortable with adopting this position, it seems intellectually consistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am a fan of the latter approach for images. For v1, the behaviour will stay the same since there should only ever be a single lint/breaking config. For v2, we check for a top-level config, and if nothing is there, then we would use whatever the version defaults are. And yes, in the case you provided, where there is a single module, path: ., that would not be considered the default configuration. Changes incoming.

}
return c.defaultBreakingConfig
doriable marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *bufYAMLFile) ConfiguredDepModuleRefs() []bufmodule.ModuleRef {
return slicesext.Copy(c.configuredDepModuleRefs)
}
Expand Down Expand Up @@ -351,6 +390,8 @@ func readBufYAMLFile(
[]ModuleConfig{
moduleConfig,
},
DefaultLintConfigV1,
DefaultBreakingConfigV1,
configuredDepModuleRefs,
)
case FileVersionV2:
Expand Down Expand Up @@ -464,6 +505,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 +531,8 @@ func readBufYAMLFile(
fileVersion,
objectData,
moduleConfigs,
defaultLintConfig,
defaultBreakingConfig,
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