From 17be1c8199ff23652fb9f3a07197502a2ce7919e Mon Sep 17 00:00:00 2001 From: Alex McKinney Date: Tue, 3 Sep 2024 18:30:42 -0400 Subject: [PATCH] fix(cli): Substitute env vars before IR generation (#4520) --- packages/cli/cli/versions.yml | 9 ++++++++ .../src/createAndStartJob.ts | 21 ++++--------------- .../src/runRemoteGenerationForGenerator.ts | 18 +++++++++++++--- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/cli/cli/versions.yml b/packages/cli/cli/versions.yml index bff68f0a56e..1e0b86a4731 100644 --- a/packages/cli/cli/versions.yml +++ b/packages/cli/cli/versions.yml @@ -1,3 +1,12 @@ +- changelog_entry: + - summary: | + Fix an issue where some postman environment variables (e.g. API key) were not substituted + when running fern generate. + type: fix + created_at: '2024-09-03' + ir_version: 53 + version: 0.41.0-rc1 + - changelog_entry: - summary: | Every fern folder that is using OpenAPI must configure an explicit location to the diff --git a/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts b/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts index 2e4f02bfabe..0256cfb4a7e 100644 --- a/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts +++ b/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts @@ -8,7 +8,6 @@ import { ROOT_API_FILENAME } from "@fern-api/configuration"; import { createFiddleService, getFiddleOrigin } from "@fern-api/core"; -import { replaceEnvVariables } from "@fern-api/core-utils"; import { AbsoluteFilePath, dirname, join, RelativeFilePath, stringifyLargeObject } from "@fern-api/fs-utils"; import { migrateIntermediateRepresentationForGenerator, @@ -94,7 +93,8 @@ async function createJob({ whitelabel: FernFiddle.WhitelabelConfig | undefined; absolutePathToPreview: AbsoluteFilePath | undefined; }): Promise { - const isPreview = absolutePathToPreview != null; + const remoteGenerationService = createFiddleService({ token: token.value }); + const generatorConfig: FernFiddle.GeneratorConfigV2 = { id: generatorInvocation.name, version: generatorInvocation.version, @@ -103,19 +103,6 @@ async function createJob({ publishMetadata: generatorInvocation.publishMetadata }; - /** Sugar to substitute templated env vars in a standard way */ - const substituteEnvVars = (stringOrObject: T) => - replaceEnvVariables( - stringOrObject, - { onError: (e) => context.failAndThrow(e) }, - { substituteAsEmpty: isPreview } - ); - - const generatorConfigsWithEnvVarSubstitutions = substituteEnvVars(generatorConfig); - const whitelabelWithEnvVarSubstiutions = whitelabel != null ? substituteEnvVars(whitelabel) : undefined; - - const remoteGenerationService = createFiddleService({ token: token.value }); - let fernDefinitionMetadata: FernFiddle.remoteGen.FernDefinitionMetadata | undefined; // Only write definition if output mode is github if (generatorInvocation.outputMode.type.startsWith("github")) { @@ -214,14 +201,14 @@ async function createJob({ apiName: workspace.definition.rootApiFile.contents.name, version, organizationName: organization, - generators: [generatorConfigsWithEnvVarSubstitutions], + generators: [generatorConfig], uploadToS3: shouldUploadToS3({ outputMode: generatorInvocation.outputMode, generatorInvocation, absolutePathToPreview, shouldLogS3Url }), - whitelabel: whitelabelWithEnvVarSubstiutions, + whitelabel, fernDefinitionMetadata, preview: absolutePathToPreview != null }); diff --git a/packages/cli/generation/remote-generation/remote-workspace-runner/src/runRemoteGenerationForGenerator.ts b/packages/cli/generation/remote-generation/remote-workspace-runner/src/runRemoteGenerationForGenerator.ts index 2524861d869..f636061e978 100644 --- a/packages/cli/generation/remote-generation/remote-workspace-runner/src/runRemoteGenerationForGenerator.ts +++ b/packages/cli/generation/remote-generation/remote-workspace-runner/src/runRemoteGenerationForGenerator.ts @@ -13,6 +13,7 @@ import { createAndStartJob } from "./createAndStartJob"; import { pollJobAndReportStatus } from "./pollJobAndReportStatus"; import { RemoteTaskHandler } from "./RemoteTaskHandler"; import { SourceUploader } from "./SourceUploader"; +import { replaceEnvVariables } from "@fern-api/core-utils"; export async function runRemoteGenerationForGenerator({ projectConfig, @@ -91,20 +92,31 @@ export async function runRemoteGenerationForGenerator({ ir.sourceConfig = sourceConfig; } + /** Sugar to substitute templated env vars in a standard way */ + const isPreview = absolutePathToPreview != null; + const substituteEnvVars = (stringOrObject: T) => + replaceEnvVariables( + stringOrObject, + { onError: (e) => interactiveTaskContext.failAndThrow(e) }, + { substituteAsEmpty: isPreview } + ); + + const generatorInvocationWithEnvVarSubstitutions = substituteEnvVars(generatorInvocation); + const job = await createAndStartJob({ projectConfig, workspace, organization, - generatorInvocation, + generatorInvocation: generatorInvocationWithEnvVarSubstitutions, context: interactiveTaskContext, version, intermediateRepresentation: { ...ir, - publishConfig: getPublishConfig({ generatorInvocation }) + publishConfig: getPublishConfig({ generatorInvocation: generatorInvocationWithEnvVarSubstitutions }) }, shouldLogS3Url, token, - whitelabel, + whitelabel: whitelabel != null ? substituteEnvVars(whitelabel) : undefined, irVersionOverride, absolutePathToPreview });