[cli] Don't pull system environment vars in dev (#11526)

System environment variables would get set with empty strings in development which breaks some builds. This fixes that by using the v2 of `/env/pull` introduced in https://github.com/vercel/api/pull/27777.
This commit is contained in:
Austin Merrick
2024-05-15 05:17:47 -07:00
committed by GitHub
parent d3c1267e24
commit 446ac49e2b
5 changed files with 68 additions and 20 deletions

View File

@@ -0,0 +1,5 @@
---
'vercel': minor
---
Stop sending system environment variables in dev

View File

@@ -70,7 +70,7 @@ export async function pullEnvRecords(
); );
const query = new URLSearchParams(); const query = new URLSearchParams();
let url = `/v1/env/pull/${projectId}`; let url = `/v2/env/pull/${projectId}`;
if (target) { if (target) {
url += `/${encodeURIComponent(target)}`; url += `/${encodeURIComponent(target)}`;

View File

@@ -917,9 +917,9 @@ test('Deploy `api-env` fixture and test `vercel env` command', async () => {
expect(homeJson['MY_STDIN_VAR']).toBe('{"expect":"quotes"}'); expect(homeJson['MY_STDIN_VAR']).toBe('{"expect":"quotes"}');
expect(homeJson['MY_DECRYPTABLE_SECRET_ENV']).toBe('decryptable value'); expect(homeJson['MY_DECRYPTABLE_SECRET_ENV']).toBe('decryptable value');
// system env vars are automatically exposed // system env vars are hidden in dev
expect(apiJson['VERCEL']).toBe('1'); expect(apiJson['VERCEL']).toBeUndefined();
expect(homeJson['VERCEL']).toBe('1'); expect(homeJson['VERCEL']).toBeUndefined();
// sleep before kill, otherwise the dev process doesn't clean up and exit properly // sleep before kill, otherwise the dev process doesn't clean up and exit properly
await sleep(100); await sleep(100);
@@ -949,7 +949,7 @@ test('Deploy `api-env` fixture and test `vercel env` command', async () => {
async function vcEnvPullFetchSystemVars() { async function vcEnvPullFetchSystemVars() {
const { exitCode, stdout, stderr } = await execCli( const { exitCode, stdout, stderr } = await execCli(
binaryPath, binaryPath,
['env', 'pull', '-y'], ['env', 'pull', '-y', '--environment', 'production'],
{ {
cwd: target, cwd: target,
} }
@@ -963,7 +963,7 @@ test('Deploy `api-env` fixture and test `vercel env` command', async () => {
expect(lines).toContain('VERCEL="1"'); expect(lines).toContain('VERCEL="1"');
expect(lines).toContain('VERCEL_URL=""'); expect(lines).toContain('VERCEL_URL=""');
expect(lines).toContain('VERCEL_ENV="development"'); expect(lines).toContain('VERCEL_ENV="production"');
expect(lines).toContain('VERCEL_GIT_PROVIDER=""'); expect(lines).toContain('VERCEL_GIT_PROVIDER=""');
expect(lines).toContain('VERCEL_GIT_REPO_SLUG=""'); expect(lines).toContain('VERCEL_GIT_REPO_SLUG=""');
} }
@@ -980,22 +980,24 @@ test('Deploy `api-env` fixture and test `vercel env` command', async () => {
const localhostNoProtocol = localhost[0].slice('http://'.length); const localhostNoProtocol = localhost[0].slice('http://'.length);
const apiJson = await apiRes.json(); const apiJson = await apiRes.json();
expect(apiJson['VERCEL']).toBe('1'); // environment variables are not set in dev
expect(apiJson['VERCEL']).toBeUndefined();
expect(apiJson['VERCEL_ENV']).toBeUndefined();
expect(apiJson['VERCEL_GIT_PROVIDER']).toBeUndefined();
expect(apiJson['VERCEL_GIT_REPO_SLUG']).toBeUndefined();
// except for these because vc dev
expect(apiJson['VERCEL_URL']).toBe(localhostNoProtocol); expect(apiJson['VERCEL_URL']).toBe(localhostNoProtocol);
expect(apiJson['VERCEL_ENV']).toBe('development');
expect(apiJson['VERCEL_REGION']).toBe('dev1'); expect(apiJson['VERCEL_REGION']).toBe('dev1');
expect(apiJson['VERCEL_GIT_PROVIDER']).toBe('');
expect(apiJson['VERCEL_GIT_REPO_SLUG']).toBe('');
const homeUrl = localhost[0]; const homeUrl = localhost[0];
const homeRes = await fetch(homeUrl); const homeRes = await fetch(homeUrl);
const homeJson = await homeRes.json(); const homeJson = await homeRes.json();
expect(homeJson['VERCEL']).toBe('1'); expect(homeJson['VERCEL']).toBeUndefined();
expect(homeJson['VERCEL_URL']).toBe(localhostNoProtocol); expect(homeJson['VERCEL_URL']).toBe(localhostNoProtocol);
expect(homeJson['VERCEL_ENV']).toBe('development'); expect(homeJson['VERCEL_ENV']).toBeUndefined();
expect(homeJson['VERCEL_REGION']).toBe(undefined); expect(homeJson['VERCEL_REGION']).toBeUndefined();
expect(homeJson['VERCEL_GIT_PROVIDER']).toBe(''); expect(homeJson['VERCEL_GIT_PROVIDER']).toBeUndefined();
expect(homeJson['VERCEL_GIT_REPO_SLUG']).toBe(''); expect(homeJson['VERCEL_GIT_REPO_SLUG']).toBeUndefined();
// sleep before kill, otherwise the dev process doesn't clean up and exit properly // sleep before kill, otherwise the dev process doesn't clean up and exit properly
await sleep(100); await sleep(100);

View File

@@ -216,7 +216,7 @@ export function useProject(
res.json(project); res.json(project);
}); });
client.scenario.get( client.scenario.get(
`/v1/env/pull/${project.id}/:target?/:gitBranch?`, `/v2/env/pull/${project.id}/:target?/:gitBranch?`,
(req, res) => { (req, res) => {
const target = const target =
typeof req.params.target === 'string' typeof req.params.target === 'string'
@@ -390,7 +390,7 @@ function exposeSystemEnvs(
) { ) {
const envs: Env = {}; const envs: Env = {};
if (autoExposeSystemEnvs) { if (autoExposeSystemEnvs && target !== 'development') {
envs['VERCEL'] = '1'; envs['VERCEL'] = '1';
envs['VERCEL_ENV'] = target || 'development'; envs['VERCEL_ENV'] = target || 'development';

View File

@@ -205,10 +205,17 @@ describe('env', () => {
}); });
const cwd = setupUnitFixture('vercel-env-pull'); const cwd = setupUnitFixture('vercel-env-pull');
client.cwd = cwd; client.cwd = cwd;
client.setArgv('env', 'pull', 'other.env', '--yes'); client.setArgv(
'env',
'pull',
'other.env',
'--yes',
'--environment',
'production'
);
const exitCodePromise = env(client); const exitCodePromise = env(client);
await expect(client.stderr).toOutput( await expect(client.stderr).toOutput(
'Downloading `development` Environment Variables for Project vercel-env-pull' 'Downloading `production` Environment Variables for Project vercel-env-pull'
); );
await expect(client.stderr).toOutput('Created other.env file'); await expect(client.stderr).toOutput('Created other.env file');
await expect(client.stderr).not.toOutput('and added it to .gitignore'); await expect(client.stderr).not.toOutput('and added it to .gitignore');
@@ -218,10 +225,44 @@ describe('env', () => {
const productionFileHasVercelEnv = rawDevEnv const productionFileHasVercelEnv = rawDevEnv
.toString() .toString()
.includes('VERCEL_ENV="development"'); .includes('VERCEL_ENV="production"');
expect(productionFileHasVercelEnv).toBeTruthy(); expect(productionFileHasVercelEnv).toBeTruthy();
}); });
it('should not expose system env variables in dev', async () => {
useUser();
useTeams('team_dummy');
useProject({
...defaultProject,
id: 'vercel-env-pull',
name: 'vercel-env-pull',
autoExposeSystemEnvs: true,
});
const cwd = setupUnitFixture('vercel-env-pull');
client.cwd = cwd;
client.setArgv('env', 'pull', 'other.env', '--yes');
const exitCodePromise = env(client);
await expect(client.stderr).toOutput(
'Downloading `development` Environment Variables for Project vercel-env-pull'
);
await expect(client.stderr).toOutput('Created other.env file');
await expect(client.stderr).not.toOutput('and added it to .gitignore');
await expect(exitCodePromise).resolves.toEqual(0);
const devEnv = (
await fs.readFile(path.join(cwd, 'other.env'))
).toString();
const devFileHasVercelEnv = [
'VERCEL',
'VERCEL_ENV',
'VERCEL_URL',
'VERCEL_REGION',
'VERCEL_DEPLOYMENT_ID',
].some(envVar => devEnv.includes(envVar));
expect(devFileHasVercelEnv).toBeFalsy();
});
it('should show a delta string', async () => { it('should show a delta string', async () => {
const cwd = setupUnitFixture('vercel-env-pull-delta'); const cwd = setupUnitFixture('vercel-env-pull-delta');
client.cwd = cwd; client.cwd = cwd;