Skip to content

Commit 9128703

Browse files
authored
Handle mutations of process.env (#6664)
1 parent 0ddf73e commit 9128703

File tree

13 files changed

+428
-121
lines changed

13 files changed

+428
-121
lines changed

packages/core/integration-tests/test/html.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1242,9 +1242,7 @@ describe('html', function() {
12421242
],
12431243
},
12441244
],
1245-
hints: [
1246-
'Add type="module" as a second argument to the <script> tag.',
1247-
],
1245+
hints: ['Add the type="module" attribute to the <script> tag.'],
12481246
},
12491247
]);
12501248

@@ -1523,9 +1521,7 @@ describe('html', function() {
15231521
],
15241522
},
15251523
],
1526-
hints: [
1527-
'Add type="module" as a second argument to the <script> tag.',
1528-
],
1524+
hints: ['Add the type="module" attribute to the <script> tag.'],
15291525
},
15301526
]);
15311527

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
let name = "ABC";
2-
process.env[name] = "abc";
32
module.exports = process.env[name];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
process.env.SOMETHING = "foo";
2+
process.env.SOMETHING += "foo";
3+
delete process.env.SOMETHING;
4+
process.env.SOMETHING++;

packages/core/integration-tests/test/integration/env-mutate/node_modules/foo/index.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'foo';

packages/core/integration-tests/test/javascript.js

+207-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '@parcel/test-utils';
1818
import {makeDeferredWithPromise, normalizePath} from '@parcel/utils';
1919
import vm from 'vm';
20+
import Logger from '@parcel/logger';
2021

2122
describe('javascript', function() {
2223
beforeEach(async () => {
@@ -2703,15 +2704,15 @@ describe('javascript', function() {
27032704
let b = await bundle(
27042705
path.join(__dirname, '/integration/env-computed/index.js'),
27052706
{
2706-
env: {name: 'abc'},
2707+
env: {ABC: 'abc'},
27072708
},
27082709
);
27092710

27102711
let contents = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
27112712
assert(contents.includes('process.env'));
27122713

27132714
let output = await run(b);
2714-
assert.strictEqual(output, 'abc');
2715+
assert.strictEqual(output, undefined);
27152716
});
27162717

27172718
it('should inline computed accesses with string literals to process.env', async function() {
@@ -2807,6 +2808,210 @@ describe('javascript', function() {
28072808
assert.equal(output, 'productiontest');
28082809
});
28092810

2811+
it('should error on process.env mutations', async function() {
2812+
let filePath = path.join(__dirname, '/integration/env-mutate/index.js');
2813+
await assert.rejects(bundle(filePath), {
2814+
diagnostics: [
2815+
{
2816+
origin: '@parcel/transformer-js',
2817+
message: 'Mutating process.env is not supported',
2818+
hints: null,
2819+
codeFrames: [
2820+
{
2821+
filePath,
2822+
codeHighlights: [
2823+
{
2824+
message: null,
2825+
start: {
2826+
line: 1,
2827+
column: 1,
2828+
},
2829+
end: {
2830+
line: 1,
2831+
column: 29,
2832+
},
2833+
},
2834+
],
2835+
},
2836+
],
2837+
},
2838+
{
2839+
origin: '@parcel/transformer-js',
2840+
message: 'Mutating process.env is not supported',
2841+
hints: null,
2842+
codeFrames: [
2843+
{
2844+
filePath,
2845+
codeHighlights: [
2846+
{
2847+
message: null,
2848+
start: {
2849+
line: 2,
2850+
column: 1,
2851+
},
2852+
end: {
2853+
line: 2,
2854+
column: 30,
2855+
},
2856+
},
2857+
],
2858+
},
2859+
],
2860+
},
2861+
{
2862+
origin: '@parcel/transformer-js',
2863+
message: 'Mutating process.env is not supported',
2864+
hints: null,
2865+
codeFrames: [
2866+
{
2867+
filePath,
2868+
codeHighlights: [
2869+
{
2870+
message: null,
2871+
start: {
2872+
line: 3,
2873+
column: 1,
2874+
},
2875+
end: {
2876+
line: 3,
2877+
column: 28,
2878+
},
2879+
},
2880+
],
2881+
},
2882+
],
2883+
},
2884+
{
2885+
origin: '@parcel/transformer-js',
2886+
message: 'Mutating process.env is not supported',
2887+
hints: null,
2888+
codeFrames: [
2889+
{
2890+
filePath,
2891+
codeHighlights: [
2892+
{
2893+
message: null,
2894+
start: {
2895+
line: 4,
2896+
column: 1,
2897+
},
2898+
end: {
2899+
line: 4,
2900+
column: 23,
2901+
},
2902+
},
2903+
],
2904+
},
2905+
],
2906+
},
2907+
],
2908+
});
2909+
});
2910+
2911+
it('should warn on process.env mutations in node_modules', async function() {
2912+
let logs = [];
2913+
let disposable = Logger.onLog(d => logs.push(d));
2914+
let b = await bundle(
2915+
path.join(__dirname, '/integration/env-mutate/warn.js'),
2916+
);
2917+
disposable.dispose();
2918+
2919+
assert.deepEqual(logs, [
2920+
{
2921+
type: 'log',
2922+
level: 'warn',
2923+
diagnostics: [
2924+
{
2925+
origin: '@parcel/transformer-js',
2926+
message: 'Mutating process.env is not supported',
2927+
hints: null,
2928+
codeFrames: [
2929+
{
2930+
filePath: path.join(
2931+
__dirname,
2932+
'/integration/env-mutate/node_modules/foo/index.js',
2933+
),
2934+
codeHighlights: [
2935+
{
2936+
message: null,
2937+
start: {
2938+
line: 1,
2939+
column: 8,
2940+
},
2941+
end: {
2942+
line: 1,
2943+
column: 36,
2944+
},
2945+
},
2946+
],
2947+
},
2948+
],
2949+
},
2950+
{
2951+
origin: '@parcel/transformer-js',
2952+
message: 'Mutating process.env is not supported',
2953+
hints: null,
2954+
codeFrames: [
2955+
{
2956+
filePath: path.join(
2957+
__dirname,
2958+
'/integration/env-mutate/node_modules/foo/index.js',
2959+
),
2960+
codeHighlights: [
2961+
{
2962+
message: null,
2963+
start: {
2964+
line: 2,
2965+
column: 8,
2966+
},
2967+
end: {
2968+
line: 2,
2969+
column: 35,
2970+
},
2971+
},
2972+
],
2973+
},
2974+
],
2975+
},
2976+
{
2977+
origin: '@parcel/transformer-js',
2978+
message: 'Mutating process.env is not supported',
2979+
hints: null,
2980+
codeFrames: [
2981+
{
2982+
filePath: path.join(
2983+
__dirname,
2984+
'/integration/env-mutate/node_modules/foo/index.js',
2985+
),
2986+
codeHighlights: [
2987+
{
2988+
message: null,
2989+
start: {
2990+
line: 3,
2991+
column: 8,
2992+
},
2993+
end: {
2994+
line: 3,
2995+
column: 30,
2996+
},
2997+
},
2998+
],
2999+
},
3000+
],
3001+
},
3002+
],
3003+
},
3004+
]);
3005+
3006+
let output = [];
3007+
await run(b, {
3008+
output(o) {
3009+
output.push(o);
3010+
},
3011+
});
3012+
assert.deepEqual(output, ['foo', true, undefined]);
3013+
});
3014+
28103015
it('should replace process.browser for target browser', async function() {
28113016
let b = await bundle(
28123017
path.join(__dirname, '/integration/process/index.js'),

packages/transformers/js/core/src/dependency_collector.rs

+5
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl<'a> DependencyCollector<'a> {
206206
}]),
207207
hints: None,
208208
show_environment: true,
209+
severity: DiagnosticSeverity::Error,
209210
});
210211
}
211212
}
@@ -380,6 +381,7 @@ impl<'a> Fold for DependencyCollector<'a> {
380381
"Use a static `import`, or dynamic `import()` instead.",
381382
)]),
382383
show_environment: self.config.source_type == SourceType::Script,
384+
severity: DiagnosticSeverity::Error,
383385
});
384386
}
385387

@@ -549,6 +551,7 @@ impl<'a> Fold for DependencyCollector<'a> {
549551
str_.value,
550552
)]),
551553
show_environment: false,
554+
severity: DiagnosticSeverity::Error,
552555
});
553556
return node;
554557
} else {
@@ -719,6 +722,7 @@ impl<'a> Fold for DependencyCollector<'a> {
719722
str_.value
720723
)]),
721724
show_environment: false,
725+
severity: DiagnosticSeverity::Error,
722726
});
723727
return node;
724728
} else {
@@ -1221,6 +1225,7 @@ impl<'a> DependencyCollector<'a> {
12211225
}]),
12221226
hints: None,
12231227
show_environment: true,
1228+
severity: DiagnosticSeverity::Error,
12241229
})
12251230
}
12261231
true

0 commit comments

Comments
 (0)