microcks-cli icon indicating copy to clipboard operation
microcks-cli copied to clipboard

bug: import-dir exits 0 after partial artifact import failures

Open cotishq opened this issue 6 months ago • 3 comments

Describe the bug

microcks import-dir can report failed artifact imports but still complete with exit code 0.

The command already tracks failed imports through ImportResult.FailedCount, but after printing the final import summary it does not return a non-zero exit code when FailedCount > 0.

This is problematic for scripts and CI pipelines because a partial import failure is treated as a successful command execution.

Expected behavior

When one or more files fail to import, microcks import-dir should:

  • continue processing the remaining files
  • print the import summary
  • exit with a non-zero status code, for example 1

If all files are imported successfully, it should continue exiting with status code 0.

Actual behavior

When ImportDirectory completes with partial failures, the command prints the failed files and final summary, but then returns normally.

Because the Cobra command returns without calling os.Exit(1) or returning an error, the process exits with status code 0.

Image

How to Reproduce?

This can be observed from the current command flow in cmd/importDir.go.

Reproducer:

  1. Make ImportDirectory process multiple specification files.
  2. Have one UploadArtifact call fail while another succeeds.
  3. ImportDirectory returns a result with FailedCount > 0 and err == nil.
  4. NewImportDirCommand prints the summary but does not check result.FailedCount.
  5. The command returns normally, so the process exits with status code 0.

The existing unit test TestImportDirectory/partial_failure already demonstrates the partial-failure case at the business-logic level: one file succeeds, one file fails, and FailedCount is set to 1.

Resources to reproduce the behaviour:

  1. cmd/importDir.go
  2. cmd/importDir_test.go
  3. Existing test case: TestImportDirectory/partial_failure

Microcks version or git rev

Microcks-CLI 1.0.2

Install method (docker-compose, helm chart, operator, docker-desktop extension,...)

No response

Additional information

A possible fix is to check result.FailedCount after printing the summary:

if result.FailedCount > 0 {
    os.Exit(1)
}

cotishq avatar May 13 '26 08:05 cotishq

@Harsh4902 would love your opinion this, if this looks good to you , i can raise a pr for it

cotishq avatar May 13 '26 16:05 cotishq

Thanks @cotishq, good find. As the author of import-dir (#172), happy to defer the PR. Quick reproduction on HEAD confirms it, and the total-failure case also exits 0:

$ go build -o /tmp/microcks-cli-repro .
$ mkdir /tmp/repro-379 && cp samples/* /tmp/repro-379/   # any two valid specs
$ /tmp/microcks-cli-repro import-dir /tmp/repro-379 \
    --microcksURL=http://127.0.0.1:9 --keycloakClientId=x --keycloakClientSecret=x
✗ Failed: /tmp/repro-379/openapi.yaml - ... connection refused
✗ Failed: /tmp/repro-379/postman.json - ... connection refused
Import completed: 0/2 files imported successfully
$ echo $?
0

Three things worth folding into the fix:

  1. The same bug hits the ValidationError branch at cmd/importDir.go:164 (it returns instead of exiting non-zero), so a non-existent directory also exits 0. Worth fixing both in one PR since they're the same class of silent-success bug.

  2. Use Cobra's RunE returning an error instead of os.Exit(1) inside Run. Keeps exit-code handling idiomatic, lines up with #280, and makes the test cleaner since you can assert the returned error directly.

  3. The summary line Import completed: N/M files imported successfully reads as success on failure (literally "0/2 files imported successfully" above). Either prefix with Some imports failed: when FailedCount > 0, or split into two lines so CI logs don't rely on the exit code alone.

Happy to review when the PR is up. Pasting the reproduction commands and output screenshots in the PR description will speed that up.

Caesarsage avatar May 14 '26 04:05 Caesarsage

@Caesarsage you have done a pretty good job with that import-dir command back then and really thanks for the suggestions, will take into considerations all the points you have stated

cotishq avatar May 14 '26 14:05 cotishq