From 51e7e5c44674068e0509798da0a852b4e2397dc6 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Wed, 22 Jun 2022 10:22:34 +0100
Subject: [PATCH 1/5] improve develop.sh

---
 scripts/develop.sh | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/scripts/develop.sh b/scripts/develop.sh
index 3b4cc5c0d5376..fb0712b0d41c8 100755
--- a/scripts/develop.sh
+++ b/scripts/develop.sh
@@ -5,6 +5,13 @@ set -x
 
 SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
 PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel)
+set +u
+CODER_DEV_ADMIN_PASSWORD="${CODER_DEV_ADMIN_PASSWORD:-password}"
+set -u
+
+# Preflight check: make sure nothing is listening on port 3000 or 8080
+nc -z 127.0.0.1 3000 && echo '== ERROR: something is listening on port 3000. Kill it and re-run this script.' && exit 1
+nc -z 127.0.0.1 8080 && echo '== ERROR: something is listening on port 8080. Kill it and re-run this script.' && exit 1
 
 echo '== Run "make build" before running this command to build binaries.'
 echo '== Without these binaries, workspaces will fail to start!'
@@ -17,18 +24,19 @@ echo '== Without these binaries, workspaces will fail to start!'
 # https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
 (
 	cd "${PROJECT_ROOT}"
-
-	trap 'kill 0' SIGINT
+	# Send an interrupt signal to all processes in this subshell on exit
 	CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev &
 	go run -tags embed cmd/coder/main.go server --in-memory --tunnel &
 
-	# Just a minor sleep to ensure the first user was created to make the member.
-	sleep 2
+	echo '== Waiting for Coder to become ready'
+	timeout 60s bash -c 'until nc -z 127.0.0.1 3000 > /dev/null 2>&1; do sleep 1; done'
 
 	#  create the first user, the admin
-	go run cmd/coder/main.go login http://127.0.0.1:3000 --username=admin --email=admin@coder.com --password=password || true
+	go run cmd/coder/main.go login http://127.0.0.1:3000 --username=admin --email=admin@coder.com --password=password \
+		|| echo 'Failed to create admin user. To troubleshoot, try running this command manually.'
 
-	# || yes to always exit code 0. If this fails, whelp.
-	go run cmd/coder/main.go users create --email=member@coder.com --username=member --password="${CODER_DEV_ADMIN_PASSWORD}" || true
+	# || true to always exit code 0. If this fails, whelp.
+	go run cmd/coder/main.go users create --email=member@coder.com --username=member --password="${CODER_DEV_ADMIN_PASSWORD}" \
+		|| echo 'Failed to create regular user. To troubleshoot, try running this command manually.'
 	wait
 )

From a6dcabfafdd5eecb871b4055df84624a31aba9da Mon Sep 17 00:00:00 2001
From: johnstcn <public@cianjohnston.ie>
Date: Wed, 22 Jun 2022 09:55:14 +0000
Subject: [PATCH 2/5] deprecate makefile target, add optional verbose output,
 check for dependencies

---
 Makefile           | 3 ++-
 scripts/develop.sh | 7 +++++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 56fc493d358a7..51e1b6a707371 100644
--- a/Makefile
+++ b/Makefile
@@ -60,8 +60,9 @@ coderd/database/dump.sql: $(wildcard coderd/database/migrations/*.sql)
 coderd/database/querier.go: coderd/database/dump.sql $(wildcard coderd/database/queries/*.sql)
 	coderd/database/generate.sh
 
+# This target is deprecated, as GNU make has issues passing signals to subprocesses.
 dev:
-	./scripts/develop.sh
+	@echo Please run ./scripts/develop.sh manually.
 .PHONY: dev
 
 fmt/prettier:
diff --git a/scripts/develop.sh b/scripts/develop.sh
index fb0712b0d41c8..1bfca5a382972 100755
--- a/scripts/develop.sh
+++ b/scripts/develop.sh
@@ -1,15 +1,18 @@
 #!/usr/bin/env bash
 
+# Allow toggling verbose output
+[[ ! -z ${VERBOSE:-""} ]] && set -x
 set -euo pipefail
-set -x
 
 SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
+source "${SCRIPT_DIR}/lib.sh"
 PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel)
 set +u
 CODER_DEV_ADMIN_PASSWORD="${CODER_DEV_ADMIN_PASSWORD:-password}"
 set -u
 
-# Preflight check: make sure nothing is listening on port 3000 or 8080
+# Preflight checks: ensure we have our required dependencies, and make sure nothing is listening on port 3000 or 8080
+dependencies git make nc go yarn
 nc -z 127.0.0.1 3000 && echo '== ERROR: something is listening on port 3000. Kill it and re-run this script.' && exit 1
 nc -z 127.0.0.1 8080 && echo '== ERROR: something is listening on port 8080. Kill it and re-run this script.' && exit 1
 

From c1c4404939579b88b2e2ae3e784eb7ffb1952691 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Wed, 22 Jun 2022 10:59:44 +0100
Subject: [PATCH 3/5] Address SC2236

---
 scripts/develop.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/develop.sh b/scripts/develop.sh
index 1bfca5a382972..61634d8fa346a 100755
--- a/scripts/develop.sh
+++ b/scripts/develop.sh
@@ -1,7 +1,7 @@
 #!/usr/bin/env bash
 
 # Allow toggling verbose output
-[[ ! -z ${VERBOSE:-""} ]] && set -x
+[[ -n ${VERBOSE:-""} ]] && set -x
 set -euo pipefail
 
 SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")

From 52b2589a053c36d5238f2b9fbdebd5b88d66a4c8 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Wed, 22 Jun 2022 11:46:25 +0100
Subject: [PATCH 4/5] add suggestion from mafredri

---
 scripts/develop.sh | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/scripts/develop.sh b/scripts/develop.sh
index 61634d8fa346a..ece67cb7391c4 100755
--- a/scripts/develop.sh
+++ b/scripts/develop.sh
@@ -5,6 +5,7 @@
 set -euo pipefail
 
 SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
+# shellcheck disable=SC1091
 source "${SCRIPT_DIR}/lib.sh"
 PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel)
 set +u
@@ -12,9 +13,9 @@ CODER_DEV_ADMIN_PASSWORD="${CODER_DEV_ADMIN_PASSWORD:-password}"
 set -u
 
 # Preflight checks: ensure we have our required dependencies, and make sure nothing is listening on port 3000 or 8080
-dependencies git make nc go yarn
-nc -z 127.0.0.1 3000 && echo '== ERROR: something is listening on port 3000. Kill it and re-run this script.' && exit 1
-nc -z 127.0.0.1 8080 && echo '== ERROR: something is listening on port 8080. Kill it and re-run this script.' && exit 1
+dependencies curl git go make nc yarn
+nc -z localhost 3000 && echo '== ERROR: something is listening on port 3000. Kill it and re-run this script.' && exit 1
+nc -z localhost 8080 && echo '== ERROR: something is listening on port 8080. Kill it and re-run this script.' && exit 1
 
 echo '== Run "make build" before running this command to build binaries.'
 echo '== Without these binaries, workspaces will fail to start!'
@@ -26,20 +27,20 @@ echo '== Without these binaries, workspaces will fail to start!'
 # to kill both at the same time. For more details, see:
 # https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
 (
+	SCRIPT_PID=$$
 	cd "${PROJECT_ROOT}"
-	# Send an interrupt signal to all processes in this subshell on exit
-	CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev &
-	go run -tags embed cmd/coder/main.go server --in-memory --tunnel &
+	CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT ${SCRIPT_PID} &
+	go run -tags embed cmd/coder/main.go server --in-memory --tunnel || kill -INT ${SCRIPT_PID} &
 
 	echo '== Waiting for Coder to become ready'
-	timeout 60s bash -c 'until nc -z 127.0.0.1 3000 > /dev/null 2>&1; do sleep 1; done'
+	timeout 60s bash -c 'until curl -s --fail http://localhost:3000 > /dev/null 2>&1; do sleep 0.5; done'
 
 	#  create the first user, the admin
-	go run cmd/coder/main.go login http://127.0.0.1:3000 --username=admin --email=admin@coder.com --password=password \
-		|| echo 'Failed to create admin user. To troubleshoot, try running this command manually.'
+	go run cmd/coder/main.go login http://127.0.0.1:3000 --username=admin --email=admin@coder.com --password="${CODER_DEV_ADMIN_PASSWORD}" ||
+		echo 'Failed to create admin user. To troubleshoot, try running this command manually.'
 
 	# || true to always exit code 0. If this fails, whelp.
-	go run cmd/coder/main.go users create --email=member@coder.com --username=member --password="${CODER_DEV_ADMIN_PASSWORD}" \
-		|| echo 'Failed to create regular user. To troubleshoot, try running this command manually.'
+	go run cmd/coder/main.go users create --email=member@coder.com --username=member --password="${CODER_DEV_ADMIN_PASSWORD}" ||
+		echo 'Failed to create regular user. To troubleshoot, try running this command manually.'
 	wait
 )

From 030558702d637c7079006e4972b57669700b4928 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Wed, 22 Jun 2022 12:07:34 +0100
Subject: [PATCH 5/5] fix process group kill invocation

---
 scripts/develop.sh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/scripts/develop.sh b/scripts/develop.sh
index ece67cb7391c4..b784cf2ae7634 100755
--- a/scripts/develop.sh
+++ b/scripts/develop.sh
@@ -14,8 +14,8 @@ set -u
 
 # Preflight checks: ensure we have our required dependencies, and make sure nothing is listening on port 3000 or 8080
 dependencies curl git go make nc yarn
-nc -z localhost 3000 && echo '== ERROR: something is listening on port 3000. Kill it and re-run this script.' && exit 1
-nc -z localhost 8080 && echo '== ERROR: something is listening on port 8080. Kill it and re-run this script.' && exit 1
+nc -z 127.0.0.1 3000 >/dev/null 2>&1 && echo '== ERROR: something is listening on port 3000. Kill it and re-run this script.' && exit 1
+nc -z 127.0.0.1 8080 >/dev/null 2>&1 && echo '== ERROR: something is listening on port 8080. Kill it and re-run this script.' && exit 1
 
 echo '== Run "make build" before running this command to build binaries.'
 echo '== Without these binaries, workspaces will fail to start!'
@@ -29,8 +29,8 @@ echo '== Without these binaries, workspaces will fail to start!'
 (
 	SCRIPT_PID=$$
 	cd "${PROJECT_ROOT}"
-	CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT ${SCRIPT_PID} &
-	go run -tags embed cmd/coder/main.go server --in-memory --tunnel || kill -INT ${SCRIPT_PID} &
+	CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT -${SCRIPT_PID} &
+	go run -tags embed cmd/coder/main.go server --address 127.0.0.1:3000 --in-memory --tunnel || kill -INT -${SCRIPT_PID} &
 
 	echo '== Waiting for Coder to become ready'
 	timeout 60s bash -c 'until curl -s --fail http://localhost:3000 > /dev/null 2>&1; do sleep 0.5; done'