Skip to content

FatalDiagnosticError is being throw instead of it being converted to ats.Diagnostic #44999

Closed

Description

@jorroll

🐞 Bug report

Command (mark with an x)

  • build

Is this a regression?

Unknown. I am in the process of updating from Angular 10.x (where things build fine) to 13.x (where things are breaking), but it's unclear if this is a regression.

Description

I have two Angular library projects in a monorepo. Project B depends on Project A. Both are pretty small. When attempting to build both, Project A builds successfully but Project B fails with the following error:

Schema validation failed with the following errors:
  Data path "/error" must be string.

It's unclear what schema the error is referring to and it's also unclear what Data path "/error" is referring to. If "schema" refers to angular.json in this context, it's unclear what Data path "/error" is referring to since an "error" key isn't documented for angular.json.

🔬 Minimal Reproduction

Clone the angular-cli-build-bug branch of my service-work/is-loading repo. The repo contains a .devcontainer.json file which can be used to quickly setup the dev environment.

  1. run yarn install
  2. (optional) run yarn test. Tests should all pass.
  3. run yarn build. The @service-work/is-loading project should build successfully but the @service-work/scroll-position library should fail with the following message:
    Schema validation failed with the following errors:
      Data path "/error" must be string.
    

Note that the @service-work/scroll-position package depends on the @service-work/is-loading package. However, the only file in @service-work/scroll-position that actually imports from @service-work/is-loading is the projects/scroll-position/src/scroll-position/module/directive/scroll-position.directive.ts file. If you comment out the @service-work/is-loading import in that file, as well as the related code, then the @service-work/scroll-position file builds without error.

It's unclear if there's something wrong in my code which is preventing the project from building (in which case the provided error message should be improved to make clear what the problem is) or if there's a bug with the build script.

🔥 Exception or Error


Schema validation failed with the following errors:
  Data path "/error" must be string.

🌍 Your Environment


Angular CLI: 13.2.2
Node: 12.22.7
Package Manager: yarn 1.22.15
OS: linux x64

Angular: 13.2.1
... animations, common, compiler, compiler-cli, core
... language-service, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1302.2
@angular-devkit/build-angular   13.2.2
@angular-devkit/core            13.2.2
@angular-devkit/schematics      13.2.2
@angular/cli                    13.2.2
@schematics/angular             13.2.2
ng-packagr                      13.2.1
rxjs                            6.6.7
typescript                      4.5.5

Anything else relevant?

  • This repo is managed as a Yarn 1.x workspace.
  • I don't think this is actually relevant, but I'll mention that my development computer is a M1 Pro Macbook Pro that is running an x86_64 devcontainer using qemu.

Activity

  1. alan-agius4 commented on Feb 7, 2022

    @alan-agius4
    Contributor

    There are two parts of this problem.

    misconfiguration
    The root cause of the error is that the project is misconfigured. The TypeScript compiler is unable to resolve @service-work/is-loading. This can be resolved by setting as path mappings in your tsconfig.

    {
      "compileOnSave": false,
      "compilerOptions": {
        ...
        "paths": {
          "@service-work/is-loading": ["projects/is-loading/build"]
        }
      }
    }

    compiler-cli

    The compiler-cli is surfacing this particular error as a FatalDiagnosticError, which should not happen and therefore this is also an issue in the compiler-cli, which should converted this to a ts.Diagnostic internally.

    FatalDiagnosticError {
      code: 3004,
      node: <ref *1> SourceFileObject {
        pos: 0,
        end: 0,
        flags: 0,
        modifierFlagsCache: 0,
        transformFlags: 0,
        parent: undefined,
        kind: 303,
        statements: [ pos: 0, end: 0, hasTrailingComma: false, transformFlags: 0 ],
        endOfFileToken: TokenObject {
          pos: 0,
          end: 0,
          flags: 0,
          modifierFlagsCache: 0,
          transformFlags: 0,
          parent: [Circular *1],
          kind: 1
        },
        fileName: '/is-loading/projects/is-loading/src/is-loading/module/directive/is-loading-spinner.component.ngtypecheck.ts',
        text: '',
        languageVersion: 99,
        languageVariant: 0,
        scriptKind: 3,
        isDeclarationFile: false,
        hasNoDefaultLib: false,
        externalModuleIndicator: undefined,
        bindDiagnostics: [],
        bindSuggestionDiagnostics: undefined,
        pragmas: Map(0) {},
        checkJsDirective: undefined,
        referencedFiles: [],
        typeReferenceDirectives: [],
        libReferenceDirectives: [],
        amdDependencies: [],
        commentDirectives: undefined,
        nodeCount: 2,
        identifierCount: 0,
        identifiers: Map(0) {},
        parseDiagnostics: []
      },
      message: {
        category: 3,
        code: 0,
        messageText: 'Unable to import symbol IsLoadingSpinnerComponent.',
        next: [ [Object] ]
      },
      relatedInformation: [
        {
          category: 3,
          code: 0,
          file: [SourceFileObject],
          start: 75,
          length: 305,
          messageText: 'The symbol is declared here.'
        }
      ],
      _isFatalDiagnosticError: true
    }
    
    
  2. self-assigned this
    on Feb 7, 2022
  3. changed the title [-]`Data path "/error" must be string` build failure[/-] [+]FatalDiagnosticError is being throw instead of it being converted to a`ts.Diagnostic`[/+] on Feb 7, 2022
  4. added
    area: compiler
    P3
    on Feb 7, 2022
  5. modified the milestone: Backlog on Feb 7, 2022
  6. changed the title [-]FatalDiagnosticError is being throw instead of it being converted to a`ts.Diagnostic`[/-] [+]`FatalDiagnosticError` is being throw instead of it being converted to a`ts.Diagnostic`[/+] on Feb 7, 2022
  7. jorroll commented on Feb 8, 2022

    @jorroll
    ContributorAuthor

    Thanks for tracking this down alan-agius! I can confirm that adding

    {
      "compilerOptions": {
        ...
        "paths": {
          "@service-work/is-loading": ["projects/is-loading/build"]
        }
      }
    }

    Does fix the build problem. I'm surprised that it is needed though. Already, because this is a yarn workspace, both @service-work/is-loading and @service-work/scroll-position are symlinked in the node_modules folder (though the symlinks point to the raw source rather than the "build" output). It's for this reason that the tests run without error when the scroll-position library imports the is-loading library. Why is the "build" version of is-loading needed in order to build scroll-position? Is this documented somewhere? Since is-loading is a peer dependency, it's not being included in scroll-position anyway and so, I expected, it was only used for typings during the build process.

    As an aside, it's strange but I never got a notification that a comment was added to this issue (I've looked back through all my recently "done" notifications and confirmed that nothing exists for this issue). I wonder if it's related to the fact that the issue was moved from one repo to another.

  8. IanTheG commented on Feb 9, 2022

    @IanTheG

    I was having a similar issue when trying to build my shared-components library when also using an Angular workspace to hold a few Angular apps. Unlike the @service-work/is-loading and @service-work/scroll-position packages, I wanted my library to only be used locally.

    I'm also using Angular 13.2.

    my-workspace git:(template) ✗ ng build shared-components
    Building Angular Package
    
    ------------------------------------------------------------------------------
    Building entry point 'shared-components'
    ------------------------------------------------------------------------------
    ✖ Compiling with Angular sources in Ivy partial compilation mode.
    Schema validation failed with the following errors:
      Data path "/error" must be string.
    

    The error was caused by two things (that I think only happened because I was using a workspace):

    1. public-api.ts file was contained within a projects folder that lived adjacently to the lib folder and inside src
    2. The export statements in the public-api.ts file were incorrect, giving the error Cannot find module... or its corresponding type declarations error

    Moving the file outside the folder, correcting the exports, and renaming the entryFile field in the ng-package.json solved the issue. For me, no other changes to ts.config.json files were needed.

    To use my library, I first built it using ng build shared-components --watch and then imported into my module like this:

    import { SharedComponentsModule } from 'shared-components'
    // other imports...
    
    const COMPONENTS = [ /* components to use across app */ ]
    const ANGULAR_MODULES = [CommonModule, RouterModule]
    
    const MODULES = [
      ...ANGULAR_MODULES,
      SharedComponentsModule,
      // other modules...
    ]
    
    @NgModule({
      declarations: [...COMPONENTS],
      // declarations: [...COMPONENTS, ...PIPES],
      exports: [...COMPONENTS, ...MODULES],
      providers: [],
      imports: [...MODULES],
    })
    export class ComponentsModule {}

    Then I can use any component, service, etc. in any place in my app!

    I'm not sure why Angular would generate the public-api.ts file inside a folder when using workspaces. I'd hoped to have it work out the box, but it cost me an hour or two of problem solving.

  9. jorroll commented on Feb 10, 2022

    @jorroll
    ContributorAuthor

    @IanTheG It's a little unclear because you haven't included a reproduction, but it sounds like you are providing another example of a (different) error message being suppressed by the compiler-cli. It sounds like this is also incorrectly resulting in a FatalDiagnosticError as described by alan-agius above.

    I'm not sure why Angular would generate the public-api.ts file inside a folder when using workspaces. I'd hoped to have it work out the box, but it cost me an hour or two of problem solving.

    Personally, I find this comment to be confusing. You seem to be implying that, because a file named public-api.ts is inside a folder your build was failing. But, in theory, so long as the relevant configuration files are setup correctly (i.e. angular.json, tsconfig.json, ng-package.json, etc), a library's entry file named public-api.ts (or something else, because it can be named anything) should have no problem being located inside a folder. If you look at my Angular IsLoadingService repo (which, thanks to alen-arguis' help, is now building properly), you'll see that the entry points for the libraries are located within folders. If you're saying that you generated a new angular library using the CLI, and that new library was not generated with the proper configuration, that sounds like a separate issue.

  10. PhiLhoSoft commented on Feb 14, 2022

    @PhiLhoSoft

    @alan-agius4, is there a way to get a dump of FatalDiagnosticError object, like you show?
    I have the same error, probably hiding some other error, and I would like to fix the problem without waiting for the next release.
    Thank you.

  11. PhiLhoSoft commented on Feb 14, 2022

    @PhiLhoSoft

    Thanks.
    I translated it to: <project>/node_modules/@angular-devkit/build-angular/src/builders/ng-packagr/index.js
    And I changed it as follows:
    (line 61)

        })()).pipe(
            (0, operators_1.switchMap)(({ packager, ngPackagrOptions }) => options.watch ? packager.watch(ngPackagrOptions) : packager.build(ngPackagrOptions)),
            (0, operators_1.mapTo)({ success: true }),
            (0, operators_1.catchError)((err) => {
                console.error('Error', err);
                return (0, rxjs_1.of)({ success: false, error: err.message });
            })
        );
    

    I got an error, but it remains cryptic (Unable to import symbol AddressbookAvatarComponent; this symbol was imported OK before…)
    That's a problem I had in the past (Angular 11 I think), not sure why it resurfaced.
    I fear I can't provide a reproduction, so I am on my own.

    Thanks anyway, it was useful!

  12. PhiLhoSoft commented on Mar 4, 2022

    @PhiLhoSoft

    Update: I had this error after adding a component to the library project.
    I had another look at the component, and finally spotted the culprits: I routinely import classes by typing their name, then hitting Enter on the suggestion made by VSC.
    But I didn't see it made import { SomeClass } from 'api'; where there is no definition of this api thing.
    It should have been import { SomeClass } from 'the-current-project/api'; because in tsconfig.json, I defined in the paths section: 'the-current-project/*': ['./*'],
    And then, it worked…

    Note 1: the 'Unable to import symbol AddressbookAvatarComponent.' message is totally unrelated to the new component which doesn't use this one. This is why it was hard to track…
    Note 2: beware, sometime VSC also makes import { Foo ] from '../'; (or from './';) because there is an index.ts file at this location (it is used to export models, services, modules, components, etc. Barrel files.). These imports creates subtle bugs too, perhaps cyclic dependencies. Change them to import { Foo ] from '../foo.ts'; for example.

  13. kkoo95 commented on May 4, 2022

    @kkoo95

    So is it some kind of regression ? TS path mapping must point to build library directory no matter what ? Has it always be liked this ? Isn't Nx capable of something like this ? What's the magic behind it then ? (so many questions...)

    A future use case to this is being capable of mapping to symlinked sources. This could be really helpful for companies which have lots of separated VCS repo but where all those projects need each other (hence mimicking some kind of monorepo)

  14. added a commit that references this issue on May 22, 2022
    e702a96
  15. added a commit that references this issue on Jun 10, 2022
    b92c1a6
  16. angular-automatic-lock-bot commented on Jul 11, 2022

    @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.

  17. locked and limited conversation to collaborators on Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: compilerbugstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions