From 6e280a3e6ad2a92ef22916570411ba37909b63fa Mon Sep 17 00:00:00 2001 From: Aleksander Katan Date: Mon, 15 Dec 2025 18:12:24 +0100 Subject: [PATCH 1/4] Update error message --- packages/typegpu/src/tgsl/wgslGenerator.ts | 2 +- packages/typegpu/tests/constant.test.ts | 2 +- packages/typegpu/tests/tgslFn.test.ts | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/typegpu/src/tgsl/wgslGenerator.ts b/packages/typegpu/src/tgsl/wgslGenerator.ts index 0267213e65..354b20d4de 100644 --- a/packages/typegpu/src/tgsl/wgslGenerator.ts +++ b/packages/typegpu/src/tgsl/wgslGenerator.ts @@ -296,7 +296,7 @@ ${this.ctx.pre}}`; convLhs.origin === 'runtime-tgpu-const-ref' ) { throw new WgslTypeError( - `'${lhsStr} = ${rhsStr}' is invalid, because ${lhsStr} is a constant.`, + `'${lhsStr} = ${rhsStr}' is invalid, because ${lhsStr} is a constant. This error may also occur when accidentally assigning to a value defined outside of a TypeGPU function.`, ); } diff --git a/packages/typegpu/tests/constant.test.ts b/packages/typegpu/tests/constant.test.ts index 392ebcc11b..b8058e66b8 100644 --- a/packages/typegpu/tests/constant.test.ts +++ b/packages/typegpu/tests/constant.test.ts @@ -83,7 +83,7 @@ describe('tgpu.const', () => { [Error: Resolution of the following tree failed: - - fn*:fn - - fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant.] + - fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant. This error may also occur when accidentally assigning to a value defined outside of a TypeGPU function.] `); // Since we freeze the object, we cannot mutate when running the function in JS either diff --git a/packages/typegpu/tests/tgslFn.test.ts b/packages/typegpu/tests/tgslFn.test.ts index ebd1aedb72..5a8629287b 100644 --- a/packages/typegpu/tests/tgslFn.test.ts +++ b/packages/typegpu/tests/tgslFn.test.ts @@ -1007,4 +1007,17 @@ describe('tgsl fn when using plugin', () => { }" `); }); + + it('throws a readable error when assigning to a value defined outside of tgsl', () => { + let a = 0; + const f = tgpu.fn([])(() => { + a = 2; + }); + + expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(` + [Error: Resolution of the following tree failed: + - + - fn:f: '0 = 2' is invalid, because 0 is a constant. This error may also occur when accidentally assigning to a value defined outside of a TypeGPU function.] + `); + }); }); From 74a4fc77a63f8b69c22af4033ab01b55a4999683 Mon Sep 17 00:00:00 2001 From: Aleksander Katan Date: Mon, 15 Dec 2025 18:22:53 +0100 Subject: [PATCH 2/4] Update the message again --- packages/typegpu/src/tgsl/wgslGenerator.ts | 2 +- packages/typegpu/tests/constant.test.ts | 2 +- packages/typegpu/tests/tgslFn.test.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/typegpu/src/tgsl/wgslGenerator.ts b/packages/typegpu/src/tgsl/wgslGenerator.ts index 354b20d4de..43e6e7017e 100644 --- a/packages/typegpu/src/tgsl/wgslGenerator.ts +++ b/packages/typegpu/src/tgsl/wgslGenerator.ts @@ -296,7 +296,7 @@ ${this.ctx.pre}}`; convLhs.origin === 'runtime-tgpu-const-ref' ) { throw new WgslTypeError( - `'${lhsStr} = ${rhsStr}' is invalid, because ${lhsStr} is a constant. This error may also occur when accidentally assigning to a value defined outside of a TypeGPU function.`, + `'${lhsStr} = ${rhsStr}' is invalid, because ${lhsStr} is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.`, ); } diff --git a/packages/typegpu/tests/constant.test.ts b/packages/typegpu/tests/constant.test.ts index b8058e66b8..817b9b646a 100644 --- a/packages/typegpu/tests/constant.test.ts +++ b/packages/typegpu/tests/constant.test.ts @@ -83,7 +83,7 @@ describe('tgpu.const', () => { [Error: Resolution of the following tree failed: - - fn*:fn - - fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant. This error may also occur when accidentally assigning to a value defined outside of a TypeGPU function.] + - fn*:fn(): Cannot assign to 'boid.pos' because it is a constant. If 'boid.pos' is defined outside the GPU function scope, consider declaring it with 'var' or 'let' inside the function instead.] `); // Since we freeze the object, we cannot mutate when running the function in JS either diff --git a/packages/typegpu/tests/tgslFn.test.ts b/packages/typegpu/tests/tgslFn.test.ts index 5a8629287b..2534c7cb59 100644 --- a/packages/typegpu/tests/tgslFn.test.ts +++ b/packages/typegpu/tests/tgslFn.test.ts @@ -1017,7 +1017,7 @@ describe('tgsl fn when using plugin', () => { expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(` [Error: Resolution of the following tree failed: - - - fn:f: '0 = 2' is invalid, because 0 is a constant. This error may also occur when accidentally assigning to a value defined outside of a TypeGPU function.] + - fn:f: Cannot assign to '0' because it is a constant. If '0' is defined outside the GPU function scope, consider declaring it with 'var' or 'let' inside the function instead.] `); }); }); From 5117e2aca5789b2e64ed7c4c6f4c68eb65814a16 Mon Sep 17 00:00:00 2001 From: Aleksander Katan Date: Mon, 15 Dec 2025 18:29:51 +0100 Subject: [PATCH 3/4] Update tests --- packages/typegpu/tests/constant.test.ts | 2 +- packages/typegpu/tests/tgslFn.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/typegpu/tests/constant.test.ts b/packages/typegpu/tests/constant.test.ts index 817b9b646a..2ddc6cab35 100644 --- a/packages/typegpu/tests/constant.test.ts +++ b/packages/typegpu/tests/constant.test.ts @@ -83,7 +83,7 @@ describe('tgpu.const', () => { [Error: Resolution of the following tree failed: - - fn*:fn - - fn*:fn(): Cannot assign to 'boid.pos' because it is a constant. If 'boid.pos' is defined outside the GPU function scope, consider declaring it with 'var' or 'let' inside the function instead.] + - fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.] `); // Since we freeze the object, we cannot mutate when running the function in JS either diff --git a/packages/typegpu/tests/tgslFn.test.ts b/packages/typegpu/tests/tgslFn.test.ts index 2534c7cb59..f608033991 100644 --- a/packages/typegpu/tests/tgslFn.test.ts +++ b/packages/typegpu/tests/tgslFn.test.ts @@ -1017,7 +1017,7 @@ describe('tgsl fn when using plugin', () => { expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(` [Error: Resolution of the following tree failed: - - - fn:f: Cannot assign to '0' because it is a constant. If '0' is defined outside the GPU function scope, consider declaring it with 'var' or 'let' inside the function instead.] + - fn:f: '0 = 2' is invalid, because 0 is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.] `); }); }); From 419548fd83c8297619cd1bb1a0ee69de40eee503 Mon Sep 17 00:00:00 2001 From: Aleksander Katan Date: Thu, 18 Dec 2025 14:56:29 +0100 Subject: [PATCH 4/4] Update error message --- packages/typegpu/tests/constant.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typegpu/tests/constant.test.ts b/packages/typegpu/tests/constant.test.ts index f41258591b..f5dffb0638 100644 --- a/packages/typegpu/tests/constant.test.ts +++ b/packages/typegpu/tests/constant.test.ts @@ -158,7 +158,7 @@ describe('tgpu.const', () => { [Error: Resolution of the following tree failed: - - fn*:fn - - fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant.] + - fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.] `); // Since we freeze the object, we cannot mutate when running the function in JS either