Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): run the SDK gates when invoked through a symlink
Yudhi Armyndharis04a40e0da219
diff --git a/scripts/check-contract-shapes.mjs b/scripts/check-contract-shapes.mjsindex a5fa1fe41..53efa4e6c 100644--- a/scripts/check-contract-shapes.mjs+++ b/scripts/check-contract-shapes.mjs@@ -45,8 +45,7 @@* under-describes reality, fix the backend DTO decorator, regenerate, and un-exclude).*/-import { readFileSync, readdirSync } from 'node:fs';-import { resolve } from 'node:path';+import { readFileSync, readdirSync, realpathSync } from 'node:fs';import { fileURLToPath } from 'node:url';// Resolve from the script's own location, not process.cwd() — same reason check-sdk-coverage.mjs@@ -1081,9 +1080,9 @@ export function parseJavaTypes(sources) {// Resolved-path comparison, not a basename match: splitting on `/` finds no separator in a Windows// path so the whole native path became the "basename" and never matched, and a bare `endsWith` on a-// basename would also fire for any other script sharing this file's name. Same comparison as-// check-sdk-docs.mjs and check-upstream-surface.mjs.-const isDirectRun = Boolean(process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url));+// basename would also fire for any other script sharing this file's name. argv[1] is realpathed+// because Node realpaths the main module's URL, so an unresolved path through a symlink never matched.+const isDirectRun = Boolean(process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url));if (isDirectRun) {const openapi = JSON.parse(readFileSync(`${REPO_ROOT}openapi.json`, 'utf8'));const schemas = openapi.components.schemas;diff --git a/scripts/check-contract-shapes.spec.mjs b/scripts/check-contract-shapes.spec.mjsindex e234c5b7e..acfeb84ce 100644--- a/scripts/check-contract-shapes.spec.mjs+++ b/scripts/check-contract-shapes.spec.mjs@@ -10,6 +10,11 @@import { test } from 'node:test';import assert from 'node:assert/strict';+import { spawnSync } from 'node:child_process';+import { mkdtempSync, rmSync, symlinkSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { fileURLToPath } from 'node:url';import {comparePair,handToken,@@ -312,3 +317,18 @@ test('parseJavaTypes: boxed numerics reduce to number rather than their class naconst schema = { type: 'object', required: ['a'], properties: { a: { type: 'string' } } };assert.equal(comparePair('Sample', { a: s.a }, 'Dto', schema, {}, false).length, 1);});++test('a run through a symlinked path still reports', () => {+ // Node realpaths the main module's URL but not argv[1], so a guard comparing the unresolved path+ // skipped every check and exited 0 whenever the invocation crossed a symlink (/tmp on macOS).+ const dir = mkdtempSync(join(tmpdir(), 'contract-shapes-'));+ try {+ symlinkSync(fileURLToPath(new URL('..', import.meta.url)), join(dir, 'repo'));+ const run = spawnSync(process.execPath, [join(dir, 'repo', 'scripts', 'check-contract-shapes.mjs')], {+ encoding: 'utf8',+ });+ assert.match(run.stdout + run.stderr, /pairs compared/);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});diff --git a/scripts/check-sdk-coverage.mjs b/scripts/check-sdk-coverage.mjsindex 777d2780a..df9a9cd93 100644--- a/scripts/check-sdk-coverage.mjs+++ b/scripts/check-sdk-coverage.mjs@@ -29,8 +29,8 @@** Run locally: `npm run check:sdk-coverage`.*/-import { readFileSync, readdirSync, statSync } from 'node:fs';-import { join, resolve } from 'node:path';+import { readFileSync, readdirSync, realpathSync, statSync } from 'node:fs';+import { join } from 'node:path';import { fileURLToPath } from 'node:url';// fileURLToPath, not URL.pathname: the latter stays percent-encoded, so a checkout under a path@@ -356,8 +356,9 @@ const verbPairsOf = (sdk) => {return pairs;};-// A spec imports the helpers above; only a direct run reports and exits.-if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {+// A spec imports the helpers above; only a direct run reports and exits. argv[1] is realpathed because+// Node realpaths the main module's URL: through a symlinked path the two never matched and nothing ran.+if (process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)) {const spec = JSON.parse(readFileSync(join(root, 'openapi.json'), 'utf8')).paths;// Same scope as the path layer: resources the README declares unexposed are not the SDKs' to build.const specByPath = new Map(Object.entries(spec).map(([k, v]) => [normalize(k), v]));diff --git a/scripts/check-sdk-coverage.spec.mjs b/scripts/check-sdk-coverage.spec.mjsindex af62445cf..2eee8ba63 100644--- a/scripts/check-sdk-coverage.spec.mjs+++ b/scripts/check-sdk-coverage.spec.mjs@@ -1,5 +1,10 @@import { test } from 'node:test';import assert from 'node:assert/strict';+import { spawnSync } from 'node:child_process';+import { mkdtempSync, rmSync, symlinkSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { fileURLToPath } from 'node:url';import { joinPythonLiterals, undeclaredVerbPairs } from './check-sdk-coverage.mjs';const specByPath = new Map([@@ -29,3 +34,16 @@ test('joinPythonLiterals joins a path split across adjacent literals', () => {const expr = 'f"/api/sessions/{a}/groups/{b}"\n "/membership-requests/approve"';assert.equal(joinPythonLiterals(expr), '/api/sessions/{a}/groups/{b}/membership-requests/approve');});++test('a run through a symlinked path still reports', () => {+ // Node realpaths the main module's URL but not argv[1], so a guard comparing the unresolved path+ // skipped every check and exited 0 whenever the invocation crossed a symlink (/tmp on macOS).+ const dir = mkdtempSync(join(tmpdir(), 'sdk-coverage-'));+ try {+ symlinkSync(fileURLToPath(new URL('..', import.meta.url)), join(dir, 'repo'));+ const run = spawnSync(process.execPath, [join(dir, 'repo', 'scripts', 'check-sdk-coverage.mjs')], { encoding: 'utf8' });+ assert.match(run.stdout + run.stderr, /SDK contract coverage/);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});diff --git a/scripts/cli-entrypoint-guard.spec.mjs b/scripts/cli-entrypoint-guard.spec.mjsindex 1c54e8b7a..963588225 100644--- a/scripts/cli-entrypoint-guard.spec.mjs+++ b/scripts/cli-entrypoint-guard.spec.mjs@@ -17,8 +17,10 @@ import { fileURLToPath } from 'node:url';* match built with `process.argv[1].split('/')` finds no separator in a Windows path and is loose* enough to fire for an unrelated script of the same name.*- * The correct comparison is resolved path against decoded URL. This test pins it for every script- * rather than for the two that were caught.+ * The correct comparison is real path against decoded URL. A resolved path is not enough: Node+ * realpaths the main module's URL but not argv[1], so a run through a symlink (/tmp on macOS)+ * never matched and exited 0 having checked nothing. This test pins it for every script rather+ * than for the two that were caught.*/const SCRIPTS_DIR = dirname(fileURLToPath(import.meta.url));@@ -26,7 +28,7 @@ const SCRIPTS_DIR = dirname(fileURLToPath(import.meta.url));const GUARD_LINE = /process\.argv\[1\]|import\.meta\.url\s*===/;/** The one shape that is correct on every platform and every path. */-const CORRECT_GUARD = /resolve\(process\.argv\[1\]\)\s*===\s*fileURLToPath\(import\.meta\.url\)/;+const CORRECT_GUARD = /realpathSync\(process\.argv\[1\]\)\s*===\s*fileURLToPath\(import\.meta\.url\)/;const BANNED = [{ pattern: /import\.meta\.url\s*===\s*`file:\/\/\$\{process\.argv\[1\]\}`/, why: 'compares a percent-encoded URL against a raw native path' },@@ -58,9 +60,9 @@ test('no script uses a self-invocation guard that can silently disable it', () =assert.deepEqual(offenders, [], `banned self-invocation guard:\n${offenders.join('\n')}`);});-test('every guarded script compares a resolved path against the decoded module URL', () => {+test('every guarded script compares a real path against the decoded module URL', () => {const offenders = guarded.filter(s => !CORRECT_GUARD.test(s.source)).map(s => s.name);- assert.deepEqual(offenders, [], `missing the resolved-path guard: ${offenders.join(', ')}`);+ assert.deepEqual(offenders, [], `missing the real-path guard: ${offenders.join(', ')}`);});test('the correct guard holds for a path that needs URL escaping', () => {