Skip to content

Error when explicit destination of pkg get already exists - #4696

Open
gangadhar-res wants to merge 1 commit into
kptdev:mainfrom
gangadhar-res:fix/pkg-get-explicit-dest-exists
Open

Error when explicit destination of pkg get already exists#4696
gangadhar-res wants to merge 1 commit into
kptdev:mainfrom
gangadhar-res:fix/pkg-get-explicit-dest-exists

Conversation

@gangadhar-res

Copy link
Copy Markdown

Fixes #2656

Problem: running kpt pkg get REPO_URI DEST a second time silently fetched the package into DEST/ instead of erroring. getDest() always defaulted to a repo-named subdirectory when the destination existed, even when the user explicitly named it.

Fix: when the destination was explicitly provided, already exists, and is not ., return the same "destination directory already exists" error the defaulted path produces. The . special case and the defaulting behavior for omitted destinations are unchanged.

Validation: ran the reproduction steps from the issue against this branch — first run fetches into packages/newDirName, second run now errors with destination directory "packages/newDirName" already exists, and no nested nginx/ directory is created. kpt pkg get with no destination still defaults to ./nginx. Added a Test_getDest table test covering explicit/defaulted/existing/./file/missing-parent cases.

@gangadhar-res
gangadhar-res requested review from a team August 12, 2026 23:46
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 12, 2026
@netlify

netlify Bot commented Aug 12, 2026

Copy link
Copy Markdown

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit db01bbd
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/6a7d9e6362db550008a60961
😎 Deploy Preview https://deploy-preview-4696--kptdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@dosubot dosubot Bot added the bug Something isn't working label Aug 12, 2026
@aravindtga
aravindtga requested a balanced review from Copilot August 13, 2026 10:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes repeated kpt pkg get calls incorrectly creating nested packages.

Changes:

  • Rejects explicitly provided destinations that already exist.
  • Adds unit coverage for destination resolution scenarios.

Reviewed changes

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

File Description
pkg/lib/util/parse/parse.go Adds explicit-destination validation.
pkg/lib/util/parse/parse_test.go Tests destination handling and errors.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pkg/lib/util/parse/parse.go Outdated
Comment on lines +283 to +284
if explicitDest {
return "", errors.Errorf("destination directory %q already exists", v)
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Aug 13, 2026
A repeated `kpt pkg get REPO_URI DEST` silently fetched the package
into a nested subdirectory (DEST/<pkg-name>): getDest always defaulted
to a repo-named subdirectory when the destination existed, even when
the user explicitly named the directory that the first run created.

Reject an explicitly named destination only when it already contains a
Kptfile, which is exactly the repeated-fetch case. Existing directories
without a Kptfile keep working as containers to fetch into
(DEST/<pkg-name>), the '.' special case is unchanged, and omitted
destinations keep the defaulting behavior, so no existing tests change.

Fixes kptdev#2656

Signed-off-by: Gangadhar Chalapaka <gangadhar@resolve.ai>
@gangadhar-res
gangadhar-res force-pushed the fix/pkg-get-explicit-dest-exists branch from dd6159c to db01bbd Compare August 13, 2026 10:37
// defaulted destination does below. An existing directory without a
// Kptfile keeps working as a container to fetch the package into.
if explicitDest {
if _, err := os.Stat(filepath.Join(v, kptfilev1.KptFileName)); err == nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a specific reason for checking the Kptfile rather than erroring whenever the explicit destination already exists? This could be simpler to just error unconditionally if the directory exists.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@aravindtga
Good question. That was actually the first version of this PR: unconditionally erroring when the explicit destination exists. It failed CI because many of the existing command tests rely on fetching into an existing directory as a container, for example TestCmd_execute and most of the cmdget suite pass the existing workspace directory and expect the package to land at workspace/. Real scripts that mkdir a target and then run kpt pkg get URL target would break the same way. The Kptfile check was the narrowest rule I could find that still catches the repeated-fetch case from #2656: the second run always trips it because the first run wrote a Kptfile at that path, while container directories keep working. If you would rather error unconditionally and treat the container pattern as deprecated, I am happy to make that change and update the affected tests, but that felt like a bigger behavior decision than this bug fix should make on its own imo.

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

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kpt pkg get duplicates a package as a subdirectory when attempting to override the local directory name

3 participants