From 7f2afaad15402b1087fdec0643f80af90c085644 Mon Sep 17 00:00:00 2001 From: Eric Jizba Date: Mon, 14 Nov 2022 12:08:17 -0800 Subject: [PATCH] Add script file properties to function metadata (#646) --- src/WorkerChannel.ts | 1 + src/coreApi/registerFunction.ts | 14 +++++++++++--- src/startApp.ts | 8 +++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/WorkerChannel.ts b/src/WorkerChannel.ts index 1cf521fc..ef557a5e 100644 --- a/src/WorkerChannel.ts +++ b/src/WorkerChannel.ts @@ -50,6 +50,7 @@ export class WorkerChannel { functions: { [id: string]: RegisteredFunction } = {}; workerIndexingLocked = false; isUsingWorkerIndexing = false; + currentEntryPoint?: string; constructor(workerId: string, eventStream: IEventStream, legacyFunctionLoader: ILegacyFunctionLoader) { this.workerId = workerId; diff --git a/src/coreApi/registerFunction.ts b/src/coreApi/registerFunction.ts index d082f406..4c1a46c4 100644 --- a/src/coreApi/registerFunction.ts +++ b/src/coreApi/registerFunction.ts @@ -2,6 +2,7 @@ // Licensed under the MIT License. import { FunctionCallback, FunctionMetadata } from '@azure/functions-core'; +import * as path from 'path'; import { AzureFunctionsRpcMessages as rpc } from '../../azure-functions-language-worker-protobuf/src/rpc'; import { Disposable } from '../Disposable'; import { AzFuncSystemError } from '../errors'; @@ -27,9 +28,16 @@ export function registerFunction( rpcMetadata.rawBindings = Object.entries(metadata.bindings).map(([name, binding]) => { return JSON.stringify({ ...binding, name }); }); - // The host validates that the `scriptFile` property is defined even though neither the host nor the worker needs it - // Long term we should adjust the host to remove that unnecessary validation, but for now we'll just set it to 'n/a' - rpcMetadata.scriptFile = 'n/a'; + + // The host validates that the `scriptFile` property is defined. Neither the host nor the worker needs it, but tooling like the portal may use it so we'll make a best guess + // (The real script file may be a separate file referenced from the entry point, or it may be coming from a different entry point entirely if there are some async shenanigans) + if (channel.currentEntryPoint) { + rpcMetadata.scriptFile = path.basename(channel.currentEntryPoint); + rpcMetadata.directory = path.dirname(channel.currentEntryPoint); + } else { + rpcMetadata.scriptFile = 'unknown'; + } + channel.functions[functionId] = { metadata: rpcMetadata, callback }; return new Disposable(() => { diff --git a/src/startApp.ts b/src/startApp.ts index f434f2f9..8075a49d 100644 --- a/src/startApp.ts +++ b/src/startApp.ts @@ -55,7 +55,13 @@ async function loadEntryPointFile(functionAppDirectory: string, channel: WorkerC level: LogLevel.Debug, logCategory: LogCategory.System, }); - await loadScriptFile(path.join(functionAppDirectory, file), channel.packageJson); + try { + const entryPointFilePath = path.join(functionAppDirectory, file); + channel.currentEntryPoint = entryPointFilePath; + await loadScriptFile(entryPointFilePath, channel.packageJson); + } finally { + channel.currentEntryPoint = undefined; + } channel.log({ message: `Loaded entry point file "${file}"`, level: LogLevel.Debug,