From 0ef615f83f1d64f98f52d1b113852ae20b23cfa3 Mon Sep 17 00:00:00 2001 From: Hasan <45375125+hasanaburayyan@users.noreply.github.com> Date: Fri, 26 Apr 2024 18:23:00 -0400 Subject: [PATCH] fix(cli)!: .env file not loaded when compiling outside sourceDir (#6348) Closes: https://github.com/winglang/wing/issues/6346 ## Breaking Change - Removed the ability to specify command specific `.env` files ## Summary Basically just moves the loading of environment files to the subcommands. Previously this loading was happening for all commands, which really doesnt make sense since some commands like `lsp`, `docs`, `init` dont need to load those env files. Also I several subcommands call the compile subcommand (secrets, test, etc..) so having subcommand specific `.env` files is additional overhead that Im not sure what the usecase it solves is. If needed, its probably just easier to just use a `--env-file` flag ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- apps/wing/src/cli.ts | 5 ----- apps/wing/src/commands/compile.ts | 5 +++-- apps/wing/src/commands/pack.ts | 2 ++ apps/wing/src/commands/run.ts | 5 ++++- apps/wing/src/env.ts | 10 +--------- libs/wingsdk/src/target-sim/secret.inflight.ts | 11 ----------- 6 files changed, 10 insertions(+), 28 deletions(-) diff --git a/apps/wing/src/cli.ts b/apps/wing/src/cli.ts index 7083289cda8..dfd15b2d6bc 100644 --- a/apps/wing/src/cli.ts +++ b/apps/wing/src/cli.ts @@ -4,7 +4,6 @@ import { satisfies } from "compare-versions"; import { optionallyDisplayDisclaimer } from "./analytics/disclaimer"; import { exportAnalytics } from "./analytics/export"; import { SNAPSHOTS_HELP } from "./commands/test/snapshots-help"; -import { loadEnvVariables } from "./env"; import { currentPackage, projectTemplateNames } from "./util"; export const PACKAGE_VERSION = currentPackage.version; @@ -22,10 +21,6 @@ const DEFAULT_PLATFORM = ["sim"]; let analyticsExportFile: Promise | undefined; function runSubCommand(subCommand: string, path: string = subCommand) { - loadEnvVariables({ - mode: subCommand, - }); - return async (...args: any[]) => { try { // paths other than the root path aren't working unless specified in the path arg diff --git a/apps/wing/src/commands/compile.ts b/apps/wing/src/commands/compile.ts index 2697a0d0ad8..caefeb6b318 100644 --- a/apps/wing/src/commands/compile.ts +++ b/apps/wing/src/commands/compile.ts @@ -1,5 +1,5 @@ import { promises as fsPromise } from "fs"; -import { relative, resolve } from "path"; +import { dirname, relative, resolve } from "path"; import * as wingCompiler from "@winglang/compiler"; import { prettyPrintError } from "@winglang/sdk/lib/util/enhanced-error"; @@ -7,6 +7,7 @@ import chalk from "chalk"; import { CHARS_ASCII, emitDiagnostic, File, Label } from "codespan-wasm"; import debug from "debug"; import { glob } from "glob"; +import { loadEnvVariables } from "../env"; // increase the stack trace limit to 50, useful for debugging Rust panics // (not setting the limit too high in case of infinite recursion) @@ -78,7 +79,7 @@ export async function compile(entrypoint?: string, options?: CompileOptions): Pr } entrypoint = wingFiles[0]; } - + loadEnvVariables({ cwd: resolve(dirname(entrypoint)) }); const coloring = chalk.supportsColor ? chalk.supportsColor.hasBasic : false; try { return await wingCompiler.compile(entrypoint, { diff --git a/apps/wing/src/commands/pack.ts b/apps/wing/src/commands/pack.ts index 3929a82e17b..059e00a3543 100644 --- a/apps/wing/src/commands/pack.ts +++ b/apps/wing/src/commands/pack.ts @@ -8,6 +8,7 @@ import { BuiltinPlatform } from "@winglang/compiler"; import packlist from "npm-packlist"; import * as tar from "tar"; import { compile } from "./compile"; +import { loadEnvVariables } from "../env"; // TODO: add --dry-run option? // TODO: let the user specify library's supported targets in package.json, and compile to each before packaging @@ -79,6 +80,7 @@ export async function pack(options: PackageOptions = {}): Promise { const outfile = options.outFile ? resolve(options.outFile) : undefined; const outdir = outfile ? path.dirname(outfile) : userDir; + loadEnvVariables({ cwd: userDir }); // check package.json exists const originalPkgJsonPath = path.join(userDir, "package.json"); if (!(await exists(originalPkgJsonPath))) { diff --git a/apps/wing/src/commands/run.ts b/apps/wing/src/commands/run.ts index bfd0612f9c4..e93fc5687fe 100644 --- a/apps/wing/src/commands/run.ts +++ b/apps/wing/src/commands/run.ts @@ -1,10 +1,11 @@ import { existsSync } from "fs"; -import { resolve } from "path"; +import { dirname, resolve } from "path"; import { createConsoleApp } from "@wingconsole/app"; import { BuiltinPlatform } from "@winglang/compiler"; import { debug } from "debug"; import { glob } from "glob"; import once from "lodash.once"; +import { loadEnvVariables } from "../env"; import { parseNumericString } from "../util"; import { beforeShutdown } from "../util.before-shutdown.js"; @@ -63,6 +64,8 @@ export async function run(entrypoint?: string, options?: RunOptions) { throw new Error(entrypoint + " doesn't exist"); } + loadEnvVariables({ cwd: resolve(dirname(entrypoint)) }); + if (options?.platform && options?.platform[0] !== BuiltinPlatform.SIM) { throw new Error( `The first platform in the list must be the sim platform (try "-t sim -t ${options.platform.join( diff --git a/apps/wing/src/env.ts b/apps/wing/src/env.ts index 250bd5b5259..f3198d10cc7 100644 --- a/apps/wing/src/env.ts +++ b/apps/wing/src/env.ts @@ -11,12 +11,6 @@ export interface EnvLoadOptions { * @default process.cwd() */ cwd?: string; - - /** - * The mode to load additional environment variables from. - * e.g. "wing compile" has mode "compile", which will load ".env.compile" and ".env.compile.local". - */ - mode?: string; } /** @@ -24,9 +18,7 @@ export interface EnvLoadOptions { */ export function loadEnvVariables(options?: EnvLoadOptions): Record | undefined { const envDir = options?.cwd ?? process.cwd(); - const envFiles = DEFAULT_ENV_FILES.concat( - options?.mode ? [`.env.${options.mode}`, `.env.${options.mode}.local`] : [] - ).map((file) => join(envDir, file)); + const envFiles = DEFAULT_ENV_FILES.map((file) => join(envDir, file)); // Parse `envFiles` and combine their variables into a single object const parsed = Object.fromEntries( diff --git a/libs/wingsdk/src/target-sim/secret.inflight.ts b/libs/wingsdk/src/target-sim/secret.inflight.ts index c6521dca11e..ebfe657c1bc 100644 --- a/libs/wingsdk/src/target-sim/secret.inflight.ts +++ b/libs/wingsdk/src/target-sim/secret.inflight.ts @@ -1,5 +1,3 @@ -import * as fs from "fs"; -import * as path from "path"; import { SecretAttributes, SecretSchema } from "./schema-resources"; import { ISecretClient, SECRET_FQN } from "../cloud"; import { @@ -11,18 +9,9 @@ import { Json, TraceType } from "../std"; export class Secret implements ISecretClient, ISimulatorResourceInstance { private _context: ISimulatorContext | undefined; - private readonly secretsFile: string; private readonly name: string; constructor(props: SecretSchema) { - this.secretsFile = path.join(process.cwd(), ".env"); - - if (!fs.existsSync(this.secretsFile)) { - throw new Error( - `No secrets file found at ${this.secretsFile} while looking for secret ${props.name}` - ); - } - this.name = props.name; }