mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-06 12:57:46 +00:00
[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:
5
.changeset/light-cherries-relax.md
Normal file
5
.changeset/light-cherries-relax.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'vercel': minor
|
||||
---
|
||||
|
||||
Stop sending system environment variables in dev
|
||||
2
packages/cli/src/util/env/get-env-records.ts
vendored
2
packages/cli/src/util/env/get-env-records.ts
vendored
@@ -70,7 +70,7 @@ export async function pullEnvRecords(
|
||||
);
|
||||
const query = new URLSearchParams();
|
||||
|
||||
let url = `/v1/env/pull/${projectId}`;
|
||||
let url = `/v2/env/pull/${projectId}`;
|
||||
|
||||
if (target) {
|
||||
url += `/${encodeURIComponent(target)}`;
|
||||
|
||||
30
packages/cli/test/integration-1.test.ts
vendored
30
packages/cli/test/integration-1.test.ts
vendored
@@ -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_DECRYPTABLE_SECRET_ENV']).toBe('decryptable value');
|
||||
|
||||
// system env vars are automatically exposed
|
||||
expect(apiJson['VERCEL']).toBe('1');
|
||||
expect(homeJson['VERCEL']).toBe('1');
|
||||
// system env vars are hidden in dev
|
||||
expect(apiJson['VERCEL']).toBeUndefined();
|
||||
expect(homeJson['VERCEL']).toBeUndefined();
|
||||
|
||||
// sleep before kill, otherwise the dev process doesn't clean up and exit properly
|
||||
await sleep(100);
|
||||
@@ -949,7 +949,7 @@ test('Deploy `api-env` fixture and test `vercel env` command', async () => {
|
||||
async function vcEnvPullFetchSystemVars() {
|
||||
const { exitCode, stdout, stderr } = await execCli(
|
||||
binaryPath,
|
||||
['env', 'pull', '-y'],
|
||||
['env', 'pull', '-y', '--environment', 'production'],
|
||||
{
|
||||
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_URL=""');
|
||||
expect(lines).toContain('VERCEL_ENV="development"');
|
||||
expect(lines).toContain('VERCEL_ENV="production"');
|
||||
expect(lines).toContain('VERCEL_GIT_PROVIDER=""');
|
||||
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 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_ENV']).toBe('development');
|
||||
expect(apiJson['VERCEL_REGION']).toBe('dev1');
|
||||
expect(apiJson['VERCEL_GIT_PROVIDER']).toBe('');
|
||||
expect(apiJson['VERCEL_GIT_REPO_SLUG']).toBe('');
|
||||
|
||||
const homeUrl = localhost[0];
|
||||
const homeRes = await fetch(homeUrl);
|
||||
const homeJson = await homeRes.json();
|
||||
expect(homeJson['VERCEL']).toBe('1');
|
||||
expect(homeJson['VERCEL']).toBeUndefined();
|
||||
expect(homeJson['VERCEL_URL']).toBe(localhostNoProtocol);
|
||||
expect(homeJson['VERCEL_ENV']).toBe('development');
|
||||
expect(homeJson['VERCEL_REGION']).toBe(undefined);
|
||||
expect(homeJson['VERCEL_GIT_PROVIDER']).toBe('');
|
||||
expect(homeJson['VERCEL_GIT_REPO_SLUG']).toBe('');
|
||||
expect(homeJson['VERCEL_ENV']).toBeUndefined();
|
||||
expect(homeJson['VERCEL_REGION']).toBeUndefined();
|
||||
expect(homeJson['VERCEL_GIT_PROVIDER']).toBeUndefined();
|
||||
expect(homeJson['VERCEL_GIT_REPO_SLUG']).toBeUndefined();
|
||||
|
||||
// sleep before kill, otherwise the dev process doesn't clean up and exit properly
|
||||
await sleep(100);
|
||||
|
||||
@@ -216,7 +216,7 @@ export function useProject(
|
||||
res.json(project);
|
||||
});
|
||||
client.scenario.get(
|
||||
`/v1/env/pull/${project.id}/:target?/:gitBranch?`,
|
||||
`/v2/env/pull/${project.id}/:target?/:gitBranch?`,
|
||||
(req, res) => {
|
||||
const target =
|
||||
typeof req.params.target === 'string'
|
||||
@@ -390,7 +390,7 @@ function exposeSystemEnvs(
|
||||
) {
|
||||
const envs: Env = {};
|
||||
|
||||
if (autoExposeSystemEnvs) {
|
||||
if (autoExposeSystemEnvs && target !== 'development') {
|
||||
envs['VERCEL'] = '1';
|
||||
envs['VERCEL_ENV'] = target || 'development';
|
||||
|
||||
|
||||
@@ -205,10 +205,17 @@ describe('env', () => {
|
||||
});
|
||||
const cwd = setupUnitFixture('vercel-env-pull');
|
||||
client.cwd = cwd;
|
||||
client.setArgv('env', 'pull', 'other.env', '--yes');
|
||||
client.setArgv(
|
||||
'env',
|
||||
'pull',
|
||||
'other.env',
|
||||
'--yes',
|
||||
'--environment',
|
||||
'production'
|
||||
);
|
||||
const exitCodePromise = env(client);
|
||||
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).not.toOutput('and added it to .gitignore');
|
||||
@@ -218,10 +225,44 @@ describe('env', () => {
|
||||
|
||||
const productionFileHasVercelEnv = rawDevEnv
|
||||
.toString()
|
||||
.includes('VERCEL_ENV="development"');
|
||||
.includes('VERCEL_ENV="production"');
|
||||
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 () => {
|
||||
const cwd = setupUnitFixture('vercel-env-pull-delta');
|
||||
client.cwd = cwd;
|
||||
|
||||
Reference in New Issue
Block a user