Skip to content

Commit

Permalink
fix(cli): load env files depepnding on the wing command (#7021)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyrpex authored Aug 15, 2024
1 parent ef4a141 commit 1dc08f5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/wing-console/console/server/src/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const createCompiler = ({
return;
}

loadEnvVariables({ cwd: dirname });
loadEnvVariables({ modes: ["run", "it"], cwd: dirname });

isCompiling = true;
await events.emit("compiling");
Expand Down
5 changes: 4 additions & 1 deletion apps/wing/src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export async function compile(entrypoint?: string, options?: CompileOptions): Pr
}
entrypoint = wingFiles[0];
}
loadEnvVariables({ cwd: resolve(dirname(entrypoint)) });
loadEnvVariables({
modes: options?.testing ? ["test"] : ["compile"],
cwd: resolve(dirname(entrypoint)),
});
const coloring = chalk.supportsColor ? chalk.supportsColor.hasBasic : false;
const compileOutput = await wingCompiler.compile(entrypoint, {
...options,
Expand Down
2 changes: 1 addition & 1 deletion apps/wing/src/commands/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function pack(options: PackageOptions = {}): Promise<string> {
const outfile = options.outFile ? resolve(options.outFile) : undefined;
const outdir = outfile ? path.dirname(outfile) : userDir;

loadEnvVariables({ cwd: userDir });
loadEnvVariables({ modes: ["pack"], cwd: userDir });
// check package.json exists
const originalPkgJsonPath = path.join(userDir, "package.json");
if (!(await exists(originalPkgJsonPath))) {
Expand Down
2 changes: 1 addition & 1 deletion apps/wing/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function run(entrypoint?: string, options?: RunOptions) {
throw new Error(entrypoint + " doesn't exist");
}

loadEnvVariables({ cwd: resolve(dirname(entrypoint)) });
loadEnvVariables({ modes: ["run", "it"], cwd: resolve(dirname(entrypoint)) });

if (options?.platform && options?.platform[0] !== BuiltinPlatform.SIM) {
throw new Error(
Expand Down
22 changes: 18 additions & 4 deletions libs/wingsdk/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ export function preflightClassSingleton(
return root.resourceSingletons[type];
}

const DEFAULT_ENV_FILES = [`.env`, `.env.local`];

/**
* Options for loading environment variables.
*/
Expand All @@ -244,16 +242,32 @@ export interface EnvLoadOptions {
* @default process.cwd()
*/
readonly cwd?: string;

/**
* The modes to load the environment variables for.
*/
readonly modes: string[];
}

/**
* Loads environment variables from `.env` and `.env.local` files.
* Loads environment variables from:
* - `.env`
* - `.env.local`
* - `.env.{mode}`
* - `.env.{mode}.local`
*/
export function loadEnvVariables(
options?: EnvLoadOptions
): Record<string, string> | undefined {
const envDir = options?.cwd ?? process.cwd();
const envFiles = DEFAULT_ENV_FILES.map((file) => path.join(envDir, file));
const envFiles = [
`.env`,
`.env.local`,
...(options?.modes.flatMap((mode) => [
`.env.${mode}`,
`.env.${mode}.local`,
]) ?? []),
].map((file) => path.join(envDir, file));

// Parse `envFiles` and combine their variables into a single object
const parsed = Object.fromEntries(
Expand Down
26 changes: 26 additions & 0 deletions libs/wingsdk/test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("loadEnvVariables", () => {

const loaded = loadEnvVariables({
cwd: tempdir,
modes: ["test"],
})!;

expect(loaded).toBeDefined();
Expand All @@ -28,10 +29,35 @@ describe("loadEnvVariables", () => {

const loaded = loadEnvVariables({
cwd: tempdir,
modes: ["test"],
})!;

expect(loaded).toBeDefined();
expect(loaded.BASE_THING).toBe("hi");
expect(loaded.OTHER_THING).toBe("hi_wing");
});

it("can load local and mode env files", async () => {
const tempdir = await mkdtemp(join(tmpdir(), "env-test"));

await writeFile(`${tempdir}/.env`, "TEST1=1\n");
await writeFile(`${tempdir}/.env.local`, "TEST2=2\n");
await writeFile(`${tempdir}/.env.run`, "TEST3=3\n");
await writeFile(`${tempdir}/.env.run.local`, "TEST4=4\n");
await writeFile(`${tempdir}/.env.it`, "TEST5=5\n");
await writeFile(`${tempdir}/.env.it.local`, "TEST6=6\n");

const loaded = loadEnvVariables({
cwd: tempdir,
modes: ["run", "it"],
})!;

expect(loaded).toBeDefined();
expect(loaded.TEST1).toBe("1");
expect(loaded.TEST2).toBe("2");
expect(loaded.TEST3).toBe("3");
expect(loaded.TEST4).toBe("4");
expect(loaded.TEST5).toBe("5");
expect(loaded.TEST6).toBe("6");
});
});

0 comments on commit 1dc08f5

Please sign in to comment.