Skip to content

WritableSignal.set is not available in PipeTransform function with NG0600 error.  #49967

Closed

Description

@leo6104

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

core

Is this a regression?

No

Description

I know that Signal API is still developer-preview. If it should be go to discussion part, let me know.

I read all Signal RFC & trying to use Signal for our translation library.

We forked @ngx-translate/core and customize its structure into Signal-based pipe.
https://gist.github.com/leo6104/1d8da771f1cb3da237ca3ba06fe4fdea#file-translate-pipe-ts

And it shows error message that is out of control range from developer. It would be some point that can be improved (show other error message or support WritableSignal.set in Pipe transform function.)

Please provide a link to a minimal reproduction of the bug

Here is minimal reproduction code from the library code.

import { ChangeDetectorRef, effect, EnvironmentInjector, Pipe, runInInjectionContext, signal } from '@angular/core';

@Pipe({
  name: 'test',
  standalone: true,
})
export class TestPipe {
  private value = signal<string>('');
  private lastKey = signal<string>(null);

  constructor() {
    effect((onCleanup) => {
      const key = this.lastKey();

      this.value.set(key + ' world');
    }, { allowSignalWrites: true });
  }

  transform(_query: string | number, ...args: any[]): any {
    const query = (typeof _query === 'number') ? `${_query}` : _query;
    this.lastKey.set(query);
    return this.value();
  }
}

@Component({
   selector: 'app',
   template: `{{ 'hello' | test }}`,
})
export AppComponent {
}

With above code, i thought it should print hello world but it raise error. we cannot give allowSignalWrites to true because we didn't use computed/effect in transform function.

Please provide the exception or error you saw

ERROR Error: NG0600: Writing to signals is not allowed in a `computed` or an `effect` by default. Use `allowSignalWrites` in the `CreateEffectOptions` to enable this inside effects.
    at core.mjs:26566:15
    at throwInvalidWriteToSignalError (core.mjs:2549:5)
    at WritableSignalImpl.set (core.mjs:2584:13)
    at TestPipe.transform (test.pipe.ts:37:18)
    at pureFunction1Internal (core.mjs:22308:65)
    at ɵɵpipeBind1 (core.mjs:22494:9)
    at RootCmp_Template (template.html:15:1)
    at ReactiveLViewConsumer.runInContext (core.mjs:10310:13)
    at executeTemplate (core.mjs:11069:22)
    at refreshView (core.mjs:10952:13)
h


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

```true
Angular CLI: 16.0.0-rc.1
Node: 16.19.0
Package Manager: npm 8.19.3
OS: darwin x64

Angular: 16.1.0-next.0+sha-7ba01b4
... core

Package                             Version
-------------------------------------------------------------
@angular-devkit/architect           0.1600.0-rc.1
@angular-devkit/build-angular       16.0.0-rc.1
@angular-devkit/core                16.0.0-rc.1
@angular-devkit/schematics          16.0.0-rc.1
@angular/animations                 16.0.0-rc.2
@angular/bazel                      15.2.6
@angular/cdk                        16.0.0-rc.1
@angular/cli                        16.0.0-rc.1
@angular/common                     16.0.0-rc.2
@angular/compiler                   16.0.0-rc.2
@angular/compiler-cli               16.0.0-rc.2
@angular/elements                   16.0.0-rc.2
@angular/forms                      16.0.0-rc.2
@angular/localize                   16.0.0-rc.2
@angular/material                   16.0.0-rc.1
@angular/platform-browser           16.0.0-rc.2
@angular/platform-browser-dynamic   16.0.0-rc.2
@angular/platform-server            16.0.0-rc.2
@angular/router                     16.0.0-rc.2
@angular/service-worker             16.0.0-rc.2
@angular/youtube-player             16.0.0-next.2
@nguniversal/builders               16.0.0-next.0
@nguniversal/common                 16.0.0-next.0
@nguniversal/express-engine         16.0.0-next.0
@schematics/angular                 16.0.0-rc.1
ng-packagr                          16.0.0-rc.1
rxjs                                7.8.0
typescript                          5.0.3

Anything else?

Tested in platformBrowserDynamic().bootstrapModule. Not bootstrapApplication with standalone AppComponent

Activity

  1. pkozlowski-opensource commented on Apr 24, 2023

    @pkozlowski-opensource
    Member

    I'm sorry but it is not clear to me what you are trying to achieve here.

    Generally speaking:

    • we do discourage write to signals in effects, I'm not sure why effect is needed here in the first place;
    • it is not clear to me why a pipe is used here.

    Could you please explain what you are trying to achieve and share a minimal, runnable example in stackblitz? Thnx!

  2. added
    needs: clarification
    on Apr 24, 2023
  3. pkozlowski-opensource commented on Apr 24, 2023

    @pkozlowski-opensource
    Member

    Moreover, it might be the cases that effect is not needed here at all, why not using computed?

    export class TestPipe {
    
      private key = signal<string>('');
      private value = computed(() => this.key() + ' world');
      
      transform(_query: string | number, ...args: any[]): any {
        const query = (typeof _query === 'number') ? `${_query}` : _query;
        this.key.set(query);
        return this.value();
      }
    }

    Not sure what value a pipe adds here but again, I don't think I understand the scenario here...

  4. JeanMeche commented on Apr 24, 2023

    @JeanMeche
    Member
  5. pkozlowski-opensource commented on Apr 24, 2023

    @pkozlowski-opensource
    Member

    Thnx @JeanMeche - I think I know what is going on. This is the use-case that requires a bit more thinking IMO.

    The thing is that pipe is normally used as a data synchronization mechanism. Basically a "smarter getter" called in a template. So the question here is: should we allow such use-cases in the transform part of the pipe?

    It is really equivalent to writing to signal from a getter called in a template and it makes reasoning about data flow very hard. My initial reaction here is that the error thrown here actually is catching a problematic usage. But as I've said it needs more deliberation.

  6. added
    area: core
    needs: discussion
    and removed
    needs: clarification
    on Apr 24, 2023
  7. added this to the Backlog milestone on Apr 24, 2023
  8. JeanMeche commented on Apr 24, 2023

    @JeanMeche
    Member

    That being said, the error message probably needs to be rephrased as it can happen outside of effect/computed !

  9. pkozlowski-opensource commented on Apr 24, 2023

    @pkozlowski-opensource
    Member

    That being said, the error message probably needs to be rephrased as it can happen outside of effect/computed !

    It actually does happen in an effect (one created by the framework to detect signal reads in a template). But I do agree that the error is not obvious and the error needs improvement.

  10. leo6104 commented on Apr 24, 2023

    @leo6104
    ContributorAuthor

    @JeanMeche @pkozlowski-opensource Thanks for comment.

    we do discourage write to signals in effects, I'm not sure why effect is needed here in the first place;

    I didn't catch the thing that signal was discouraged in effect. Thanks for the information.

    it is not clear to me why a pipe is used here.

    The pipe i tried to implement was translate pipe which support runtime translation function.
    When translate pipe called, it lazily load i18n files from network and transform its value to localized text.

    https://gist.github.com/leo6104/1d8da771f1cb3da237ca3ba06fe4fdea#file-translate-pipe-ts-L37-L41

    It needs to call Observable and wait for json response. It is not sync behavior so i use effects.

    It can be one example that someone can thinking like this implementation through signal / effect / computed 3 functions.

    I fully understand that it is difficult to support in framework-level.

    If it should be go to discussion part, let me know.

    As i mentioned in first section in this issue, feel free to close the issue if it should be go to discussion part. 😅

    Thanks for having discussion about this implementation.

  11. pkozlowski-opensource commented on Apr 24, 2023

    @pkozlowski-opensource
    Member

    Thnx for sharing more info @leo6104 , appreciate it.

    It needs to call Observable and wait for json response. It is not sync behavior so i use effects.
    It can be one example that someone can thinking like this implementation through signal / effect / computed 3 functions.

    This is an interesting use-case but somehow specialized so I'm not sure if this is the best educational example. In any case we are going to discuss it more in the team.

  12. leo6104 commented on Apr 24, 2023

    @leo6104
    ContributorAuthor

    I close this issue because of my misunderstanding about current state of Signal + zone-based change detection.

    I thought it is possible to use pipe with returning signal value in zone-based component and i expect that pipe/component will tracking the changes of signals. But it wasn't. After i tested it with this stackblitz i made, i realize that it doesn't trigger change detection when signal value changed.

    I found the discussion #49682 (reply in thread) and i guess it will be only working on Signal-based component. They already have a question about pipe which return signal value in transform function. I will follow-up in the RFC comment. The questions exactly same what i have. So, this issue is duplicate.

    I really look forward to improving rendering performance/code complexity/learning curves with signal.

    Really thanks for making great things in Angular. @pkozlowski-opensource @JeanMeche and the teams.

  13. angular-automatic-lock-bot commented on May 25, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecross-cutting: signalsneeds: discussion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions