简体   繁体   English

Nx - 使用运行命令生成器时出现“无法写入引用...”错误

[英]Nx - 'Unable to write a reference...' error when using run-commands builder

I am getting a peculiar error with building a lib that has a dependency on another lib in the same Nx workspace.我在构建一个依赖于同一 Nx 工作区中的另一个库的库时遇到了一个特殊错误。 I am using the run-commands builder as there are some other tasks that need to be invoked after the initial library build.我正在使用运行命令构建器,因为在初始库构建之后需要调用其他一些任务。

I am seeing 'Unable to write a reference...' type error message, eg我看到“无法编写参考...”类型错误消息,例如

Unable to write a reference to SomethingCoolComponent in /Users/robert.parker/Documents/Github/nx-playground/libs/common-components/src/lib/components/something-cool/something-cool.component.ts from /Users/robert.parker/Documents/Github/nx-playground/libs/common-components/src/lib/common-components.module.ts
ERROR: Something went wrong in @nrwl/run-commands - Command failed: nx base-build shiny-components

I have a replicable repo where I can showcase here https://github.com/parky128/nx-playground using a couple of minimal libs with a dependency between the two.我有一个可复制的仓库,我可以在这里展示https://github.com/parky128/nx-playground使用几个最小的库,两者之间存在依赖关系。

After installing dependencies, just run ng run shiny-components:build and see the error emitted.安装依赖项后,只需运行ng run shiny-components:build并查看发出的错误。

If I don't use the run-commands builder, and just run ng run shiny-components:base-build task on the same lib it builds just fine, so I am blaming run-commands but I am unsure why it's breaking.如果我不使用run-commands构建器,而只是在同一个库上运行ng run shiny-components:base-build任务,它构建得很好,所以我责怪 run-commands 但我不确定它为什么会中断。

I have seen this answer thats related to the same issue, although they don't seem to be using run-commands and for them was down to a path import issue, but I don't think thats cause for my issue here.我已经看到与同一问题相关的这个答案,尽管他们似乎没有使用运行命令并且对他们来说归结为路径导入问题,但我认为这不是我的问题的原因。

The issue here is that you introduced a brand new target name base-build which nx doesn't know how to build.这里的问题是您引入了一个全新的目标名称base-build ,而 nx 不知道如何构建。

There are two issues:有两个问题:

Luckily, you can configure your workpace properly:幸运的是,您可以正确配置工作区:

  1. Update targetDependencies option in nx.json :更新targetDependencies中的nx.json选项:

     "targetDependencies": { "build": [ { "target": "build", "projects": "dependencies" } ], "base-build": [ <== add this { "target": "base-build", "projects": "dependencies" } ] },
  2. Rename or introduce the same target name for common-components in angular.json :angular.json中的 common-components 重命名或引入相同的目标名称:

     "common-components": { "projectType": "library", "root": "libs/common-components", "sourceRoot": "libs/common-components/src", "prefix": "rob", "architect": { "base-build": { <========================= this line "builder": "@nrwl/angular:package", "outputs": ["dist/libs/common-components"],

Update更新

I think there is even better approach: don't rename common targets ( build , lint , test etc) but instead rename your custom target only我认为还有更好的方法:不要重命名常见目标buildlinttest等) ,而是只重命名您的自定义目标

angular.json angular.json

"build-with-task": {
      "builder": "@nrwl/workspace:run-commands",
      "options": {
          "commands": [
              "nx build shiny-components",
              "echo done"
          ],

Now, you should be able to run one of these commands:现在,您应该能够运行以下命令之一:

ng run shiny-components:build
ng run shiny-components:build-with-task

Here's the final PR for your repo https://github.com/parky128/nx-playground/pull/1/files这是您的 repo https://github.com/parky128/nx-playground/pull/1/files的最终 PR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 nx 与 angular.json 一起使用时使用哪个构建器 - Which builder to use when using nx with angular.json 为什么我在使用任何 `nx` 命令时都会出错 - Why am i getting error on using any `nx` commands 将 Nx 与 PrimeNG 表组件一起使用时出错 - Error when using Nx with PrimeNG table component 使用 IntelliJ 在 NX Workspace 中运行 Jest 测试时出现“错误:未找到预设 jest.preset.js” - "Error: Preset jest.preset.js not found" when using IntelliJ to run Jest tests in NX Workspace 使用 NX 命令创建 angular 库时出现 SchematicsException - SchematicsException when creating an angular library with NX Commands 如何在 Angular/nx 工作区的上下文中运行 nestjs-console 命令 - How to run nestjs-console commands in context of Angular / nx workspace nx migrate - 运行 nx migrate --run-migration 时跳过 npm 安装 - nx migrate - Skip npm install when running nx migrate --run-migration Angular 10:无法编写参考 - Angular 10: Unable to write a reference 使用“nx migrate”在 nx 工作区中从 Angular 13 升级到 14 后生成 Angular 组件时出错 - Error while generating Angular component after upgrading from Angular 13 to 14 in nx workspace using `nx migrate` 创建新的应用程序和库时出现@ nrwl / nx错误 - @nrwl/nx error when creating new apps and libs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM