Skip to content

Commit 9dc6584

Browse files
committed
fix: use BUILDAH_PREPROCESS only to change default
explicitly disabling preprocessing requires using --preprocess=false kwarg
1 parent 1c57c20 commit 9dc6584

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

imagebuildah/build.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B
163163
}
164164

165165
// pre-process Dockerfiles with ".in" suffix, if --preprocess is specified, or if BUILDAH_PREPROCESS is set
166-
if strings.HasSuffix(dfile, ".in") || options.Preprocess {
166+
preprocessSpecified := options.Preprocess == types.OptionalBoolTrue
167+
preprocessInferred := (options.Preprocess == types.OptionalBoolUndefined) && strings.HasSuffix(dfile, ".in")
168+
if preprocessSpecified || preprocessInferred {
167169
pData, err := preprocessContainerfileContents(logger, dfile, data, options.ContextDirectory, options.CPPFlags)
168170
if err != nil {
169171
return "", nil, err

pkg/cli/build.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
363363
sbomScanOptions = append(sbomScanOptions, *sbomScanOption)
364364
}
365365

366-
var compatVolumes, createdAnnotation, inheritAnnotations, inheritLabels, skipUnusedStages types.OptionalBool
366+
var compatVolumes, createdAnnotation, inheritAnnotations, inheritLabels, skipUnusedStages, preprocess types.OptionalBool
367367
if c.Flag("compat-volumes").Changed {
368368
compatVolumes = types.NewOptionalBool(iopts.CompatVolumes)
369369
}
@@ -379,6 +379,9 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
379379
if c.Flag("skip-unused-stages").Changed {
380380
skipUnusedStages = types.NewOptionalBool(iopts.SkipUnusedStages)
381381
}
382+
if c.Flag("preprocess").Changed || DefaultPreprocess() {
383+
preprocess = types.NewOptionalBool(iopts.Preprocess)
384+
}
382385

383386
options = define.BuildOptions{
384387
AddCapabilities: iopts.CapAdd,
@@ -397,7 +400,7 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
397400
CompatVolumes: compatVolumes,
398401
ConfidentialWorkload: confidentialWorkloadOptions,
399402
CPPFlags: iopts.CPPFlags,
400-
Preprocess: iopts.Preprocess,
403+
Preprocess: preprocess,
401404
CommonBuildOpts: commonOpts,
402405
Compression: compression,
403406
ConfigureNetwork: networkPolicy,

pkg/cli/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
248248
fs.BoolVar(&flags.InheritLabels, "inherit-labels", true, "inherit the labels from the base image or base stages.")
249249
fs.BoolVar(&flags.InheritAnnotations, "inherit-annotations", true, "inherit the annotations from the base image or base stages.")
250250
fs.StringArrayVar(&flags.CPPFlags, "cpp-flag", []string{}, "set additional flag to pass to C preprocessor (cpp)")
251-
fs.BoolVar(&flags.Preprocess, "preprocess", PreprocessContainerfile(), "use the C preprocessor (cpp) on a Dockerfile, regardless of the file suffix")
251+
fs.BoolVar(&flags.Preprocess, "preprocess", DefaultPreprocess(), "use the C preprocessor (cpp) on a Dockerfile, regardless of the file suffix. Use BUILDAH_PREPROCESS environment variable to change default.")
252252
fs.BoolVar(&flags.CreatedAnnotation, "created-annotation", true, `set an "org.opencontainers.image.created" annotation in the image`)
253253
fs.StringVar(&flags.Creds, "creds", "", "use `[username[:password]]` for accessing the registry")
254254
fs.StringVarP(&flags.CWOptions, "cw", "", "", "confidential workload `options`")
@@ -538,9 +538,9 @@ func DefaultHistory() bool {
538538
return false
539539
}
540540

541-
// PreprocessContainerfile returns true if BUILDAH_PREPROCESS is set to "1" or "true"
541+
// DefaultPreprocess returns true if BUILDAH_PREPROCESS is set to "1" or "true"
542542
// otherwise it returns false
543-
func PreprocessContainerfile() bool {
543+
func DefaultPreprocess() bool {
544544
preprocess := os.Getenv("BUILDAH_PREPROCESS")
545545
if strings.ToLower(preprocess) == "true" || preprocess == "1" {
546546
return true

tests/bud.bats

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,13 @@ _EOF
41344134
expect_output --substring "Ignoring <stdin>:5:2: error: #error"
41354135
}
41364136

4137+
@test "bud with preprocessor-implied containerfile, with preprocess force disabled" {
4138+
_prefetch busybox
4139+
target=alpine-image
4140+
run_buildah 125 build $WITH_POLICY_JSON -t ${target} --preprocess=false -f Decomposed.in $BUDFILES/preprocess
4141+
expect_output --substring 'Build error: Unknown instruction: "RUNHELLO"'
4142+
}
4143+
41374144
@test "bud-with-rejected-name" {
41384145
target=ThisNameShouldBeRejected
41394146
run_buildah 125 build -q $WITH_POLICY_JSON -t ${target} $BUDFILES/from-scratch

0 commit comments

Comments
 (0)