Skip to content

Commit 545b330

Browse files
authored
Improve bin/detect error messages (#575)
1 parent 549a36a commit 545b330

10 files changed

Lines changed: 92 additions & 1 deletion

File tree

bin/detect

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,74 @@
11
#!/usr/bin/env bash
22
# bin/detect <build-dir>
33

4+
error() {
5+
local c="2,999 s/^/ ! /"
6+
# send all of our output to stderr
7+
exec 1>&2
8+
9+
echo -e "\033[1;31m" # bold; red
10+
echo -n " ! ERROR: "
11+
# this will be fed from stdin
12+
case $(uname) in
13+
Darwin) sed -l "$c";; # mac/bsd sed: -l buffers on line boundaries
14+
*) sed -u "$c";; # unix/gnu sed: -u unbuffered (arbitrary) chunks of data
15+
esac
16+
echo -e "\033[0m" # reset style
17+
exit 1
18+
}
19+
420
if [ -f $1/package.json ]; then
521
echo 'Node.js'
622
exit 0
723
fi
824

9-
>&2 echo 'Node.js: package.json not found in application root'
25+
if [[ -f "$1/.slugignore" ]] && grep -Fxq "package.json" "$1/.slugignore"; then
26+
error << EOF
27+
'package.json' listed in '.slugignore' file
28+
29+
The 'heroku/nodejs' buildpack is set on this application, but was
30+
unable to detect a 'package.json' file. This is likely because
31+
the '.slugignore' file is removing it before the build begins.
32+
33+
For more information, refer to the following documentation:
34+
https://devcenter.heroku.com/articles/slug-compiler#ignoring-files-with-slugignore
35+
EOF
36+
elif [[ -f "$1/.gitignore" ]] && grep -Fxq "package.json" "$1/.gitignore"; then
37+
error << EOF
38+
'package.json' listed in '.gitignore' file
39+
40+
The 'heroku/nodejs' buildpack is set on this application, but was
41+
unable to detect a 'package.json' file. This is likely because
42+
the '.gitignore' file is preventing it from being checked in to
43+
the git repo.
44+
45+
For more information, refer to the following documentation:
46+
https://devcenter.heroku.com/articles/gitignore
47+
EOF
48+
else
49+
error <<- EOF
50+
Application not supported by 'heroku/nodejs' buildpack
51+
52+
The 'heroku/nodejs' buildpack is set on this application, but was
53+
unable to detect a Node.js codebase.
54+
55+
A Node.js app on Heroku requires a 'package.json' at the root of
56+
the directory structure.
57+
58+
If you are trying to deploy a Node.js application, ensure that this
59+
file is present at the top level directory. This directory has the
60+
following files:
61+
62+
$(ls -1p $1)
63+
64+
If you are trying to deploy an application written in another
65+
language, you need to change the list of buildpacks set on your
66+
Heroku app using the 'heroku buildpacks' command.
67+
68+
For more information, refer to the following documentation:
69+
https://devcenter.heroku.com/articles/buildpacks
70+
https://devcenter.heroku.com/articles/nodejs-support#activation
71+
EOF
72+
fi
73+
1074
exit 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package.json

test/fixtures/node-project-missing-package-json/LICENSE

Whitespace-only changes.

test/fixtures/node-project-missing-package-json/README.md

Whitespace-only changes.

test/fixtures/node-project-missing-package-json/index.js

Whitespace-only changes.

test/fixtures/node-project-missing-package-json/src/CHANGELOG.md

Whitespace-only changes.

test/fixtures/node-project-missing-package-json/src/app.js

Whitespace-only changes.

test/fixtures/node-project-missing-package-json/src/webpack.config.js

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package.json

test/run

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,22 @@ testMemoryMetrics() {
990990
assertFileNotContains "measure#buildpack.nodejs.exec.heroku-postbuild.memory=" $metrics_log
991991
}
992992

993+
testBinDetectWarnings() {
994+
detect "slugignore-package-json"
995+
assertCapturedError "'package.json' listed in '.slugignore' file"
996+
assertCapturedError "https://devcenter.heroku.com/articles/slug-compiler#ignoring-files-with-slugignore"
997+
998+
detect "gitignore-package-json"
999+
assertCapturedError "'package.json' listed in '.gitignore' file"
1000+
assertCapturedError "https://devcenter.heroku.com/articles/gitignore"
1001+
1002+
detect "node-project-missing-package-json"
1003+
assertCapturedError "Application not supported by 'heroku/nodejs' buildpack"
1004+
assertCapturedError "https://devcenter.heroku.com/articles/nodejs-support#activation"
1005+
assertCapturedError "index.js"
1006+
assertCapturedError "src/"
1007+
}
1008+
9931009
# Utils
9941010

9951011
pushd "$(dirname 0)" >/dev/null
@@ -1018,6 +1034,15 @@ default_process_types_cleanup() {
10181034
fi
10191035
}
10201036

1037+
detect() {
1038+
default_process_types_cleanup
1039+
bp_dir=$(mktmpdir)
1040+
compile_dir=$(mktmpdir)
1041+
cp -a "$(pwd)"/* ${bp_dir}
1042+
cp -a ${bp_dir}/test/fixtures/$1/. ${compile_dir}
1043+
capture ${bp_dir}/bin/detect ${compile_dir}
1044+
}
1045+
10211046
compile() {
10221047
default_process_types_cleanup
10231048
bp_dir=$(mktmpdir)

0 commit comments

Comments
 (0)