Skip to content

Commit 71bfa43

Browse files
committed
Verify disabled sources in injected-env reuse too
@disable lives in the source's own content, so an edit to a disabled file can re-enable it. An edit that leaves it disabled just costs one harmless re-resolution.
1 parent b04f3a5 commit 71bfa43

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

packages/varlock/src/lib/injected-env-reuse.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,18 @@ export function evaluateInjectedEnvReuse(opts: {
202202
}
203203

204204
// Source drift: the producer fingerprints the contents of each file source it actually
205-
// parsed (see getSerializedGraph). If an enabled source has been edited or removed since
206-
// the blob was made (e.g. an env file edit followed by a dev-server restart inside the
207-
// same `varlock run`), reuse would serve pre-edit values - re-resolve instead. Disabled
208-
// sources can't affect the resolved values (and whatever disabled them is covered by the
209-
// ambient-env drift check below), so they're skipped. Known gap: a matching env file
210-
// *created* after the blob won't appear in its sources list and isn't detected here.
205+
// parsed (see getSerializedGraph). If a source has been edited or removed since the blob
206+
// was made (e.g. an env file edit followed by a dev-server restart inside the same
207+
// `varlock run`), reuse could serve pre-edit values - re-resolve instead. Disabled
208+
// sources are verified too: `@disable` lives in the source's own content, so an edit can
209+
// re-enable it (an edit that leaves it disabled just costs one harmless re-resolution).
210+
// Known gap: a matching env file *created* after the blob won't appear in its sources
211+
// list and isn't detected here.
211212
if (!Array.isArray(parsedEnv.sources)) {
212213
return { reuse: false, reason: 'blob has no sources recorded' };
213214
}
214215
for (const source of parsedEnv.sources) {
215-
if (!source.enabled || source.path === undefined) continue;
216+
if (source.path === undefined) continue;
216217
// older producers didn't record fingerprints - we can't verify, so re-resolve
217218
if (!source.contentHash) {
218219
return { reuse: false, reason: `blob has no content fingerprint for source ${source.path}` };

packages/varlock/src/lib/test/injected-env-reuse.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,27 @@ describe('evaluateInjectedEnvReuse', () => {
217217
expect(decision).toMatchObject({ reuse: false, reason: expect.stringContaining('fingerprint') });
218218
});
219219

220-
test('a changed disabled source does not block reuse', () => {
221-
// a disabled file cannot affect resolved values; whatever disabled it is ambient
222-
// env, which the env-drift check covers separately
223-
const source = writeSourceFile('.env.production', 'FOO=prod-val\n');
220+
test('an unchanged disabled source does not block reuse', () => {
221+
const source = writeSourceFile('.env.production', '# @disable\nFOO=prod-val\n');
224222
source.enabled = false;
225-
fs.writeFileSync(path.join(tempDir, '.env.production'), 'FOO=edited\n');
226223
const decision = evaluateInjectedEnvReuse({
227224
env: { __VARLOCK_ENV: makeBlob({ sources: [source] }) },
228225
cwd: tempDir,
229226
});
230227
expect(decision.reuse).toBe(true);
231228
});
232229

230+
test('a changed disabled source blocks reuse (editing can remove @disable)', () => {
231+
const source = writeSourceFile('.env.production', '# @disable\nFOO=prod-val\n');
232+
source.enabled = false;
233+
fs.writeFileSync(path.join(tempDir, '.env.production'), 'FOO=prod-val\n');
234+
const decision = evaluateInjectedEnvReuse({
235+
env: { __VARLOCK_ENV: makeBlob({ sources: [source] }) },
236+
cwd: tempDir,
237+
});
238+
expect(decision).toMatchObject({ reuse: false, reason: expect.stringContaining('changed since') });
239+
});
240+
233241
test('non-file sources (no path) are skipped', () => {
234242
const decision = evaluateInjectedEnvReuse({
235243
env: { __VARLOCK_ENV: makeBlob({ sources: [{ type: 'processEnv', label: 'process env', enabled: true }] }) },

0 commit comments

Comments
 (0)