Files
opentf/internal/tofu/deprecation_level.go
Martin Atkins e74bf2d0a1 go.mod: Use the new "tool" directive
Previously the Go toolchain had no explicit support for "tools" and so we
used the typical Go community workaround of adding "tools.go" files (two,
for some reason) that existed only to trick the Go toolchain into
considering the tools as dependencies we could track in go.mod.

Go 1.24 introduced explicit support for tracking tools as part of go.mod,
and the ability to run those using "go tool" instead of "go run", and so
this commit switches us over to using that strategy for everything we were
previously managing in tools.go.

There are some intentional exceptions here:

- The protobuf-compile script can't use "go tool" or "go run" because the
  tools in question are run only indirectly through protoc. However, we
  do still use the "tool" directive in go.mod to tell the Go toolchain that
  we depend on those tools, so that it'll track which versions we are
  currently using as part of go.mod.
- Our golangci-lint Makefile target uses "go run" to run a specific
  version of golangci-lint. We _intentionally_ don't consider that tool
  to be a direct dependency of OpenTofu because it has a lot of indirect
  dependencies that would pollute our go.mod file. Therefore that continues
  to use "go run" after this commit.
- Both of our tools.go files previously referred to
  github.com/nishanths/exhaustive , but nothing actually appears to be
  using that tool in the current OpenTofu tree, so it's no longer a
  dependency after this commit.

All of the dependencies we have _only_ for tools are now classified as
"indirect" in the go.mod file. This is the default behavior of the Go
toolchain and appears to be motivated by making it clearer that these
modules do not contribute anything to the runtime behavior of OpenTofu.
This also corrected a historical oddity in our go.mod where for some reason
the "indirect" dependencies had been split across two different "require"
directives; they are now all grouped together in a single directive.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-10-10 07:06:56 -03:00

85 lines
3.4 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tofu
//go:generate go tool golang.org/x/tools/cmd/stringer -type=DeprecationWarningLevel deprecation_level.go
import (
"log"
"github.com/opentofu/opentofu/internal/lang/marks"
"github.com/opentofu/opentofu/internal/tfdiags"
)
// DeprecationWarningLevel defines different levels of deprecation warnings that can be used by the user to control
// what type of warnings it wants to see
type DeprecationWarningLevel uint8
const (
// DeprecationWarningLevelAll shows all deprecation warnings for outputs and variables, where no filtering is applied.
DeprecationWarningLevelAll DeprecationWarningLevel = iota
// DeprecationWarningLevelLocal shows only the deprecation warnings for the outputs and variables that are coming from
// modules that are referenced with a relative path (aka local module).
DeprecationWarningLevelLocal
// DeprecationWarningLevelNone disables any deprecation warnings, filtering out any diagnostic that was generated about
// this.
DeprecationWarningLevelNone
)
// DeprecationDiagnosticAllowed is used to determine if a diagnostic generated by a deprecation check against an
// output or a variable should be allowed to be shown to the user based on the given DeprecationWarningLevel.
// This is making use of tfdiags.Diagnostic#ExtraInfo() to figure out if the diagnostic is generated by a deprecation check.
func DeprecationDiagnosticAllowed(lvl DeprecationWarningLevel, diagnostic tfdiags.Diagnostic) bool {
if lvl == DeprecationWarningLevelAll {
return true
}
outputCause, outputOk := marks.DiagnosticOutputDeprecationCause(diagnostic)
variableCause, variableOk := DiagnosticVariableDeprecationCause(diagnostic)
switch lvl {
case DeprecationWarningLevelLocal:
switch {
case !outputOk && !variableOk:
// If it's not a deprecation warning diagnostic, always allow it to not filter out diagnostics unrelated with deprecation
return true
case outputOk && outputCause.IsFromRemoteModule: // do not allow deprecation warnings for outputs from remote module calls
return false
case variableOk && variableCause.IsFromRemoteModule: // do not allow deprecation warnings for variables from remote module calls
return false
}
return true
case DeprecationWarningLevelNone:
if outputOk || variableOk {
return false
}
return true
default:
return true
}
}
// ParseDeprecatedWarningLevel gets in a string and returns a DeprecationWarningLevel.
// Since these warnings are not critical to the system, this method is returning no error when the
// warn level identifier is missing a mapping. Instead, it falls back on returning the level that
// will write all the deprecation warnings.
func ParseDeprecatedWarningLevel(s string) DeprecationWarningLevel {
switch s {
// Adding also the empty string just to make it clear that empty string will result in DeprecationWarningLevelAll. Useful also for skipping the warn log in the default branch.
case "all", "":
return DeprecationWarningLevelAll
case "local":
return DeprecationWarningLevelLocal
case "none":
return DeprecationWarningLevelNone
default:
log.Printf(
"[WARN] ParseDeprecatedWarningLevel: returning %s deprecation warn level since the given value is unknown: %s",
DeprecationWarningLevelAll,
s,
)
return DeprecationWarningLevelAll
}
}