Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .bumpy/fix-builtin-var-type-preservation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
varlock: patch
---

Fix typed builtin vars (e.g. boolean VARLOCK_IS_CI) being stringified when referenced from root decorators like @import/@initOp, which broke not()/if() logic
6 changes: 6 additions & 0 deletions packages/varlock/src/env-graph/lib/env-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ export class EnvGraph {
const BuiltinVarResolver = createResolver({
name: `\0builtin:${key}`,
description: builtinDef.description,
// Advertise the builtin's declared type so that if the item gets a
// process() call (e.g. when registered early via a root-decorator
// reference), config-item type inference preserves it instead of
// defaulting back to 'string' — which would stringify a boolean/number
// builtin (e.g. VARLOCK_IS_CI false -> "false", breaking not()/if()).
inferredType: builtinType,
async resolve() {
return builtinDef.resolver(graph.ciEnvInfo, graph.processEnvForBuiltins);
},
Expand Down
38 changes: 38 additions & 0 deletions packages/varlock/src/env-graph/test/builtin-vars.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
describe, test, vi, expect,
} from 'vitest';
import outdent from 'outdent';
import { envFilesTest } from './helpers/generic-test';

// We need to mock the child_process module used by builtin-vars.ts
Expand Down Expand Up @@ -43,6 +44,43 @@ describe('VARLOCK_* builtin variables', () => {
}));
});

describe('type preservation', () => {
// Regression: when a typed builtin (e.g. boolean VARLOCK_IS_CI) is registered
// early via a root-decorator reference, the finishLoad process() pass used to
// recompute its type and default it back to 'string', stringifying `false` to
// "false" — which made not()/if() see a truthy string. The builtin resolver now
// advertises its declared type via inferredType so the type is preserved.
test('boolean builtin referenced in a root decorator keeps its boolean type', envFilesTest({
files: {
'.env.schema': outdent`
# @import(./.env.imported, enabled=not($VARLOCK_IS_CI))
# ---
LOCAL_VAR=local
`,
'.env.imported': 'IMPORTED_VAR=imported',
},
processEnv: {},
expectValues: {
VARLOCK_IS_CI: false, // boolean false, not the string "false"
IMPORTED_VAR: 'imported', // import is enabled because not(false) === true
},
}));

test('same root-decorator reference disables the import when in CI', envFilesTest({
files: {
'.env.schema': outdent`
# @import(./.env.imported, enabled=not($VARLOCK_IS_CI))
# ---
LOCAL_VAR=local
`,
'.env.imported': 'IMPORTED_VAR=imported',
},
processEnv: { CI: 'true', GITHUB_ACTIONS: 'true' },
expectValues: { VARLOCK_IS_CI: true },
expectNotInSchema: ['IMPORTED_VAR'],
}));
});

describe('VARLOCK_ENV environment detection', () => {
test('detects test environment from NODE_ENV=test', envFilesTest({
envFile: 'MY_ENV=$VARLOCK_ENV',
Expand Down
Loading