diff --git a/.changeset/template-handler-validation.md b/.changeset/template-handler-validation.md new file mode 100644 index 000000000..9a75fb12e --- /dev/null +++ b/.changeset/template-handler-validation.md @@ -0,0 +1,5 @@ +--- +"@graphprotocol/graph-cli": patch +--- + +Validate template mapping handlers during builds, including when a compiled mapping is reused from another data source or template. diff --git a/packages/cli/src/compiler/index.test.ts b/packages/cli/src/compiler/index.test.ts new file mode 100644 index 000000000..0daa2452f --- /dev/null +++ b/packages/cli/src/compiler/index.test.ts @@ -0,0 +1,89 @@ +import os from 'node:os'; +import path from 'node:path'; +import fs from 'fs-extra'; +import immutable from 'immutable'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import Protocol from '../protocols/index.js'; +import * as asc from './asc.js'; +import Compiler from './index.js'; + +vi.mock('./asc.js', () => ({ ready: vi.fn(), compile: vi.fn() })); + +// A valid WASM module exporting a single no-op function named `present`. +const wasm = Buffer.from([ + 0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 7, 11, 1, 7, 112, 114, 101, 115, 101, + 110, 116, 0, 0, 10, 4, 1, 2, 0, 11, +]); + +describe('compiled mapping handler validation', () => { + let dir: string; + let compiler: Compiler; + + beforeEach(async () => { + dir = await fs.mkdtemp(path.join(os.tmpdir(), 'graph-handlers-')); + await fs.writeFile(path.join(dir, 'mapping.ts'), 'export function present(): void {}'); + // Substreams avoids resolving graph-ts for this compiler unit test. + compiler = new Compiler({ + ipfs: undefined, + subgraphManifest: path.join(dir, 'subgraph.yaml'), + outputDir: path.join(dir, 'build'), + outputFormat: 'wasm', + skipMigrations: true, + protocol: new Protocol('substreams'), + }); + Object.assign(compiler, { protocol: new Protocol('ethereum'), globalsFile: 'global.ts' }); + vi.mocked(asc.compile).mockImplementation(({ baseDir, outputFile }) => { + fs.outputFileSync(path.resolve(baseDir, outputFile), wasm); + }); + }); + + afterEach(async () => { + vi.clearAllMocks(); + await fs.remove(dir); + }); + + function mapping(name: string, handler: string) { + return { + name, + mapping: { file: './mapping.ts', eventHandlers: [{ handler, event: 'Event()' }] }, + }; + } + + it.each([false, true])('rejects missing template handlers (cached: %s)', async cached => { + const manifest = immutable.fromJS({ + dataSources: cached ? [mapping('Source', 'present')] : [], + templates: [mapping('Template', 'missing')], + }); + await expect(compiler.compileSubgraph(manifest, true)).rejects.toThrow( + 'Missing handlers in WASM: missing', + ); + }); + + it('checks each data source even when its mapping is cached', async () => { + const manifest = immutable.fromJS({ + dataSources: [mapping('First', 'present'), mapping('Second', 'missing')], + }); + await expect(compiler.compileSubgraph(manifest, true)).rejects.toThrow( + 'Missing handlers in WASM: missing', + ); + }); + + it('reuses valid mappings without compiling them twice', async () => { + const manifest = immutable.fromJS({ + dataSources: [mapping('Source', 'present')], + templates: [mapping('Template', 'present')], + }); + await compiler.compileSubgraph(manifest, true); + expect(asc.compile).toHaveBeenCalledTimes(1); + }); + + it('retains the option to skip handler validation', async () => { + await compiler.compileSubgraph( + immutable.fromJS({ + dataSources: [], + templates: [mapping('Template', 'missing')], + }), + false, + ); + }); +}); diff --git a/packages/cli/src/compiler/index.ts b/packages/cli/src/compiler/index.ts index 143b5f7e3..3af4b7241 100644 --- a/packages/cli/src/compiler/index.ts +++ b/packages/cli/src/compiler/index.ts @@ -268,7 +268,13 @@ export default class Compiler { ? templates : templates.map((template: any) => template.updateIn(['mapping', 'file'], (mappingPath: string) => - this._compileTemplateMapping(template, mappingPath, compiledFiles, spinner), + this._compileTemplateMapping( + template, + mappingPath, + compiledFiles, + spinner, + validate, + ), ), ), ); @@ -360,6 +366,15 @@ export default class Compiler { const alreadyCompiled = compiledFiles.has(inputCacheKey); if (alreadyCompiled) { const outFile = compiledFiles.get(inputCacheKey); + if (validate) { + const missingHandlers = this._validateHandlersInWasm({ + pathToWasm: outFile, + dataSource, + }); + if (missingHandlers.length > 0) { + throw Error(`\n\tMissing handlers in WASM: ${missingHandlers.join(', ')}`); + } + } step( spinner, 'Compile data source:', @@ -416,10 +431,11 @@ export default class Compiler { } _compileTemplateMapping( - template: immutable.Collection, + template: immutable.Map, mappingPath: string, compiledFiles: Map, spinner: Spinner, + validate = false, ) { try { const templateName = template.get('name'); @@ -435,6 +451,15 @@ export default class Compiler { const alreadyCompiled = compiledFiles.has(inputCacheKey); if (alreadyCompiled) { const outFile = compiledFiles.get(inputCacheKey); + if (validate) { + const missingHandlers = this._validateHandlersInWasm({ + pathToWasm: outFile, + dataSource: template, + }); + if (missingHandlers.length > 0) { + throw Error(`\n\tMissing handlers in WASM: ${missingHandlers.join(', ')}`); + } + } step( spinner, 'Compile data source template:', @@ -477,6 +502,16 @@ export default class Compiler { outputFile, }); + if (validate) { + const missingHandlers = this._validateHandlersInWasm({ + pathToWasm: outFile, + dataSource: template, + }); + if (missingHandlers.length > 0) { + throw Error(`\n\tMissing handlers in WASM: ${missingHandlers.join(', ')}`); + } + } + // Remember the output file to avoid compiling the same file again compiledFiles.set(inputCacheKey, outFile);