Skip to content

feat: Auto-create package directory in kpt pkg init#4405

Merged
liamfallon merged 2 commits intokptdev:mainfrom
SurbhiAgarwal1:fix/pkg-init-auto-create
Feb 25, 2026
Merged

feat: Auto-create package directory in kpt pkg init#4405
liamfallon merged 2 commits intokptdev:mainfrom
SurbhiAgarwal1:fix/pkg-init-auto-create

Conversation

@SurbhiAgarwal1
Copy link
Contributor

@SurbhiAgarwal1 SurbhiAgarwal1 commented Feb 20, 2026

Description

This PR enables kpt pkg init to automatically create the package directory if it doesn't exist, eliminating the need for users to manually create directories before initializing packages.

Motivation

Currently, users must create a directory before running kpt pkg init, which adds an unnecessary manual step to the workflow. This change streamlines the package initialization process by automatically creating the directory when needed.

Current workflow:

mkdir my-package
kpt pkg init my-package

New workflow:

kpt pkg init my-package  # Directory created automatically

Changes

  • Modified pkg/kptpkg/init.go to use MkdirAll() instead of returning an error when directory doesn't exist
  • Updated commands/pkg/init/cmdinit_test.go to verify auto-creation behavior
  • Added user-friendly message when creating directories

Features

  • Supports nested directory paths: kpt pkg init path/to/package
  • Backward compatible: existing directories work without changes
  • Idempotent: safe to run multiple times

Testing

All tests pass including:

  • Auto-creation of new directories
  • Existing directory handling
  • Nested path creation
  • Current directory initialization

Fixes #1835

- Automatically create package directory if it doesn't exist
- Update test to verify auto-creation behavior
- Add user-friendly message when creating directory

Fixes kptdev#1835

Signed-off-by: Surbhi <agarwalsurbhi1807@gmail.com>
Copilot AI review requested due to automatic review settings February 20, 2026 05:14
@netlify
Copy link

netlify bot commented Feb 20, 2026

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit 40c589e
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/6997fe50873f790008a59b55
😎 Deploy Preview https://deploy-preview-4405--kptdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 20, 2026
@dosubot dosubot bot added the area/pkg label Feb 20, 2026
@dosubot
Copy link
Contributor

dosubot bot commented Feb 20, 2026

Documentation Updates

7 document(s) were updated by changes in this PR:

_index
View Changes
@@ -188,7 +188,7 @@
 
 ![img](/images/lifecycle/flow5.svg)
 
-- **Create**: Initialize a directory using `kpt pkg init`.
+- **Create**: Initialize a package using `kpt pkg init`. The command creates the directory if it doesn't exist.
 
 Now, let's say you have rendered the package, and want to deploy it to a cluster. The workflow
 may look like this:
_index
View Changes
@@ -34,9 +34,8 @@
 Create a new kpt package:
 
 ```shell
-mkdir my-pkg
+kpt pkg init my-pkg
 cd my-pkg
-kpt pkg init
 ```
 
 Configure a local `ConfigMap`:
_index
View Changes
@@ -110,9 +110,8 @@
 Create a new kpt package:
 
 ```shell
-mkdir my-pkg
+kpt pkg init my-pkg
 cd my-pkg
-kpt pkg init
 ```
 
 Configure two pods, with one that depends on the other:
_index
View Changes
@@ -281,8 +281,8 @@
 This section highlights practices that commonly lead to CI failures, drift, or unintended cluster changes. Avoid the
 following:
 
-- Running `kpt pkg init` in CI. Packages and `Kptfile` metadata should be authored by developers, not created during
-  CI runs.
+- Running `kpt pkg init` in CI to create package metadata. The `Kptfile` and package structure should be authored by developers
+  and committed to Git, not generated during CI runs.
 - Mutating packages in CI. CI should validate and render the declared intent, not change the source of truth.
 - Storing secrets in configuration. Secrets must not appear in YAML files, the `Kptfile`, or `functionConfig`.
 - Applying on pull requests. PRs should validate only; deployment belongs in gated, mainline workflows.
_index
View Changes
@@ -314,13 +314,13 @@
 ```
 ## Creating a package
 
-Creating a new package is simple. Use the `pkg init` command to create a package directory with a minimal `Kptfile` and `README` files:
+Creating a new package is simple. Use the `pkg init` command to initialize a directory as a kpt package with a minimal `Kptfile` and `README` files:
 
 ```shell
 kpt pkg init awesomeapp
 ```
 
-This will create the `awesomeapp` directory if it doesn't exist, and initialize it with the necessary files.
+The command will automatically create the `awesomeapp` directory if it doesn't exist, eliminating the need to manually create the directory beforehand.
 
 Refer to the [init command reference](../../reference/cli/pkg/init/) for usage.
 
_index
View Changes
@@ -35,9 +35,8 @@
 Create a new kpt package:
 
 ```shell
-mkdir my-pkg
+kpt pkg init my-pkg
 cd my-pkg
-kpt pkg init
 ```
 
 Configure two pods, with one that depends on the other:
namespace-provisioning-cli
View Changes
@@ -117,8 +117,9 @@
 # You should be under the `./blueprint` git directory. If not, check the above
 # section  "Prerequisites | Repositories"
 
-# initialize the package (directory will be created automatically)
+# initialize the package (directory will be created automatically if it doesn't exist)
 $ kpt pkg init basens --description "kpt package for provisioning namespace"
+creating package directory basens
 writing basens/Kptfile
 writing basens/README.md
 writing basens/package-context.yaml

How did I do? Any feedback?  Join Discord

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Update description to reflect that pkg init now creates
the directory automatically if it doesn't exist.

Related to kptdev#1835

Signed-off-by: Surbhi <agarwalsurbhi1807@gmail.com>
@dosubot dosubot bot added the lgtm label Feb 20, 2026
@SurbhiAgarwal1
Copy link
Contributor Author

SurbhiAgarwal1 commented Feb 21, 2026

Hi @kispaljr, @liamfallon, and @efiacor!

This PR is ready for review and has:

  • All checks passing (3 neutral, 12 successful checks)
  • No conflicts with base branch

The PR implements automatic package directory creation in kpt pkg init to improve user experience. Would appreciate your review when you have a moment!

Thank you!

@liamfallon
Copy link
Contributor

liamfallon commented Feb 23, 2026

Related Documentation

1 document(s) may need updating based on files changed in this PR:

kpt

migration /kpt/blob/main/documentation/content/en/installation/migration.md

View Suggested Changes

@@ -68,7 +68,7 @@
 | [v0.39 Commands]                                                                                  | [v1.0 Commands]                                                                                                                                                                                                                                      |
 | ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
 | `kpt pkg get REPO_URI[.git]/PKG_PATH[@VERSION] LOCAL_DEST_DIRECTORY [flags]`                      | `kpt pkg get REPO_URI[.git]/PKG_PATH[@VERSION] [flags] [LOCAL_DEST_DIRECTORY]` <br> Fetch a remote package from a git subdirectory and writes it to a new local directory.                                                                           |
-| `kpt pkg init DIR [flags]`                                                                        | `kpt pkg init [DIR] [flags]` <br> Initializes an existing empty directory as a kpt package by adding a Kptfile.                                                                                                                                      |
+| `kpt pkg init DIR [flags]`                                                                        | `kpt pkg init [DIR] [flags]` <br> Initializes a directory as a kpt package by adding a Kptfile. Creates the directory if it does not exist.                                                                                                                                      |
 | `kpt pkg update LOCAL_PKG_DIR[@VERSION] [flags]`                                                  | `kpt pkg update [PKG_PATH][@version] [flags]` <br> Pulls in upstream changes and merges them into a local package.                                                                                                                                   |
 | `kpt pkg fix DIR [flags]`                                                                         | `kpt fn eval --image ghcr.io/kptdev/krm-functions-catalog/fix:latest --include-meta-resources` <br> Fix a local package which is using deprecated features.                                                                                                                                            |
 | `kpt pkg desc DIR [flags]`                                                                        | Deprecated in favor of reading Kptfile directly                                                                                                                                                                                                      |

[Accept] [Decline]

Note: You must be authenticated to accept/decline updates.

How did I do? Any feedback? Join Discord

I think we should update the message as suggested in the dosubot comment above.

@SurbhiAgarwal1
Copy link
Contributor Author

Hello @liamfallon , I had updated the changes that you had suggested , is their any other changes ?

@liamfallon
Copy link
Contributor

Hello @liamfallon , I had updated the changes that you had suggested , is their any other changes ?

Hi @SurbhiAgarwal1 , It's all good now. Thanks!

@liamfallon liamfallon merged commit 122789d into kptdev:main Feb 25, 2026
15 checks passed
dosubot bot added a commit that referenced this pull request Feb 25, 2026
@dosubot dosubot bot mentioned this pull request Feb 25, 2026
efiacor added a commit that referenced this pull request Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/pkg lgtm size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kpt pkg init should create package dir if it doesn't exist

5 participants