question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Assignment of a outer scope variable in a closure failing when optimization enabled

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

No

Description

Assignment of a outer scope variable in a closure, that works perfectly in development mode, it’s failing when optimization are enabled.

This error can be reproduced following the next steps:

  1. Define a string variable with a default value.
  2. Update it in a closure.
  3. Just after the closure, use +=to concatenate something the variable.

The variable, at the end, contains the initial value followed by the concatenated one. The value set in the closure is lost.

I’ve been able to reproduce this issue with latest versions of angular 13 and 14 when optimization is enabled ("optimization": true) but not with angular 15.

Example of failing code:

  problem(): string {
    let url = 'If you see this, there is a problem.';
    let query = (() => {
      url = 'If you see this, all is right.';
      console.log('Updating URL (1): ' + url);
      return '';
    })();
    url += query;
    console.log('Updating URL (2): ' + url);
    return url;
  }

Please provide a link to a minimal reproduction of the bug

https://github.com/josepm9/ng-optimization-enabled-issue

Please provide the exception or error you saw

Please use the example (master or release/angular14 branch) to reproduce the error.

1) Start the example (`npm run start`)
2) Browse the main page (http://localhost:4200/)

Expected page content:

Angular 15.0.4
Problem: If you see this, all is right.
No problem: If you see this, all is right.

Actual page content:

Angular 13.3.12
Problem: If you see this, there is a problem.
No problem: If you see this, all is right.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 13.3.10
Node: 16.16.0
Package Manager: npm 8.11.0
OS: linux x64

Angular: 13.3.12
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.10
@angular-devkit/build-angular   13.3.10
@angular-devkit/core            13.3.10
@angular-devkit/schematics      13.3.10
@angular/cli                    13.3.10
@schematics/angular             13.3.10
rxjs                            7.8.0
typescript                      4.6.4

Anything else?

I’ve been able to reproduce this issue with latest versions of angular 13 and 14 when optimization is enabled ("optimization": true) but not with angular 15.

Angular 14 environment:

Angular CLI: 14.2.10
Node: 16.16.0
Package Manager: npm 8.11.0 
OS: linux x64

Angular: 14.2.12
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.10
@angular-devkit/build-angular   14.2.10
@angular-devkit/core            14.2.10
@angular-devkit/schematics      14.2.10
@angular/cli                    14.2.10
@schematics/angular             14.2.10
rxjs                            7.8.0
typescript                      4.8.4

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
alan-agius4commented, Dec 22, 2022

Version 14 is in LTS and only security fixes are applied. Please see: https://angular.io/guide/releases#lts-fixes

Closing as this issue is not reproducible using the current latest version 15

1reaction
alan-agius4commented, Dec 21, 2022

@josepm9, this seem to be solved in version 15. Please update to the more recent version of Angular.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referencing local variable before assignment results in ...
My impression was that outer(3) should return 3, but it actually returns 4. Somehow the code in the inner() block assigns value 4...
Read more >
Python closure function losing outer variable access
When Python parses a function, it notes whenever it finds a variable used on the left-hand side of an assignment, such as param1...
Read more >
Grokking V8 closures for fun (and profit?) - Vyacheslav Egorov
Optimizing compiler will not be able to allocate such variables to registers and each load and store will become a memory operation. [There...
Read more >
Closures — The Swift Programming Language (Swift 5.7)
Swift's closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios.
Read more >
Understanding the Restrictions Imposed by the Closure ...
The Simple optimization level renames function parameters, local variables, and locally defined functions to reduce code size. However, some ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found