From 0a5cf9ce9176fafc547dcd7bad0021817b201db7 Mon Sep 17 00:00:00 2001 From: Hiroshi Hayakawa Date: Mon, 2 Sep 2024 12:44:44 +0900 Subject: [PATCH] Make the 'pack build' command warn that the positional argument will not be treated as the source directory path. Signed-off-by: Hiroshi Hayakawa --- internal/commands/build.go | 8 ++++++-- internal/commands/build_test.go | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/internal/commands/build.go b/internal/commands/build.go index c80123d77..ef2400876 100644 --- a/internal/commands/build.go +++ b/internal/commands/build.go @@ -8,14 +8,13 @@ import ( "strings" "time" - "github.com/buildpacks/pack/pkg/cache" - "github.com/google/go-containerregistry/pkg/name" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/buildpacks/pack/internal/config" "github.com/buildpacks/pack/internal/style" + "github.com/buildpacks/pack/pkg/cache" "github.com/buildpacks/pack/pkg/client" "github.com/buildpacks/pack/pkg/image" "github.com/buildpacks/pack/pkg/logging" @@ -328,6 +327,11 @@ func validateBuildFlags(flags *BuildFlags, cfg config.Config, inputImageRef clie return client.NewExperimentError("Exporting to OCI layout is currently experimental.") } + if !inputImageRef.Layout() && flags.AppPath == "" { + logger.Warnf("You are building an image named '%s'. If you mean it as an app directory path, run 'pack build --path %s'", + inputImageRef.Name(), inputImageRef.Name()) + } + return nil } diff --git a/internal/commands/build_test.go b/internal/commands/build_test.go index 4e4d03c69..06cf50a06 100644 --- a/internal/commands/build_test.go +++ b/internal/commands/build_test.go @@ -17,11 +17,10 @@ import ( "github.com/sclevine/spec/report" "github.com/spf13/cobra" - "github.com/buildpacks/pack/internal/paths" - "github.com/buildpacks/pack/internal/commands" "github.com/buildpacks/pack/internal/commands/testmocks" "github.com/buildpacks/pack/internal/config" + "github.com/buildpacks/pack/internal/paths" "github.com/buildpacks/pack/pkg/client" "github.com/buildpacks/pack/pkg/image" "github.com/buildpacks/pack/pkg/logging" @@ -77,6 +76,7 @@ func testBuildCommand(t *testing.T, when spec.G, it spec.S) { command.SetArgs([]string{"--builder", "my-builder", "image"}) h.AssertNil(t, command.Execute()) + h.AssertContains(t, outBuf.String(), "Warning: You are building an image named 'image'. If you mean it as an app directory path, run 'pack build --path image'") }) it("builds an image with a builder short command arg", func() { @@ -968,6 +968,7 @@ builder = "my-builder" command.SetArgs([]string{"oci:image", "--builder", "my-builder"}) err := command.Execute() h.AssertNil(t, err) + h.AssertNotContainsMatch(t, outBuf.String(), `Warning: You are building an image named '([^']+)'\. If you mean it as an app directory path, run 'pack build --path ([^']+)'`) }) }) @@ -982,6 +983,7 @@ builder = "my-builder" command.SetArgs([]string{"oci:image", "--previous-image", "oci:my-previous-image", "--builder", "my-builder"}) err := command.Execute() h.AssertNil(t, err) + h.AssertNotContainsMatch(t, outBuf.String(), `Warning: You are building an image named '([^']+)'\. If you mean it as an app directory path, run 'pack build --path ([^']+)'`) }) }) @@ -995,6 +997,7 @@ builder = "my-builder" command.SetArgs([]string{"oci:image", "--sparse", "--builder", "my-builder"}) err := command.Execute() h.AssertNil(t, err) + h.AssertNotContainsMatch(t, outBuf.String(), `Warning: You are building an image named '([^']+)'\. If you mean it as an app directory path, run 'pack build --path ([^']+)'`) }) }) })