bug: import-dir exits 0 after partial artifact import failures
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.
How to Reproduce?
This can be observed from the current command flow in cmd/importDir.go.
Reproducer:
- Make
ImportDirectoryprocess multiple specification files. - Have one
UploadArtifactcall fail while another succeeds. ImportDirectoryreturns a result withFailedCount > 0anderr == nil.NewImportDirCommandprints the summary but does not checkresult.FailedCount.- 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:
cmd/importDir.gocmd/importDir_test.go- 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)
}
@Harsh4902 would love your opinion this, if this looks good to you , i can raise a pr for it
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:
-
The same bug hits the
ValidationErrorbranch atcmd/importDir.go:164(itreturns 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. -
Use Cobra's
RunEreturning an error instead ofos.Exit(1)insideRun. Keeps exit-code handling idiomatic, lines up with #280, and makes the test cleaner since you can assert the returned error directly. -
The summary line
Import completed: N/M files imported successfullyreads as success on failure (literally "0/2 files imported successfully" above). Either prefix withSome imports failed:whenFailedCount > 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 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