Skip to content

Commit da6cbcf

Browse files
fix: support installs without yarn.lock (#836)
Co-authored-by: Crystal Luc-Magloire <crystal.magloire@datadoghq.com>
1 parent dc39117 commit da6cbcf

3 files changed

Lines changed: 79 additions & 9 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ jobs:
3333
- name: Install dependencies
3434
run: ./scripts/install_deps.sh
3535

36+
- name: Test dependency installer without a lockfile
37+
run: ./scripts/test_install_deps.sh
38+
3639
- name: Check formatting
3740
run: yarn check-formatting
3841

‎scripts/install_deps.sh‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,30 @@ yarn_args=("--ignore-engines")
4747

4848
if [ -n "$dd_trace_override" ]; then
4949
echo "Node ${TARGET_NODE_MAJOR} is not supported by dd-trace v6, pinning dd-trace to ${dd_trace_override}"
50-
# Rewrite the manifest against temporary backups and restore both tracked
51-
# files on exit, however the install ends. node_modules keeps the resolved
52-
# v5 line (which is what update_dist_version.sh reads), but leaving
53-
# package.json/yarn.lock modified would make the next v6 build on this
54-
# tree silently install the wrong tracer line against a dirty lockfile.
50+
# Rewrite the manifest against a temporary backup and preserve the lockfile
51+
# when the input tree contains one. Restore that initial state on exit,
52+
# however the install ends. node_modules keeps the resolved v5 line (which
53+
# is what update_dist_version.sh reads), but leaving package.json or a
54+
# generated v5 lockfile behind could affect the next build from this tree.
5555
package_backup=$(mktemp)
56-
lock_backup=$(mktemp)
56+
lock_backup=""
5757
cp package.json "$package_backup"
58-
cp yarn.lock "$lock_backup"
58+
if [ -f yarn.lock ]; then
59+
lock_backup=$(mktemp)
60+
cp yarn.lock "$lock_backup"
61+
fi
5962
restore_manifests() {
6063
cp "$package_backup" package.json
61-
cp "$lock_backup" yarn.lock
62-
rm -f "$package_backup" "$lock_backup"
64+
if [ -n "$lock_backup" ]; then
65+
cp "$lock_backup" yarn.lock
66+
rm -f "$lock_backup"
67+
else
68+
# Reduced build contexts may omit the repository lockfile. Remove
69+
# the v5 lockfile generated by Yarn so the temporary pin still
70+
# leaves the input tree unchanged.
71+
rm -f yarn.lock
72+
fi
73+
rm -f "$package_backup"
6374
}
6475
trap restore_manifests EXIT
6576
node ./scripts/set_ddtrace_version.js "$(cat package.json)" "$dd_trace_override" > package-new.json

‎scripts/test_install_deps.sh‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# Regression test for install_deps.sh in reduced build contexts that do not
4+
# include the repository's yarn.lock.
5+
6+
set -euo pipefail
7+
8+
scripts_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
9+
repo_dir=$(dirname "$scripts_dir")
10+
test_repo=$(mktemp -d)
11+
12+
cleanup() {
13+
rm -rf "$test_repo"
14+
}
15+
trap cleanup EXIT
16+
17+
mkdir -p "$test_repo/scripts" "$test_repo/bin"
18+
cp "$repo_dir/package.json" "$test_repo/package.json"
19+
cp "$scripts_dir/dd_trace_versions.sh" "$test_repo/scripts/dd_trace_versions.sh"
20+
cp "$scripts_dir/install_deps.sh" "$test_repo/scripts/install_deps.sh"
21+
cp "$scripts_dir/set_ddtrace_version.js" "$test_repo/scripts/set_ddtrace_version.js"
22+
23+
# Avoid a network install while preserving Yarn's relevant behavior: verify
24+
# the temporary v5 manifest and create the lockfile that a real install would.
25+
cat > "$test_repo/bin/yarn" <<'EOF'
26+
#!/bin/bash
27+
set -euo pipefail
28+
29+
case " $* " in
30+
*" --ignore-engines "*) ;;
31+
*) echo "install did not pass --ignore-engines" >&2; exit 1 ;;
32+
esac
33+
case " $* " in
34+
*" --frozen-lockfile "*) echo "v5 install unexpectedly froze the lockfile" >&2; exit 1 ;;
35+
esac
36+
37+
expected=$(sed -n 's/^DD_TRACE_V5_VERSION="\([^"]*\)"/\1/p' scripts/dd_trace_versions.sh)
38+
actual=$(node -p "require('./package.json').devDependencies['dd-trace']")
39+
if [ "$actual" != "$expected" ]; then
40+
echo "expected temporary dd-trace pin $expected, found $actual" >&2
41+
exit 1
42+
fi
43+
44+
touch yarn.lock
45+
EOF
46+
chmod +x "$test_repo/bin/yarn"
47+
48+
PATH="$test_repo/bin:$PATH" TARGET_NODE_MAJOR=20 "$test_repo/scripts/install_deps.sh"
49+
50+
cmp "$repo_dir/package.json" "$test_repo/package.json"
51+
if [ -e "$test_repo/yarn.lock" ]; then
52+
echo "install_deps.sh left a generated yarn.lock in a lockfile-free context" >&2
53+
exit 1
54+
fi
55+
56+
echo "install_deps.sh supports a build context without yarn.lock"

0 commit comments

Comments
 (0)