Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions bin/build-native.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

set -euo pipefail

driver_repo=$(pwd)
Comment thread
vuanhphung marked this conversation as resolved.
kernel_repo=${DATABRICKS_SQL_KERNEL_REPO:-../../databricks-sql-kernel}
napi_dir="${kernel_repo}/napi"
napi_major=$(
cargo metadata --format-version 1 --locked --manifest-path "${napi_dir}/Cargo.toml" |
node -e '
const metadata = JSON.parse(require("fs").readFileSync(0, "utf8"));
const rootId = metadata.resolve.root ?? metadata.workspace_members[0];
const root = metadata.resolve.nodes.find(({ id }) => id === rootId);
const napiId = root?.deps.find(({ name }) => name === "napi")?.pkg;
const version = metadata.packages.find(({ id }) => id === napiId)?.version;
process.stdout.write(version?.split(".")[0] ?? "");
'
)

# napi-rs v2 and v3 derive macros expect different CLI environment variables.
case "${napi_major}" in
2)
cli=(npx --yes @napi-rs/cli@2.18.4)
;;
3)
cli=(npx --yes --package @napi-rs/cli@3.8.2 napi)
;;
*)
echo "Unsupported napi-rs major version: ${napi_major:-unknown}" >&2
exit 1
;;
esac

build_profile=${BUILD_PROFILE---release}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — The default operator changed from :- to -. ${BUILD_PROFILE---release} only substitutes --release when BUILD_PROFILE is unset; the original inline command used ${BUILD_PROFILE:---release} which also substituted when the variable was set-but-empty.

Consequence: running with BUILD_PROFILE= (explicitly empty) now falls through to the else branch (napi build --platform with no profile), producing a debug build, whereas previously it produced a --release build. The common unset case (e.g. CI) is unaffected since both default to --release.

If empty-means-debug is intentional (the whitespace-stripping if guard suggests it is), this is fine — but the divergence from the previous :- semantics is worth confirming so an empty override doesn't silently ship a debug native binary.


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium — When BUILD_PROFILE= is set (the documented debug-build path), read -r -a build_profile <<< "" produces an empty array. The final expansion "${build_profile[@]}" is then evaluated under set -u (set -euo pipefail, line 3).

On bash 4.3 and earlier, expanding an empty array as "${arr[@]}" under set -u raises build_profile[@]: unbound variable and aborts the script (this was only fixed in bash 4.4). macOS still ships bash 3.2.57 as /usr/bin/bash, and the shebang is #!/usr/bin/env bash, so a developer on stock macOS bash running the documented BUILD_PROFILE= npm run build:native debug workflow will hit an abort before napi is ever invoked.

The release default path is unaffected (the array holds --release). Guarding the expansion makes the empty case safe on all bash versions.

cd "${napi_dir}"
if [[ -n "${build_profile//[[:space:]]/}" ]]; then
read -r -a build_profile_args <<< "${build_profile}"
"${cli[@]}" build --platform "${build_profile_args[@]}"
else
"${cli[@]}" build --platform
fi
cp index.* "${driver_repo}/native/kernel/"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test": "nyc --report-dir=${NYC_REPORT_DIR:-coverage_unit} mocha --config tests/unit/.mocharc.js",
"update-version": "node bin/update-version.js && prettier --write ./lib/version.ts",
"build": "npm run update-version && tsc --project tsconfig.build.json",
"build:native": "bash -c 'cd ${DATABRICKS_SQL_KERNEL_REPO:-../../databricks-sql-kernel}/napi && npx --yes @napi-rs/cli@2.18.4 build --platform ${BUILD_PROFILE:---release} && cp index.* $OLDPWD/native/kernel/'",
"build:native": "bash bin/build-native.sh",
"prepack": "test -f native/kernel/index.js || { echo 'ERROR: native/kernel/index.js (napi-rs router) is missing — the published tarball would fail to load kernel. It is committed to git; run `npm run build:native` if you removed it.' >&2; exit 1; }",
"watch": "tsc --project tsconfig.build.json --watch",
"type-check": "tsc --noEmit",
Expand Down
Loading