From ac0e4411706c76968ee4bf5bd5cec85eabbc3cf0 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 20 Dec 2024 14:19:26 +0000 Subject: [PATCH 1/5] Allow env vars to be loaded from .env and EAS env simultaneously --- .../eas-cli/src/commands/worker/deploy.ts | 11 ++++- packages/eas-cli/src/worker/assets.ts | 43 ++++++++++++------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/packages/eas-cli/src/commands/worker/deploy.ts b/packages/eas-cli/src/commands/worker/deploy.ts index 2f905845f6..4716072c88 100644 --- a/packages/eas-cli/src/commands/worker/deploy.ts +++ b/packages/eas-cli/src/commands/worker/deploy.ts @@ -253,7 +253,7 @@ export default class WorkerDeploy extends EasCommand { let progress = ora('Preparing project').start(); try { - const manifest = await WorkerAssets.createManifestAsync( + const manifestResult = await WorkerAssets.createManifestAsync( { environment: flags.environment, projectDir, @@ -261,13 +261,20 @@ export default class WorkerDeploy extends EasCommand { }, graphqlClient ); + if (manifestResult.conflictingVariableNames?.length) { + Log.warn( + '> The following environment variables were loaded both from local .env files as well as EAS environment variables, ' + + ' and will be set to the EAS environment variable values instead: ' + + manifestResult.conflictingVariableNames.join(' '), + ); + } assetMap = await WorkerAssets.createAssetMapAsync( projectDist.type === 'server' ? projectDist.clientPath : projectDist.path ); tarPath = await WorkerAssets.packFilesIterableAsync( emitWorkerTarballAsync({ assetMap, - manifest, + manifest: manifestResult.manifest, }) ); diff --git a/packages/eas-cli/src/worker/assets.ts b/packages/eas-cli/src/worker/assets.ts index 31a56a5d85..7f4ef18251 100644 --- a/packages/eas-cli/src/worker/assets.ts +++ b/packages/eas-cli/src/worker/assets.ts @@ -98,6 +98,11 @@ export interface Manifest { env: Record; } +export interface CreateManifestResult { + conflictingVariableNames: string[] | undefined; + manifest: Manifest; +} + interface CreateManifestParams { projectId: string; projectDir: string; @@ -108,23 +113,31 @@ interface CreateManifestParams { export async function createManifestAsync( params: CreateManifestParams, graphqlClient: ExpoGraphqlClient -): Promise { - let env: Record; +): Promise { + // NOTE: This is required for the .env resolution + process.env.NODE_ENV = 'production'; + // Resolve .env file variables + const env: Record = getEnv(params.projectDir).env; + // Maybe load EAS Environment Variables (based on `--environment` arg) + let conflictingVariableNames: string[] | undefined; if (params.environment) { - env = Object.fromEntries( - ( - await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(graphqlClient, { - appId: params.projectId, - environment: params.environment, - }) - ).map(variable => [variable.name, variable.value ?? undefined]) - ); - } else { - // NOTE: This is required for the .env resolution - process.env.NODE_ENV = 'production'; - env = getEnv(params.projectDir).env; + const loadedVariables = await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(graphqlClient, { + appId: params.projectId, + environment: params.environment, + }); + // Load EAS Env vars into `env` object, keeping track of conflicts + conflictingVariableNames = []; + for (const variable of loadedVariables) { + if (variable.value != null) { + if (env[variable.name] != null) { + conflictingVariableNames.push(variable.name); + } + env[variable.name] = variable.value; + } + } } - return { env }; + const manifest: Manifest = { env }; + return { conflictingVariableNames, manifest }; } interface WorkerFileEntry { From c6ce1604bd8b15b21d4217268ec7fd83b70cabba Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 20 Dec 2024 14:20:30 +0000 Subject: [PATCH 2/5] Add CHANGELOG entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index af18e030cc..78dd550e82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ This is the log of notable changes to EAS CLI and related packages. ### ๐ŸŽ‰ New features +- Load `.env` variables even when `--environment` is specified for `deploy` command. Conflicts will be highlighted by a warning message. ([#2783](https://github.com/expo/eas-cli/pull/2783) by [@kitten](https://github.com/kitten)) + ### ๐Ÿ› Bug fixes ### ๐Ÿงน Chores From 15a64acca532ec16d05491e10a39b7a1e5ab97a8 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 20 Dec 2024 14:33:50 +0000 Subject: [PATCH 3/5] Apply lints --- packages/eas-cli/src/commands/worker/deploy.ts | 6 +++--- packages/eas-cli/src/worker/assets.ts | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/eas-cli/src/commands/worker/deploy.ts b/packages/eas-cli/src/commands/worker/deploy.ts index 4716072c88..dc3c1adecc 100644 --- a/packages/eas-cli/src/commands/worker/deploy.ts +++ b/packages/eas-cli/src/commands/worker/deploy.ts @@ -263,9 +263,9 @@ export default class WorkerDeploy extends EasCommand { ); if (manifestResult.conflictingVariableNames?.length) { Log.warn( - '> The following environment variables were loaded both from local .env files as well as EAS environment variables, ' - + ' and will be set to the EAS environment variable values instead: ' - + manifestResult.conflictingVariableNames.join(' '), + '> The following environment variables were loaded both from local .env files as well as EAS environment variables, ' + + ' and will be set to the EAS environment variable values instead: ' + + manifestResult.conflictingVariableNames.join(' ') ); } assetMap = await WorkerAssets.createAssetMapAsync( diff --git a/packages/eas-cli/src/worker/assets.ts b/packages/eas-cli/src/worker/assets.ts index 7f4ef18251..2536df2bbe 100644 --- a/packages/eas-cli/src/worker/assets.ts +++ b/packages/eas-cli/src/worker/assets.ts @@ -121,10 +121,13 @@ export async function createManifestAsync( // Maybe load EAS Environment Variables (based on `--environment` arg) let conflictingVariableNames: string[] | undefined; if (params.environment) { - const loadedVariables = await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(graphqlClient, { - appId: params.projectId, - environment: params.environment, - }); + const loadedVariables = await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync( + graphqlClient, + { + appId: params.projectId, + environment: params.environment, + } + ); // Load EAS Env vars into `env` object, keeping track of conflicts conflictingVariableNames = []; for (const variable of loadedVariables) { From ccdabb2951cbd7537967b3af7665d360c7271f2d Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Wed, 8 Jan 2025 16:34:56 +0000 Subject: [PATCH 4/5] Switch to new @expo/env@1.0.0 API --- packages/eas-cli/package.json | 2 +- packages/eas-cli/src/worker/assets.ts | 6 ++---- yarn.lock | 8 ++++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/eas-cli/package.json b/packages/eas-cli/package.json index 8894bc5e79..8324161baf 100644 --- a/packages/eas-cli/package.json +++ b/packages/eas-cli/package.json @@ -14,7 +14,7 @@ "@expo/config-plugins": "9.0.12", "@expo/eas-build-job": "1.0.156", "@expo/eas-json": "14.3.1", - "@expo/env": "^0.4.0", + "@expo/env": "^1.0.0", "@expo/json-file": "8.3.3", "@expo/logger": "1.0.117", "@expo/multipart-body-parser": "2.0.0", diff --git a/packages/eas-cli/src/worker/assets.ts b/packages/eas-cli/src/worker/assets.ts index 2536df2bbe..437b9a1ee2 100644 --- a/packages/eas-cli/src/worker/assets.ts +++ b/packages/eas-cli/src/worker/assets.ts @@ -1,4 +1,4 @@ -import { get as getEnv } from '@expo/env'; +import { parseProjectEnv } from '@expo/env'; import { Gzip, GzipOptions } from 'minizlib'; import { HashOptions, createHash, randomBytes } from 'node:crypto'; import fs, { createWriteStream } from 'node:fs'; @@ -114,10 +114,8 @@ export async function createManifestAsync( params: CreateManifestParams, graphqlClient: ExpoGraphqlClient ): Promise { - // NOTE: This is required for the .env resolution - process.env.NODE_ENV = 'production'; // Resolve .env file variables - const env: Record = getEnv(params.projectDir).env; + const { env } = parseProjectEnv(params.projectDir, { mode: 'production' }); // Maybe load EAS Environment Variables (based on `--environment` arg) let conflictingVariableNames: string[] | undefined; if (params.environment) { diff --git a/yarn.lock b/yarn.lock index fa5351b0c6..7385d9ed3a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1475,10 +1475,10 @@ semver "^7.6.2" zod "^3.23.8" -"@expo/env@^0.4.0": - version "0.4.0" - resolved "https://registry.yarnpkg.com/@expo/env/-/env-0.4.0.tgz#1ff3a15084566d12ca92cb67e5b0a9796a9f0aa7" - integrity sha512-g2JYFqck3xKIwJyK+8LxZ2ENZPWtRgjFWpeht9abnKgzXVXBeSNECFBkg+WQjQocSIdxXhEWM6hz4ZAe7Tc4ng== +"@expo/env@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@expo/env/-/env-1.0.0.tgz#8e22894fae04ba6dc41fe0f1351a4973d410273a" + integrity sha512-43R5rxlaLJQOWunyfnQEpgHJCsSLCUWDoU/wJNABEYkHdt1Sjrlf+ieH3MG1f/RZYE+1Wss3sYiO9oPps1aMNg== dependencies: chalk "^4.0.0" debug "^4.3.4" From 3510b238473c88e708b7ff849f9e1af04674e632 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Wed, 8 Jan 2025 16:55:07 +0000 Subject: [PATCH 5/5] Update packages/eas-cli/src/commands/worker/deploy.ts Co-authored-by: Kadi Kraman --- packages/eas-cli/src/commands/worker/deploy.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eas-cli/src/commands/worker/deploy.ts b/packages/eas-cli/src/commands/worker/deploy.ts index dc3c1adecc..ae90cbcdc2 100644 --- a/packages/eas-cli/src/commands/worker/deploy.ts +++ b/packages/eas-cli/src/commands/worker/deploy.ts @@ -263,8 +263,8 @@ export default class WorkerDeploy extends EasCommand { ); if (manifestResult.conflictingVariableNames?.length) { Log.warn( - '> The following environment variables were loaded both from local .env files as well as EAS environment variables, ' + - ' and will be set to the EAS environment variable values instead: ' + + '> The following environment variables were present in local .env files as well as EAS environment variables. ' + + 'In case of conflict, the EAS environment variable values will be used: ' + manifestResult.conflictingVariableNames.join(' ') ); }