mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
fix: fail fast when package manager is not installed (#360)
* fix: fail fast with clear error when package manager is not installed When PMG shims intercept a package manager that isn't installed (e.g. a clean JAMF-provisioned laptop where PMG is set up before dev tooling), real-binary resolution failed with a generic "unknown" error and a bug-report link, making it look like PMG itself had crashed. Introduce a typed BinaryNotFoundError that exits with code 127 (standard "command not found") and maps to a new PackageManagerNotFound error code with an actionable message instead of the Unknown classification. Co-authored-by: Cursor <cursoragent@cursor.com> * fix: improve missing package manager help message Use a concise, dynamic install hint instead of explaining PMG's PATH forwarding internals. Co-authored-by: Cursor <cursoragent@cursor.com> * fix: mention PATH in missing package manager help text Covers the common case where a package manager is installed but its bin directory is not on PATH yet (e.g. after curl | bash install). Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package shim
|
||||
|
||||
import "fmt"
|
||||
|
||||
const commandNotFoundExitCode = 127
|
||||
|
||||
// BinaryNotFoundError is returned when a package manager name resolves through
|
||||
// PMG shims but the real binary is absent from PATH (after shim dirs are stripped).
|
||||
type BinaryNotFoundError struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (e *BinaryNotFoundError) Error() string {
|
||||
return fmt.Sprintf("%s is not installed", e.Name)
|
||||
}
|
||||
|
||||
func (e *BinaryNotFoundError) ExitCode() int {
|
||||
return commandNotFoundExitCode
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package shim
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBinaryNotFoundError(t *testing.T) {
|
||||
err := &BinaryNotFoundError{Name: "bun"}
|
||||
|
||||
assert.Equal(t, "bun is not installed", err.Error())
|
||||
assert.Equal(t, commandNotFoundExitCode, err.ExitCode())
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package shim
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -79,6 +80,9 @@ func ResolveRealBinary(name string) (string, error) {
|
||||
|
||||
resolved, err := exec.LookPath(name)
|
||||
if err != nil {
|
||||
if errors.Is(err, exec.ErrNotFound) {
|
||||
return "", &BinaryNotFoundError{Name: name}
|
||||
}
|
||||
return "", fmt.Errorf("could not find %s in PATH (excluding pmg shims): %w", name, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -194,6 +194,9 @@ func TestResolveRealBinary(t *testing.T) {
|
||||
|
||||
if tc.wantErr {
|
||||
assert.Error(t, err)
|
||||
var notFound *BinaryNotFoundError
|
||||
assert.ErrorAs(t, err, ¬Found)
|
||||
assert.Equal(t, tc.binary, notFound.Name)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user