From 0b8713cdd3a082dc696798a8ebf27e505af4dc63 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Wed, 21 Oct 2026 22:03:27 +0000 Subject: [PATCH 1/5] build: pin libpg_query to an exact commit and emsdk to 6.0.3; generate every Makefile from x-publish Each version's libpg_query repo, branch and commit now live in package.json x-publish and are applied by copy:templates, so regenerating no longer flips 15/17/18 back to pganalyze sources or drops the v18 memory knobs. The build checks out the pinned commit and keys the source cache by it; wasm:make runs the emscripten/emsdk:6.0.3 image instead of the floating default tag. --- REPO_NOTES.md | 8 ++++---- scripts/copy-templates.js | 13 +++++++++++-- templates/Makefile.template | 21 ++++++++++++++++----- templates/README.md | 13 ++++++++++--- versions/13/Makefile | 19 +++++++++++++++---- versions/13/package.json | 6 ++++-- versions/14/Makefile | 19 +++++++++++++++---- versions/14/package.json | 6 ++++-- versions/15/Makefile | 21 +++++++++++++++------ versions/15/package.json | 6 ++++-- versions/16/Makefile | 19 +++++++++++++++---- versions/16/package.json | 6 ++++-- versions/17/Makefile | 21 +++++++++++++++------ versions/17/package.json | 6 ++++-- versions/18/Makefile | 9 ++++++--- versions/18/package.json | 4 +++- 16 files changed, 145 insertions(+), 52 deletions(-) diff --git a/REPO_NOTES.md b/REPO_NOTES.md index 4ba54e9e..8d8e0351 100644 --- a/REPO_NOTES.md +++ b/REPO_NOTES.md @@ -6,9 +6,9 @@ Versions with `x-publish.fullApi: true` (currently 18) are generated from the full-API templates in `templates/full/` and export the full API (parse, parsePlPgSQL, scan, fingerprint, normalize + sync variants); the other versions use the slim (parse-only) templates. -āš ļø Note: the Makefiles for 15/17/18 have been hand-edited to point at -constructive-io/libpg_query branches; `copy:templates` regenerates Makefiles -with the pganalyze repo URL, so re-apply the repo override if you regenerate. +Each version's libpg_query source (repo, branch/tag and exact commit) comes from +`x-publish` in its package.json, so `copy:templates` regenerates every Makefile +as-is — 15/17/18 point at constructive-io/libpg_query branches, pinned by commit. ## Code Duplication šŸ“‹ @@ -21,7 +21,7 @@ with the pganalyze repo URL, so re-apply the repo override if you regenerate. - `versions/*/src/index.ts` are nearly identical except for version numbers - `versions/*/src/wasm_wrapper.c` are identical - `versions/*/Makefile` differ only in: - - `LIBPG_QUERY_TAG` version + - `LIBPG_QUERY_REPO` / `LIBPG_QUERY_TAG` / `LIBPG_QUERY_COMMIT` - Version 13 has an extra emscripten patch ## Consistency Issues šŸ”§ diff --git a/scripts/copy-templates.js b/scripts/copy-templates.js index 6d8d312d..8213d5f4 100755 --- a/scripts/copy-templates.js +++ b/scripts/copy-templates.js @@ -32,15 +32,22 @@ function loadVersionConfigs() { const packageData = JSON.parse(fs.readFileSync(packageFile, 'utf8')); const version = packageData['x-publish']?.pgVersion; const libpgQueryTag = packageData['x-publish']?.libpgQueryTag; + const libpgQueryRepo = packageData['x-publish']?.libpgQueryRepo; + const libpgQueryCommit = packageData['x-publish']?.libpgQueryCommit; const fullApi = packageData['x-publish']?.fullApi === true; if (version && libpgQueryTag) { + if (!libpgQueryRepo || !/^[0-9a-f]{40}$/.test(libpgQueryCommit || '')) { + throw new Error(`x-publish needs libpgQueryRepo and a full 40-char libpgQueryCommit`); + } configs[version] = { tag: libpgQueryTag, + repo: libpgQueryRepo, + commit: libpgQueryCommit, hasEmscriptenPatch: version === '13', // Only version 13 needs the patch fullApi }; - console.log(` Version ${version}: tag ${libpgQueryTag}${fullApi ? ' (full API)' : ''}`); + console.log(` Version ${version}: ${libpgQueryTag} @ ${libpgQueryCommit.slice(0, 7)}${fullApi ? ' (full API)' : ''}`); } else { console.warn(` Warning: Missing x-publish data in ${packageFile}`); } @@ -106,6 +113,8 @@ function copyTemplates() { // Process Makefile template const makefileTemplate = fs.readFileSync(path.join(templatesDir, 'Makefile.template'), 'utf8'); let makefileContent = makefileTemplate.replace(/{{VERSION_TAG}}/g, config.tag); + makefileContent = makefileContent.replace(/{{LIBPG_QUERY_REPO}}/g, config.repo); + makefileContent = makefileContent.replace(/{{LIBPG_QUERY_COMMIT}}/g, config.commit); makefileContent = makefileContent.replace( /{{EXPORTED_FUNCTIONS}}/g, config.fullApi ? FULL_EXPORTED_FUNCTIONS : SLIM_EXPORTED_FUNCTIONS @@ -132,7 +141,7 @@ function copyTemplates() { // Write Makefile with header fs.writeFileSync(path.join(versionDir, 'Makefile'), MAKEFILE_HEADER + makefileContent); - console.log(` āœ“ Generated Makefile with tag ${config.tag}`); + console.log(` āœ“ Generated Makefile with ${config.tag} @ ${config.commit.slice(0, 7)}`); } console.log('\nāœ… Template copying completed!'); diff --git a/templates/Makefile.template b/templates/Makefile.template index 340d51ff..3c227b46 100644 --- a/templates/Makefile.template +++ b/templates/Makefile.template @@ -1,8 +1,18 @@ WASM_OUT_DIR := wasm WASM_OUT_NAME := libpg-query WASM_MODULE_NAME := PgQueryModule -LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git + +# WASM memory configuration (bytes). Override on the make command line for +# low-memory variants, e.g. `make build WASM_INITIAL_MEMORY=33554432` (32MiB). +WASM_INITIAL_MEMORY ?= 134217728 +WASM_MAXIMUM_MEMORY ?= 1073741824 +WASM_STACK_SIZE ?= 33554432 + +# libpg_query source, pinned to an exact commit so a release can be rebuilt +# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. +LIBPG_QUERY_REPO := {{LIBPG_QUERY_REPO}} LIBPG_QUERY_TAG := {{VERSION_TAG}} +LIBPG_QUERY_COMMIT := {{LIBPG_QUERY_COMMIT}} CACHE_DIR := .cache @@ -25,7 +35,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c -LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) +LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h CXXFLAGS := -O3 -flto @@ -40,6 +50,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) {{#USE_EMSCRIPTEN_PATCH}} ifdef EMSCRIPTEN cd $(LIBPG_QUERY_DIR); patch -p1 < $(shell pwd)/patches/emscripten_disable_spinlocks.patch @@ -71,9 +82,9 @@ ifdef EMSCRIPTEN -sMODULARIZE=1 \ -sEXPORT_ES6=0 \ -sALLOW_MEMORY_GROWTH=1 \ - -sINITIAL_MEMORY=134217728 \ - -sMAXIMUM_MEMORY=1073741824 \ - -sSTACK_SIZE=33554432 \ + -sINITIAL_MEMORY=$(WASM_INITIAL_MEMORY) \ + -sMAXIMUM_MEMORY=$(WASM_MAXIMUM_MEMORY) \ + -sSTACK_SIZE=$(WASM_STACK_SIZE) \ -lpg_query \ -o $@ \ $(SRC_FILES) diff --git a/templates/README.md b/templates/README.md index e507db5d..fc788422 100644 --- a/templates/README.md +++ b/templates/README.md @@ -28,7 +28,9 @@ This script will: The following placeholders are used in template files: -- `{{VERSION_TAG}}` - The libpg_query version tag (e.g., "14-3.0.0") +- `{{LIBPG_QUERY_REPO}}` - The libpg_query git repository to clone +- `{{VERSION_TAG}}` - The libpg_query branch or tag to clone (e.g., "14-3.0.0") +- `{{LIBPG_QUERY_COMMIT}}` - The exact commit checked out after cloning - `{{#USE_EMSCRIPTEN_PATCH}}...{{/USE_EMSCRIPTEN_PATCH}}` - Conditional block for version-specific patches (currently only used in version 13) ## Version-Specific Configurations @@ -40,13 +42,18 @@ The `scripts/copy-templates.js` script automatically reads version-specific conf "publishName": "libpg-query", "pgVersion": "15", "distTag": "pg15", - "libpgQueryTag": "15-4.2.4" + "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", + "libpgQueryTag": "fix/negative-int-pg15", + "libpgQueryCommit": "ae10504217b703340b5632f6345fe1e83d61485c" } ``` The script uses: - `pgVersion` to identify the PostgreSQL version -- `libpgQueryTag` for the {{VERSION_TAG}} placeholder replacement +- `libpgQueryRepo`, `libpgQueryTag` and `libpgQueryCommit` for the source placeholders. The commit + is required (a full 40-character SHA): the build checks it out after cloning, so a branch moving + upstream cannot change what a rebuild produces. To pick up new libpg_query changes, bump the commit + and re-run `npm run copy:templates`. - Version 13 automatically gets the emscripten patch applied ## Important Notes diff --git a/versions/13/Makefile b/versions/13/Makefile index 4ace1182..e6a19379 100644 --- a/versions/13/Makefile +++ b/versions/13/Makefile @@ -6,8 +6,18 @@ WASM_OUT_DIR := wasm WASM_OUT_NAME := libpg-query WASM_MODULE_NAME := PgQueryModule + +# WASM memory configuration (bytes). Override on the make command line for +# low-memory variants, e.g. `make build WASM_INITIAL_MEMORY=33554432` (32MiB). +WASM_INITIAL_MEMORY ?= 134217728 +WASM_MAXIMUM_MEMORY ?= 1073741824 +WASM_STACK_SIZE ?= 33554432 + +# libpg_query source, pinned to an exact commit so a release can be rebuilt +# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git LIBPG_QUERY_TAG := 13-2.2.0 +LIBPG_QUERY_COMMIT := 1097b2c33e54a37c0d2c0f2d498c7d1cf967eae9 CACHE_DIR := .cache @@ -30,7 +40,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c -LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) +LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h CXXFLAGS := -O3 -flto @@ -45,6 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) ifdef EMSCRIPTEN cd $(LIBPG_QUERY_DIR); patch -p1 < $(shell pwd)/patches/emscripten_disable_spinlocks.patch endif @@ -74,9 +85,9 @@ ifdef EMSCRIPTEN -sMODULARIZE=1 \ -sEXPORT_ES6=0 \ -sALLOW_MEMORY_GROWTH=1 \ - -sINITIAL_MEMORY=134217728 \ - -sMAXIMUM_MEMORY=1073741824 \ - -sSTACK_SIZE=33554432 \ + -sINITIAL_MEMORY=$(WASM_INITIAL_MEMORY) \ + -sMAXIMUM_MEMORY=$(WASM_MAXIMUM_MEMORY) \ + -sSTACK_SIZE=$(WASM_STACK_SIZE) \ -lpg_query \ -o $@ \ $(SRC_FILES) diff --git a/versions/13/package.json b/versions/13/package.json index e85c5daa..edef149e 100644 --- a/versions/13/package.json +++ b/versions/13/package.json @@ -13,7 +13,9 @@ "publishName": "libpg-query", "pgVersion": "13", "distTag": "pg13", - "libpgQueryTag": "13-2.2.0" + "libpgQueryRepo": "https://github.com/pganalyze/libpg_query.git", + "libpgQueryTag": "13-2.2.0", + "libpgQueryCommit": "1097b2c33e54a37c0d2c0f2d498c7d1cf967eae9" }, "files": [ "wasm/*" @@ -23,7 +25,7 @@ "build:js": "node scripts/build.js", "build": "pnpm clean && pnpm wasm:build && pnpm build:js", "publish:pkg": "node ../../scripts/publish-single-version.js", - "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) -e EMSCRIPTEN=1 emscripten/emsdk emmake make", + "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) -e EMSCRIPTEN=1 emscripten/emsdk:6.0.3 emmake make", "wasm:build": "pnpm wasm:make build", "wasm:rebuild": "pnpm wasm:make rebuild", "wasm:clean": "pnpm wasm:make clean", diff --git a/versions/14/Makefile b/versions/14/Makefile index 1d9b0738..1c425c86 100644 --- a/versions/14/Makefile +++ b/versions/14/Makefile @@ -6,8 +6,18 @@ WASM_OUT_DIR := wasm WASM_OUT_NAME := libpg-query WASM_MODULE_NAME := PgQueryModule + +# WASM memory configuration (bytes). Override on the make command line for +# low-memory variants, e.g. `make build WASM_INITIAL_MEMORY=33554432` (32MiB). +WASM_INITIAL_MEMORY ?= 134217728 +WASM_MAXIMUM_MEMORY ?= 1073741824 +WASM_STACK_SIZE ?= 33554432 + +# libpg_query source, pinned to an exact commit so a release can be rebuilt +# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git LIBPG_QUERY_TAG := 14-3.0.0 +LIBPG_QUERY_COMMIT := 397cbb9c1b188b8a5c6e1a9633461b2d01903abc CACHE_DIR := .cache @@ -30,7 +40,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c -LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) +LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h CXXFLAGS := -O3 -flto @@ -45,6 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) @@ -71,9 +82,9 @@ ifdef EMSCRIPTEN -sMODULARIZE=1 \ -sEXPORT_ES6=0 \ -sALLOW_MEMORY_GROWTH=1 \ - -sINITIAL_MEMORY=134217728 \ - -sMAXIMUM_MEMORY=1073741824 \ - -sSTACK_SIZE=33554432 \ + -sINITIAL_MEMORY=$(WASM_INITIAL_MEMORY) \ + -sMAXIMUM_MEMORY=$(WASM_MAXIMUM_MEMORY) \ + -sSTACK_SIZE=$(WASM_STACK_SIZE) \ -lpg_query \ -o $@ \ $(SRC_FILES) diff --git a/versions/14/package.json b/versions/14/package.json index c1f7fd6d..de76f1bd 100644 --- a/versions/14/package.json +++ b/versions/14/package.json @@ -13,7 +13,9 @@ "publishName": "libpg-query", "pgVersion": "14", "distTag": "pg14", - "libpgQueryTag": "14-3.0.0" + "libpgQueryRepo": "https://github.com/pganalyze/libpg_query.git", + "libpgQueryTag": "14-3.0.0", + "libpgQueryCommit": "397cbb9c1b188b8a5c6e1a9633461b2d01903abc" }, "files": [ "wasm/*" @@ -23,7 +25,7 @@ "build:js": "node scripts/build.js", "build": "pnpm clean && pnpm wasm:build && pnpm build:js", "publish:pkg": "node ../../scripts/publish-single-version.js", - "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emmake make", + "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk:6.0.3 emmake make", "wasm:build": "pnpm wasm:make build", "wasm:rebuild": "pnpm wasm:make rebuild", "wasm:clean": "pnpm wasm:make clean", diff --git a/versions/15/Makefile b/versions/15/Makefile index 68777d3b..8712e5dd 100644 --- a/versions/15/Makefile +++ b/versions/15/Makefile @@ -6,10 +6,18 @@ WASM_OUT_DIR := wasm WASM_OUT_NAME := libpg-query WASM_MODULE_NAME := PgQueryModule -# LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git -# LIBPG_QUERY_TAG := 15-4.2.4 + +# WASM memory configuration (bytes). Override on the make command line for +# low-memory variants, e.g. `make build WASM_INITIAL_MEMORY=33554432` (32MiB). +WASM_INITIAL_MEMORY ?= 134217728 +WASM_MAXIMUM_MEMORY ?= 1073741824 +WASM_STACK_SIZE ?= 33554432 + +# libpg_query source, pinned to an exact commit so a release can be rebuilt +# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git LIBPG_QUERY_TAG := fix/negative-int-pg15 +LIBPG_QUERY_COMMIT := ae10504217b703340b5632f6345fe1e83d61485c CACHE_DIR := .cache @@ -32,7 +40,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c -LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) +LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h CXXFLAGS := -O3 -flto @@ -47,6 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) @@ -73,9 +82,9 @@ ifdef EMSCRIPTEN -sMODULARIZE=1 \ -sEXPORT_ES6=0 \ -sALLOW_MEMORY_GROWTH=1 \ - -sINITIAL_MEMORY=134217728 \ - -sMAXIMUM_MEMORY=1073741824 \ - -sSTACK_SIZE=33554432 \ + -sINITIAL_MEMORY=$(WASM_INITIAL_MEMORY) \ + -sMAXIMUM_MEMORY=$(WASM_MAXIMUM_MEMORY) \ + -sSTACK_SIZE=$(WASM_STACK_SIZE) \ -lpg_query \ -o $@ \ $(SRC_FILES) diff --git a/versions/15/package.json b/versions/15/package.json index adf7cd30..e334742a 100644 --- a/versions/15/package.json +++ b/versions/15/package.json @@ -13,7 +13,9 @@ "publishName": "libpg-query", "pgVersion": "15", "distTag": "pg15", - "libpgQueryTag": "15-4.2.4" + "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", + "libpgQueryTag": "fix/negative-int-pg15", + "libpgQueryCommit": "ae10504217b703340b5632f6345fe1e83d61485c" }, "files": [ "wasm/*" @@ -23,7 +25,7 @@ "build:js": "node scripts/build.js", "build": "pnpm clean && pnpm wasm:build && pnpm build:js", "publish:pkg": "node ../../scripts/publish-single-version.js", - "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emmake make", + "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk:6.0.3 emmake make", "wasm:build": "pnpm wasm:make build", "wasm:rebuild": "pnpm wasm:make rebuild", "wasm:clean": "pnpm wasm:make clean", diff --git a/versions/16/Makefile b/versions/16/Makefile index 6bd9e62a..689bc935 100644 --- a/versions/16/Makefile +++ b/versions/16/Makefile @@ -6,8 +6,18 @@ WASM_OUT_DIR := wasm WASM_OUT_NAME := libpg-query WASM_MODULE_NAME := PgQueryModule + +# WASM memory configuration (bytes). Override on the make command line for +# low-memory variants, e.g. `make build WASM_INITIAL_MEMORY=33554432` (32MiB). +WASM_INITIAL_MEMORY ?= 134217728 +WASM_MAXIMUM_MEMORY ?= 1073741824 +WASM_STACK_SIZE ?= 33554432 + +# libpg_query source, pinned to an exact commit so a release can be rebuilt +# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git LIBPG_QUERY_TAG := 16-5.2.0 +LIBPG_QUERY_COMMIT := fce106abf41205e5d0db47bea7de44ad1e36f7a5 CACHE_DIR := .cache @@ -30,7 +40,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c -LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) +LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h CXXFLAGS := -O3 -flto @@ -45,6 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) @@ -71,9 +82,9 @@ ifdef EMSCRIPTEN -sMODULARIZE=1 \ -sEXPORT_ES6=0 \ -sALLOW_MEMORY_GROWTH=1 \ - -sINITIAL_MEMORY=134217728 \ - -sMAXIMUM_MEMORY=1073741824 \ - -sSTACK_SIZE=33554432 \ + -sINITIAL_MEMORY=$(WASM_INITIAL_MEMORY) \ + -sMAXIMUM_MEMORY=$(WASM_MAXIMUM_MEMORY) \ + -sSTACK_SIZE=$(WASM_STACK_SIZE) \ -lpg_query \ -o $@ \ $(SRC_FILES) diff --git a/versions/16/package.json b/versions/16/package.json index 8ec811db..85df3bcd 100644 --- a/versions/16/package.json +++ b/versions/16/package.json @@ -13,7 +13,9 @@ "publishName": "libpg-query", "pgVersion": "16", "distTag": "pg16", - "libpgQueryTag": "16-5.2.0" + "libpgQueryRepo": "https://github.com/pganalyze/libpg_query.git", + "libpgQueryTag": "16-5.2.0", + "libpgQueryCommit": "fce106abf41205e5d0db47bea7de44ad1e36f7a5" }, "files": [ "wasm/*" @@ -23,7 +25,7 @@ "build:js": "node scripts/build.js", "build": "pnpm clean && pnpm wasm:build && pnpm build:js", "publish:pkg": "node ../../scripts/publish-single-version.js", - "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emmake make", + "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk:6.0.3 emmake make", "wasm:build": "pnpm wasm:make build", "wasm:rebuild": "pnpm wasm:make rebuild", "wasm:clean": "pnpm wasm:make clean", diff --git a/versions/17/Makefile b/versions/17/Makefile index 2d091cba..b92278c5 100644 --- a/versions/17/Makefile +++ b/versions/17/Makefile @@ -6,10 +6,18 @@ WASM_OUT_DIR := wasm WASM_OUT_NAME := libpg-query WASM_MODULE_NAME := PgQueryModule -# LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git -# LIBPG_QUERY_TAG := 17-6.1.0 + +# WASM memory configuration (bytes). Override on the make command line for +# low-memory variants, e.g. `make build WASM_INITIAL_MEMORY=33554432` (32MiB). +WASM_INITIAL_MEMORY ?= 134217728 +WASM_MAXIMUM_MEMORY ?= 1073741824 +WASM_STACK_SIZE ?= 33554432 + +# libpg_query source, pinned to an exact commit so a release can be rebuilt +# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git LIBPG_QUERY_TAG := 17-constructive +LIBPG_QUERY_COMMIT := 1ade880fd1bbc6e89c33ea2424ddc366c41c1408 CACHE_DIR := .cache @@ -32,7 +40,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c -LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) +LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h CXXFLAGS := -O3 -flto @@ -47,6 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) @@ -73,9 +82,9 @@ ifdef EMSCRIPTEN -sMODULARIZE=1 \ -sEXPORT_ES6=0 \ -sALLOW_MEMORY_GROWTH=1 \ - -sINITIAL_MEMORY=134217728 \ - -sMAXIMUM_MEMORY=1073741824 \ - -sSTACK_SIZE=33554432 \ + -sINITIAL_MEMORY=$(WASM_INITIAL_MEMORY) \ + -sMAXIMUM_MEMORY=$(WASM_MAXIMUM_MEMORY) \ + -sSTACK_SIZE=$(WASM_STACK_SIZE) \ -lpg_query \ -o $@ \ $(SRC_FILES) diff --git a/versions/17/package.json b/versions/17/package.json index e81b7d3a..011180bb 100644 --- a/versions/17/package.json +++ b/versions/17/package.json @@ -13,7 +13,9 @@ "publishName": "libpg-query", "pgVersion": "17", "distTag": "pg17", - "libpgQueryTag": "17-6.1.0" + "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", + "libpgQueryTag": "17-constructive", + "libpgQueryCommit": "1ade880fd1bbc6e89c33ea2424ddc366c41c1408" }, "files": [ "wasm/*" @@ -23,7 +25,7 @@ "build:js": "node scripts/build.js", "build": "pnpm clean && pnpm wasm:build && pnpm build:js", "publish:pkg": "node ../../scripts/publish-single-version.js", - "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emmake make", + "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk:6.0.3 emmake make", "wasm:build": "pnpm wasm:make build", "wasm:rebuild": "pnpm wasm:make rebuild", "wasm:clean": "pnpm wasm:make clean", diff --git a/versions/18/Makefile b/versions/18/Makefile index d1b233be..bd4daadc 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -12,10 +12,12 @@ WASM_MODULE_NAME := PgQueryModule WASM_INITIAL_MEMORY ?= 134217728 WASM_MAXIMUM_MEMORY ?= 1073741824 WASM_STACK_SIZE ?= 33554432 -# LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git -# LIBPG_QUERY_TAG := 18.0.0 + +# libpg_query source, pinned to an exact commit so a release can be rebuilt +# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates. LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git LIBPG_QUERY_TAG := 18-constructive +LIBPG_QUERY_COMMIT := b0f5f331dc50d69597ec610de7b21afa47a66808 CACHE_DIR := .cache @@ -38,7 +40,7 @@ endif PLATFORM_ARCH := $(PLATFORM)-$(ARCH) SRC_FILES := src/wasm_wrapper.c -LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) +LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_COMMIT) LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h CXXFLAGS := -O3 -flto @@ -53,6 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/18/package.json b/versions/18/package.json index 5fce3b12..026b6a55 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -13,7 +13,9 @@ "publishName": "libpg-query", "pgVersion": "18", "distTag": "pg18", + "libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git", "libpgQueryTag": "18-constructive", + "libpgQueryCommit": "b0f5f331dc50d69597ec610de7b21afa47a66808", "fullApi": true }, "files": [ @@ -24,7 +26,7 @@ "build:js": "node scripts/build.js", "build": "pnpm clean && pnpm wasm:build && pnpm build:js", "publish:pkg": "node ../../scripts/publish-single-version.js", - "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emmake make", + "wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk:6.0.3 emmake make", "wasm:build": "pnpm wasm:make build", "wasm:rebuild": "pnpm wasm:make rebuild", "wasm:clean": "pnpm wasm:make clean", From 20d3bd200352915d4b39663f0b215b7fda2bc93e Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 22 Oct 2026 05:17:19 +0000 Subject: [PATCH 2/5] build: fetch protos from the pinned commit; drop a half-made clone on failed checkout; fail on a bad pin - fetch-protos.js downloads pg_query.proto from libpgQueryRepo@libpgQueryCommit (the 17-constructive / fix/negative-int-pg15 branch names 404 on pganalyze tags), only writes the file after a complete 200 (a 404 used to truncate the committed proto to 0 bytes) and exits 1 on any failure. - Makefile clone + checkout is one recipe line that removes the directory if either step fails, so make never reuses a clone at the wrong commit. - copy-templates.js no longer swallows x-publish validation errors. --- scripts/copy-templates.js | 44 +++++++++++++++++-------------------- scripts/fetch-protos.js | 43 +++++++++++++++++++++--------------- templates/Makefile.template | 5 +++-- versions/13/Makefile | 5 +++-- versions/14/Makefile | 5 +++-- versions/15/Makefile | 5 +++-- versions/16/Makefile | 5 +++-- versions/17/Makefile | 5 +++-- versions/18/Makefile | 5 +++-- 9 files changed, 66 insertions(+), 56 deletions(-) diff --git a/scripts/copy-templates.js b/scripts/copy-templates.js index 8213d5f4..c2c9c56e 100755 --- a/scripts/copy-templates.js +++ b/scripts/copy-templates.js @@ -28,31 +28,27 @@ function loadVersionConfigs() { console.log(`Found ${packageFiles.length} package.json files\n`); packageFiles.forEach(packageFile => { - try { - const packageData = JSON.parse(fs.readFileSync(packageFile, 'utf8')); - const version = packageData['x-publish']?.pgVersion; - const libpgQueryTag = packageData['x-publish']?.libpgQueryTag; - const libpgQueryRepo = packageData['x-publish']?.libpgQueryRepo; - const libpgQueryCommit = packageData['x-publish']?.libpgQueryCommit; - const fullApi = packageData['x-publish']?.fullApi === true; - - if (version && libpgQueryTag) { - if (!libpgQueryRepo || !/^[0-9a-f]{40}$/.test(libpgQueryCommit || '')) { - throw new Error(`x-publish needs libpgQueryRepo and a full 40-char libpgQueryCommit`); - } - configs[version] = { - tag: libpgQueryTag, - repo: libpgQueryRepo, - commit: libpgQueryCommit, - hasEmscriptenPatch: version === '13', // Only version 13 needs the patch - fullApi - }; - console.log(` Version ${version}: ${libpgQueryTag} @ ${libpgQueryCommit.slice(0, 7)}${fullApi ? ' (full API)' : ''}`); - } else { - console.warn(` Warning: Missing x-publish data in ${packageFile}`); + const packageData = JSON.parse(fs.readFileSync(packageFile, 'utf8')); + const version = packageData['x-publish']?.pgVersion; + const libpgQueryTag = packageData['x-publish']?.libpgQueryTag; + const libpgQueryRepo = packageData['x-publish']?.libpgQueryRepo; + const libpgQueryCommit = packageData['x-publish']?.libpgQueryCommit; + const fullApi = packageData['x-publish']?.fullApi === true; + + if (version && libpgQueryTag) { + if (!libpgQueryRepo || !/^[0-9a-f]{40}$/.test(libpgQueryCommit || '')) { + throw new Error(`${packageFile}: x-publish needs libpgQueryRepo and a full 40-char libpgQueryCommit`); } - } catch (error) { - console.error(` Error reading ${packageFile}: ${error.message}`); + configs[version] = { + tag: libpgQueryTag, + repo: libpgQueryRepo, + commit: libpgQueryCommit, + hasEmscriptenPatch: version === '13', // Only version 13 needs the patch + fullApi + }; + console.log(` Version ${version}: ${libpgQueryTag} @ ${libpgQueryCommit.slice(0, 7)}${fullApi ? ' (full API)' : ''}`); + } else { + console.warn(` Warning: Missing x-publish data in ${packageFile}`); } }); diff --git a/scripts/fetch-protos.js b/scripts/fetch-protos.js index 646130ac..fcec6a2e 100644 --- a/scripts/fetch-protos.js +++ b/scripts/fetch-protos.js @@ -18,7 +18,9 @@ function getVersionMappings() { pgVersion: pkg['x-publish'].pgVersion, packageVersion: pkg.version, distTag: pkg['x-publish'].distTag, - libpgQueryTag: pkg['x-publish'].libpgQueryTag + libpgQueryTag: pkg['x-publish'].libpgQueryTag, + libpgQueryRepo: pkg['x-publish'].libpgQueryRepo, + libpgQueryCommit: pkg['x-publish'].libpgQueryCommit }); } } @@ -27,24 +29,24 @@ function getVersionMappings() { return mappings; } -// Download file from URL +// Download file from URL; dest is only written after a complete 200 response function downloadFile(url, dest) { return new Promise((resolve, reject) => { - const file = fs.createWriteStream(dest); console.log('Downloading', url, 'to', dest); https.get(url, (response) => { - if (response.statusCode === 200) { - response.pipe(file); - file.on('finish', () => { - file.close(); - resolve(); - }); - } else { + if (response.statusCode !== 200) { + response.resume(); reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`)); + return; } - }).on('error', (err) => { - reject(err); - }); + const chunks = []; + response.on('data', (chunk) => chunks.push(chunk)); + response.on('end', () => { + fs.writeFileSync(dest, Buffer.concat(chunks)); + resolve(); + }); + response.on('error', reject); + }).on('error', reject); }); } @@ -61,7 +63,7 @@ async function fetchProtos() { console.log('Fetching protobuf files for all versions...\n'); for (const mapping of mappings) { - const { pgVersion, libpgQueryTag } = mapping; + const { pgVersion, libpgQueryTag, libpgQueryRepo, libpgQueryCommit } = mapping; const versionDir = path.join(protosDir, pgVersion); // Create version directory @@ -69,11 +71,12 @@ async function fetchProtos() { fs.mkdirSync(versionDir, { recursive: true }); } - // Use the libpgQueryTag from the Makefile - const url = `https://raw.githubusercontent.com/pganalyze/libpg_query/refs/tags/${libpgQueryTag}/protobuf/pg_query.proto`; + // The same repo and commit the WASM is built from + const repoPath = new URL(libpgQueryRepo).pathname.replace(/^\/|\.git$/g, ''); + const url = `https://raw.githubusercontent.com/${repoPath}/${libpgQueryCommit}/protobuf/pg_query.proto`; const destPath = path.join(versionDir, 'pg_query.proto'); - console.log(`Fetching protobuf for PostgreSQL ${pgVersion} with tag ${libpgQueryTag}...`); + console.log(`Fetching protobuf for PostgreSQL ${pgVersion} from ${repoPath} ${libpgQueryTag} @ ${libpgQueryCommit.slice(0, 7)}...`); try { await downloadFile(url, destPath); @@ -82,6 +85,7 @@ async function fetchProtos() { console.log(` Saved to: ${destPath}\n`); } catch (error) { console.log(`āŒ Failed to fetch protobuf for PostgreSQL ${pgVersion}: ${error.message}\n`); + process.exitCode = 1; } } @@ -90,7 +94,10 @@ async function fetchProtos() { // Run the script if (require.main === module) { - fetchProtos().catch(console.error); + fetchProtos().catch((error) => { + console.error(error); + process.exitCode = 1; + }); } module.exports = { fetchProtos, getVersionMappings }; \ No newline at end of file diff --git a/templates/Makefile.template b/templates/Makefile.template index 3c227b46..0e5b4d77 100644 --- a/templates/Makefile.template +++ b/templates/Makefile.template @@ -49,8 +49,9 @@ endif # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) - git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) + git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + { rm -rf $(LIBPG_QUERY_DIR); exit 1; } {{#USE_EMSCRIPTEN_PATCH}} ifdef EMSCRIPTEN cd $(LIBPG_QUERY_DIR); patch -p1 < $(shell pwd)/patches/emscripten_disable_spinlocks.patch diff --git a/versions/13/Makefile b/versions/13/Makefile index e6a19379..e22ce432 100644 --- a/versions/13/Makefile +++ b/versions/13/Makefile @@ -54,8 +54,9 @@ endif # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) - git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) + git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + { rm -rf $(LIBPG_QUERY_DIR); exit 1; } ifdef EMSCRIPTEN cd $(LIBPG_QUERY_DIR); patch -p1 < $(shell pwd)/patches/emscripten_disable_spinlocks.patch endif diff --git a/versions/14/Makefile b/versions/14/Makefile index 1c425c86..2f2af176 100644 --- a/versions/14/Makefile +++ b/versions/14/Makefile @@ -54,8 +54,9 @@ endif # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) - git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) + git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/15/Makefile b/versions/15/Makefile index 8712e5dd..a9a774c5 100644 --- a/versions/15/Makefile +++ b/versions/15/Makefile @@ -54,8 +54,9 @@ endif # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) - git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) + git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/16/Makefile b/versions/16/Makefile index 689bc935..2dd87a8d 100644 --- a/versions/16/Makefile +++ b/versions/16/Makefile @@ -54,8 +54,9 @@ endif # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) - git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) + git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/17/Makefile b/versions/17/Makefile index b92278c5..c7f06b0a 100644 --- a/versions/17/Makefile +++ b/versions/17/Makefile @@ -54,8 +54,9 @@ endif # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) - git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) + git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/18/Makefile b/versions/18/Makefile index bd4daadc..07a633ea 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -54,8 +54,9 @@ endif # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) - git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) + git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) From ed6deea7288da70f99e445a8edfe96abd6b9979f Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Fri, 23 Oct 2026 22:00:12 +0000 Subject: [PATCH 3/5] build(v13): apply the spinlock patch in the clone step so a failed patch drops the clone --- templates/Makefile.template | 15 ++++++++------- versions/13/Makefile | 10 ++++++---- versions/14/Makefile | 2 +- versions/15/Makefile | 2 +- versions/16/Makefile | 2 +- versions/17/Makefile | 2 +- versions/18/Makefile | 2 +- 7 files changed, 19 insertions(+), 16 deletions(-) diff --git a/templates/Makefile.template b/templates/Makefile.template index 0e5b4d77..952520d5 100644 --- a/templates/Makefile.template +++ b/templates/Makefile.template @@ -46,17 +46,18 @@ else $(error Native builds are no longer supported. Use EMSCRIPTEN=1 for WASM builds only.) endif -# Clone libpg_query source (lives in CACHE_DIR) +{{#USE_EMSCRIPTEN_PATCH}} +# Applied in the same step as the clone, so a failed patch removes the clone. +ifdef EMSCRIPTEN +LIBPG_QUERY_PATCH := && patch -d $(LIBPG_QUERY_DIR) -p1 < $(CURDIR)/patches/emscripten_disable_spinlocks.patch +endif + +{{/USE_EMSCRIPTEN_PATCH}}# Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_PATCH) || \ { rm -rf $(LIBPG_QUERY_DIR); exit 1; } -{{#USE_EMSCRIPTEN_PATCH}} -ifdef EMSCRIPTEN - cd $(LIBPG_QUERY_DIR); patch -p1 < $(shell pwd)/patches/emscripten_disable_spinlocks.patch -endif -{{/USE_EMSCRIPTEN_PATCH}} $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/13/Makefile b/versions/13/Makefile index e22ce432..47cdf4d7 100644 --- a/versions/13/Makefile +++ b/versions/13/Makefile @@ -51,15 +51,17 @@ else $(error Native builds are no longer supported. Use EMSCRIPTEN=1 for WASM builds only.) endif +# Applied in the same step as the clone, so a failed patch removes the clone. +ifdef EMSCRIPTEN +LIBPG_QUERY_PATCH := && patch -d $(LIBPG_QUERY_DIR) -p1 < $(CURDIR)/patches/emscripten_disable_spinlocks.patch +endif + # Clone libpg_query source (lives in CACHE_DIR) $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_PATCH) || \ { rm -rf $(LIBPG_QUERY_DIR); exit 1; } -ifdef EMSCRIPTEN - cd $(LIBPG_QUERY_DIR); patch -p1 < $(shell pwd)/patches/emscripten_disable_spinlocks.patch -endif $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/14/Makefile b/versions/14/Makefile index 2f2af176..5fe746c8 100644 --- a/versions/14/Makefile +++ b/versions/14/Makefile @@ -55,7 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_PATCH) || \ { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/15/Makefile b/versions/15/Makefile index a9a774c5..a16a538c 100644 --- a/versions/15/Makefile +++ b/versions/15/Makefile @@ -55,7 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_PATCH) || \ { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/16/Makefile b/versions/16/Makefile index 2dd87a8d..05e7e6f3 100644 --- a/versions/16/Makefile +++ b/versions/16/Makefile @@ -55,7 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_PATCH) || \ { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/17/Makefile b/versions/17/Makefile index c7f06b0a..747eb3f0 100644 --- a/versions/17/Makefile +++ b/versions/17/Makefile @@ -55,7 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_PATCH) || \ { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) diff --git a/versions/18/Makefile b/versions/18/Makefile index 07a633ea..c0c91c28 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -55,7 +55,7 @@ endif $(LIBPG_QUERY_DIR): mkdir -p $(CACHE_DIR) git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) && \ - git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) || \ + git -C $(LIBPG_QUERY_DIR) -c advice.detachedHead=false checkout $(LIBPG_QUERY_COMMIT) $(LIBPG_QUERY_PATCH) || \ { rm -rf $(LIBPG_QUERY_DIR); exit 1; } $(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) From b8a109f01761b154a423c3cd8dde3f57c905e67f Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Fri, 23 Oct 2026 22:25:18 +0000 Subject: [PATCH 4/5] docs(publish): how to keep the C source pins in sync; drop parser build types that no longer exist --- PUBLISH.md | 57 +++++++++++++++++++++++++--------------------------- package.json | 1 - 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/PUBLISH.md b/PUBLISH.md index fedc2e2a..1f78d41d 100644 --- a/PUBLISH.md +++ b/PUBLISH.md @@ -1,5 +1,26 @@ # Publishing Guide +## Keeping the C source in sync + +Each `versions//package.json` pins its libpg_query source in `x-publish` +(`libpgQueryRepo`, `libpgQueryTag`, `libpgQueryCommit`), and the Docker build uses +`emscripten/emsdk:6.0.3`. Every generated Makefile and the protobufs come from those pins, +and the pins ship in the published package.json. + +To move a version to new C code, do all of this in one commit: + +```bash +# 1. edit x-publish.libpgQueryCommit (full 40-char SHA) in versions//package.json +pnpm copy:templates # regenerate the Makefiles +pnpm fetch:protos # protos//pg_query.proto at the new commit +pnpm build:types && pnpm build:enums +cd versions/ && pnpm build && pnpm test +``` + +If you skip the build when publishing, `versions//wasm/` must have been built after the +last pin change, or the package will name one commit and ship another. After publishing, +`npm view libpg-query@pg x-publish` should show the commit you expect. + ## Automated Publishing (Recommended) ### Version Packages (libpg-query) @@ -14,7 +35,7 @@ This interactive script will: - Ask if you want to skip the build step (useful if already built) - Always run tests (even if build is skipped) - Publish each selected version -- Optionally promote pg17 to latest +- Optionally promote pg17 to latest (`latest` is currently pg18; to keep it there, answer no) ### Types Packages ```bash @@ -56,16 +77,6 @@ PARSER_BUILD_TYPE=full pnpm run publish:parser PARSER_BUILD_TYPE=lts pnpm run publish:parser ``` -#### Latest Only (version 17) -```bash -PARSER_BUILD_TYPE=latest pnpm run publish:parser -``` - -#### Legacy Build (versions 13-15) -```bash -PARSER_BUILD_TYPE=legacy pnpm run publish:parser -``` - This command will: - Navigate to the parser directory - Build the parser with the specified configuration @@ -241,19 +252,15 @@ pnpm publish The parser package is now a simple distribution package that copies pre-built WASM files. No TypeScript compilation needed! #### Available Build Types: -- **full**: All versions (13, 14, 15, 16, 17) - Default -- **lts**: LTS versions only (16, 17) -- **latest**: Latest version only (17) -- **legacy**: Legacy versions (13, 14, 15) +- **full**: All versions (13, 14, 15, 16, 17, 18) - Default +- **lts**: LTS versions only (15, 16, 17, 18) ```bash # Full build (default) npm run build -# Specific builds +# LTS build npm run build:lts -npm run build:latest -npm run build:legacy # Or using environment variable PARSER_BUILD_TYPE=lts npm run build @@ -275,11 +282,11 @@ The templates automatically adjust to include only the versions specified in the **Important**: Before building the parser package, ensure that the version packages are built first: ```bash # Build all version packages first -pnpm build # builds libpg-query versions +pnpm build:versions # Then build the parser with desired configuration cd parser -npm run build:lts # or build:full, build:latest, etc. +npm run build:lts # or build:full ``` ### Types Bundling @@ -299,10 +306,6 @@ npm publish # Publish LTS version with lts tag npm run build:lts npm publish --tag lts - -# Publish legacy version with legacy tag -npm run build:legacy -npm publish --tag legacy ``` ### What it does @@ -318,12 +321,6 @@ npm publish --tag legacy # Install latest (full build) npm install @pgsql/parser -# Install LTS version -npm install @pgsql/parser@lts - -# Install legacy version -npm install @pgsql/parser@legacy - # Use version-specific imports: # import { parse } from '@pgsql/parser/v17' # import { parse } from '@pgsql/parser/v16' diff --git a/package.json b/package.json index df537e2a..b259f9b7 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,6 @@ "build:parser": "pnpm --filter @pgsql/parser build", "build:parser:lts": "PARSER_BUILD_TYPE=lts pnpm --filter @pgsql/parser build", "build:parser:full": "PARSER_BUILD_TYPE=full pnpm --filter @pgsql/parser build", - "build:parser:legacy": "PARSER_BUILD_TYPE=legacy pnpm --filter @pgsql/parser build", "test:parser": "pnpm --filter @pgsql/parser test", "publish:parser": "pnpm --filter @pgsql/parser publish" }, From 792131f753f1c250c1ad70423d4199b1d6878c07 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Fri, 23 Oct 2026 22:52:10 +0000 Subject: [PATCH 5/5] publish: record sha256 of the built WASM in x-publish.wasmSha256 --- PUBLISH.md | 5 +++++ scripts/publish-single-version.js | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/PUBLISH.md b/PUBLISH.md index 1f78d41d..deab377d 100644 --- a/PUBLISH.md +++ b/PUBLISH.md @@ -21,6 +21,11 @@ If you skip the build when publishing, `versions//wasm/` must have been built last pin change, or the package will name one commit and ship another. After publishing, `npm view libpg-query@pg x-publish` should show the commit you expect. +`publish:pkg` also writes `x-publish.wasmSha256`, the sha256 of `wasm/libpg-query.wasm` and +`wasm/libpg-query.js`, into the published package.json. To verify a release, check out the +`gitHead` it was published from, run `pnpm wasm:build` in `versions/`, and compare +`sha256sum wasm/libpg-query.*` with `npm view libpg-query@ x-publish.wasmSha256`. + ## Automated Publishing (Recommended) ### Version Packages (libpg-query) diff --git a/scripts/publish-single-version.js b/scripts/publish-single-version.js index 4dc0b507..09de5d51 100644 --- a/scripts/publish-single-version.js +++ b/scripts/publish-single-version.js @@ -2,6 +2,7 @@ const fs = require('fs'); const path = require('path'); +const crypto = require('crypto'); const { execSync } = require('child_process'); const pkgPath = path.join(process.cwd(), 'package.json'); @@ -22,7 +23,22 @@ if (!original.name || !original.version) { process.exit(1); } -const modified = { ...original, name: publishName }; +// Hashes of the Emscripten outputs, so anyone can rebuild from the x-publish pins and compare. +const wasmSha256 = {}; +for (const file of ['wasm/libpg-query.wasm', 'wasm/libpg-query.js']) { + const filePath = path.join(process.cwd(), file); + if (!fs.existsSync(filePath)) { + console.error(`āŒ ${file} not found; build this version first.`); + process.exit(1); + } + wasmSha256[file] = crypto.createHash('sha256').update(fs.readFileSync(filePath)).digest('hex'); +} + +const modified = { + ...original, + name: publishName, + 'x-publish': { ...publishMeta, wasmSha256 } +}; try { console.log(`šŸ“¦ Publishing ${publishName}@${original.version} with tag '${distTag}'...`);