Skip to content

Fix #1407: Make 'kpt pkg get' use current directory when '.' is specified#4334

Open
ciaranjohnston wants to merge 7 commits intomainfrom
fix-issue-1407
Open

Fix #1407: Make 'kpt pkg get' use current directory when '.' is specified#4334
ciaranjohnston wants to merge 7 commits intomainfrom
fix-issue-1407

Conversation

@ciaranjohnston
Copy link
Member

When users explicitly pass '.' as the destination directory, kpt pkg get now places files directly in the current directory, matching the behavior of 'git clone' and 'kpt cfg set'.

Changes:

  • Modified parse.GitParseArgs() to track if destination was explicitly provided
  • Updated getDest() to use current directory directly when explicitly specified
  • Modified get.Run() to allow using existing empty directories
  • Preserved default behavior: when no destination is provided, still creates a subdirectory with the package name

Fixes #1407

@netlify
Copy link

netlify bot commented Dec 3, 2025

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit 9f98870
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/69b86bd97a763800088955ed
😎 Deploy Preview https://deploy-preview-4334--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.

Copilot AI review requested due to automatic review settings March 2, 2026 21:53
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 2, 2026
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.

Pull request overview

Adjusts kpt pkg get destination resolution so that explicitly specifying the current directory (. / paths that clean to .) places the package contents directly in that directory, aligning behavior with git clone and kpt cfg set.

Changes:

  • Added an explicitDest signal through argument parsing to distinguish defaulted destinations from user-provided ones.
  • Updated destination selection logic to treat explicit . as “use current directory”, while keeping the default “create package-name subdir” behavior when dest is omitted.
  • Updated get.Run() to allow using an existing empty destination directory.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pkg/lib/util/parse/parse.go Threads explicitDest through parsing and adjusts getDest() behavior for explicit current-directory destinations.
pkg/lib/util/parse/parse_test.go Updates parsing tests for the new GitParseArgs signature and destination expectations.
commands/pkg/get/cmdget.go Tracks whether destination was explicitly provided and passes it into parsing.
internal/util/get/get.go Permits existing empty destination directories (instead of erroring on any existing path).
commands/pkg/get/cmdget_test.go Updates get command tests to reflect the new explicit-. behavior and unchanged default behavior when dest is omitted.
commands/pkg/diff/cmddiff_test.go Updates diff tests to use the adjusted default pkg get destination behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +79 to +83
case err == nil:
// Destination exists - check if it's an empty directory
if !destInfo.IsDir() {
return errors.E(op, errors.Exist, types.UniquePath(c.Destination), fmt.Errorf("destination exists and is not a directory"))
}
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

Allowing an existing empty destination directory introduces a data-loss risk on failure: later errors (e.g., during WriteFile/fetchPackages) call cleanUpDirAndError, which unconditionally os.RemoveAll(c.Destination). If the destination pre-existed (especially when it’s the current directory), this can delete user-owned directories/content. Track whether the destination directory was created by this run and only RemoveAll when it was created; otherwise clean up only the files created by the command (or skip removal).

Copilot uses AI. Check for mistakes.
…fied

When users explicitly pass '.' as the destination directory, kpt pkg get
now places files directly in the current directory, matching the behavior
of 'git clone' and 'kpt cfg set'.

Changes:
- Modified parse.GitParseArgs() to track if destination was explicitly provided
- Updated getDest() to use current directory directly when explicitly specified
- Modified get.Run() to allow using existing empty directories
- Preserved default behavior: when no destination is provided, still creates
  a subdirectory with the package name

Fixes #1407

Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
…tions

- Changed getDest() to check cleaned path (v) instead of originalV for '.' comparison
- Updated tests to expect current directory when './' or '.' is explicitly passed
- Fixed TestCmdMainBranch_execute to not pass './' explicitly
- Fixed TestCmd_fail to use non-existent destination directory

Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
The test was explicitly passing './' as the destination, which with the
new behavior means 'use current directory directly'. Since the test runs
in a non-empty workspace directory, this caused a conflict.

Removing the explicit destination argument allows the default behavior
to create a subdirectory with the package name, which is what the test
expects.

Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
Copilot AI review requested due to automatic review settings March 15, 2026 23:25
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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
Copilot AI review requested due to automatic review settings March 15, 2026 23:51
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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
Copilot AI review requested due to automatic review settings March 16, 2026 20:45
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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

pkg/lib/util/parse/parse.go:263

  • os.Stat(v) errors other than IsNotExist aren’t handled. If Stat returns (nil, err) for e.g. permission/IO errors, the code will fall through and later call f.IsDir(), which will panic. Handle the non-nil err case explicitly (returning a wrapped/annotated error) before dereferencing f.
	f, err := os.Stat(v)
	if os.IsNotExist(err) {
		parent := filepath.Dir(v)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: kpt pkg get has inconsistent behavior when LOCAL_DEST_DIRECTORY is the current directory

4 participants