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 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/commands/worker/deploy.ts b/packages/eas-cli/src/commands/worker/deploy.ts index 2f905845f6..ae90cbcdc2 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 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(' ') + ); + } 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..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'; @@ -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,32 @@ interface CreateManifestParams { export async function createManifestAsync( params: CreateManifestParams, graphqlClient: ExpoGraphqlClient -): Promise { - let env: Record; +): Promise { + // Resolve .env file variables + const { env } = parseProjectEnv(params.projectDir, { mode: 'production' }); + // 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]) + const loadedVariables = await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync( + graphqlClient, + { + appId: params.projectId, + environment: params.environment, + } ); - } else { - // NOTE: This is required for the .env resolution - process.env.NODE_ENV = 'production'; - env = getEnv(params.projectDir).env; + // 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 { 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"