Skip to content

Commit 52f968e

Browse files
committed
make env override env file, not the other way around
1 parent 8917074 commit 52f968e

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

sources/specUtils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
156156
data: any;
157157
manifestPath: string;
158158
envFilePath?: string;
159-
localEnv?: LocalEnvFile;
159+
localEnv: LocalEnvFile;
160160
} | null = null;
161161

162162
while (nextCwd !== currCwd && (!selection || !selection.data.packageManager)) {
@@ -184,15 +184,17 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
184184
if (typeof data !== `object` || data === null)
185185
throw new UsageError(`Invalid package.json in ${path.relative(initialCwd, manifestPath)}`);
186186

187-
let localEnv: LocalEnvFile | undefined;
188-
const envFilePath = path.join(currCwd, `.corepack.env`);
187+
let localEnv: LocalEnvFile;
188+
const envFilePath = path.resolve(currCwd, `./.corepack.env`);
189189
debugUtils.log(`Checking ${envFilePath}`);
190190
try {
191-
localEnv = {...process.env, ...parseEnv(await fs.promises.readFile(envFilePath, `utf8`))};
191+
localEnv = {...parseEnv(await fs.promises.readFile(envFilePath, `utf8`)), ...process.env};
192+
debugUtils.log(`Successfully loaded env file found at ${envFilePath}`);
192193
} catch (err) {
193194
if ((err as NodeError)?.code !== `ENOENT`)
194195
throw err;
195196

197+
debugUtils.log(`No env file found at ${envFilePath}`);
196198
localEnv = process.env;
197199
}
198200

tests/main.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,47 @@ it(`should use hash from "packageManager" even when "devEngines" defines a diffe
294294
});
295295
});
296296

297+
it(`should use hash from env even when "devEngines" defines a different one`, async () => {
298+
await xfs.mktempPromise(async cwd => {
299+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as PortablePath), {
300+
devEngines: {
301+
packageManager: {
302+
name: `yarn`,
303+
version: `3.0.0-rc.2+sha224.f83f6d1cbfac10ba6b516a62ccd2a72ccd857aa6c514d1cd7185ec60`,
304+
},
305+
},
306+
});
307+
308+
process.env.COREPACK_DEV_ENGINES_YARN = `3.0.0-rc.2+sha224.deadbeef`;
309+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
310+
exitCode: 1,
311+
stderr: /Mismatch hashes. Expected deadbeef, got f83f6d1cbfac10ba6b516a62ccd2a72ccd857aa6c514d1cd7185ec60/,
312+
stdout: ``,
313+
});
314+
});
315+
});
316+
317+
it(`should use hash from env even when ".corepack.env" defines a different one`, async () => {
318+
await xfs.mktempPromise(async cwd => {
319+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as PortablePath), {
320+
devEngines: {
321+
packageManager: {
322+
name: `yarn`,
323+
version: `3.0.0-rc.2`,
324+
},
325+
},
326+
});
327+
await xfs.writeFilePromise(ppath.join(cwd, `.corepack.env` as PortablePath), `COREPACK_DEV_ENGINES_YARN=3.0.0-rc.2+sha1.bedabb1e\n`);
328+
329+
process.env.COREPACK_DEV_ENGINES_YARN = `3.0.0-rc.2+sha224.deadbeef`;
330+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
331+
exitCode: 1,
332+
stderr: /Mismatch hashes. Expected bedabb1e, got 694bdad81703169e203febd57f9dc97d3be867bd/,
333+
stdout: ``,
334+
});
335+
});
336+
});
337+
297338
describe(`should accept range in devEngines only if a specific version is provided`, () => {
298339
it(`either in .corepack.env`, async() => {
299340
await xfs.mktempPromise(async cwd => {

0 commit comments

Comments
 (0)