Skip to content

Commit f74ffa9

Browse files
committed
feat: improve warning message when Composer is not found
1 parent 1309e83 commit f74ffa9

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

local/php/composer.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,13 @@ func Composer(dir string, args, env []string, stdout, stderr, logger io.Writer,
7474
composerBin = "composer2"
7575
}
7676
path, err := e.findComposer(composerBin)
77-
if err != nil || !isPHPScript(path) {
78-
fmt.Fprintln(logger, " WARNING: Unable to find Composer, downloading one. It is recommended to install Composer yourself at https://getcomposer.org/download/")
77+
if pathIsPhpScript := isPHPScript(path); err != nil || !pathIsPhpScript {
78+
reason := "No Composer installation found."
79+
if path != "" && !pathIsPhpScript {
80+
reason = fmt.Sprintf("Detected Composer file (%s) is not a valid PHAR or PHP script.", path)
81+
}
82+
fmt.Fprintln(logger, " WARNING:", reason)
83+
fmt.Fprintln(logger, " Downloading Composer for you, but it is recommended to install Composer yourself, instructions available at https://getcomposer.org/download/")
7984
// we don't store it under bin/ to avoid it being found by findComposer as we want to only use it as a fallback
8085
binDir := filepath.Join(util.GetHomeDir(), "composer")
8186
if path, err = downloadComposer(binDir, debugLogger); err != nil {
@@ -100,6 +105,9 @@ func Composer(dir string, args, env []string, stdout, stderr, logger io.Writer,
100105

101106
// isPHPScript checks that the composer file is indeed a phar/PHP script (not a .bat file)
102107
func isPHPScript(path string) bool {
108+
if path == "" {
109+
return false
110+
}
103111
file, err := os.Open(path)
104112
if err != nil {
105113
return false
@@ -149,7 +157,7 @@ func findComposer(extraBin string, logger zerolog.Logger) (string, error) {
149157
if strings.HasSuffix(pharPath, ".bat") {
150158
pharPath = pharPath[:len(pharPath)-4] + ".phar"
151159
}
152-
logger.Debug().Str("source", "Composer").Msgf(`Found Composer as "%s"`, pharPath)
160+
logger.Debug().Str("source", "Composer").Msgf(`Found potential Composer as "%s"`, pharPath)
153161
return pharPath, nil
154162
}
155163
}

local/php/composer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (s *ComposerSuite) TestIsComposerPHPScript(c *C) {
3333
dir, err := filepath.Abs("testdata/php_scripts")
3434
c.Assert(err, IsNil)
3535

36+
c.Assert(isPHPScript(""), Equals, false)
3637
c.Assert(isPHPScript(filepath.Join(dir, "unknown")), Equals, false)
3738
c.Assert(isPHPScript(filepath.Join(dir, "invalid")), Equals, false)
3839

local/php/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (e *Executor) findComposer(extraBin string) (string, error) {
401401
}
402402
if m := d.Mode(); !m.IsDir() {
403403
// Yep!
404-
e.Logger.Debug().Str("source", "Composer").Msgf(`Found Composer as "%s"`, path)
404+
e.Logger.Debug().Str("source", "Composer").Msgf(`Found potential Composer as "%s"`, path)
405405
return path, nil
406406
}
407407
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy