build icon indicating copy to clipboard operation
build copied to clipboard

how to use `build_runner` in pub workspaces?

Open kjxbyz opened this issue 10 months ago • 20 comments

  1. The following code must be added to the root pubspec.yaml file
dev_dependencies:
  build_runner: ^2.4.14

Otherwise, the following error will be reported

dart pub -C modules/basic/assets run build_runner build --delete-conflicting-outputs
Deprecated. Use `dart run` instead.
Package "build_runner" is not an immediate dependency.
Cannot run executables in transitive dependencies.
make: *** [asset] Error 65
  1. Run the following command in the root directory. Code cannot be generated in the module directory.
dart pub -C modules/basic/assets run build_runner build --delete-conflicting-outputs
Image

kjxbyz avatar Jan 26 '25 14:01 kjxbyz

build_runner does not have any direct support for pub workspaces - you need to still use it as normal (add the dependency to each sub-packages pubspec etc).

It does support workspaces in the sense that it still works, but it doesn't try to share work across the workspace or anything like that, and each package still has to have the dependency.

jakemac53 avatar Jan 27 '25 18:01 jakemac53

I thought we had an open issue for some better support here but I don't see one, so we can leave this open to track that.

jakemac53 avatar Jan 27 '25 18:01 jakemac53

Image

I'm having this issue while trying to generate the .g file where I'm getting 0 outputs. Am I missing something? tbh I dunno if is a build_runner problem or riverpod :/

ThiagoEvoa avatar Mar 08 '25 17:03 ThiagoEvoa

Restart the IDE. It's usually just the IDE that's out of sync

rrousselGit avatar Mar 08 '25 19:03 rrousselGit

Unfortunately it's not :( I've being tying a lot o things since. Actually I created a non workspace project to see if the problem persists, the result is that works like a charm in this new project.

ThiagoEvoa avatar Mar 09 '25 09:03 ThiagoEvoa

A quick update, after adding the dev dependencies to this "package_core" (see the image), btw the packages was already added to the global pubspec.yaml project... I was able to generate the .g file. After that I removed the dev dependencies from the package pubspec.yaml file and it's generating the .g file still. Maybe something related to sync or references as @rrousselGit suggested... but didn't solve restarting my IDE.

If you don't want to have problems... add all generation dev dependencies as @jakemac53 said.

ThiagoEvoa avatar Mar 09 '25 11:03 ThiagoEvoa

I faced the same limitation and wrote a quick solution for my problem. Maybe it helps you also: https://pub.dev/packages/workspace_scripts

Summarized it starts X processes (one per workspace project) with the specific command you need.

devtronic avatar Mar 26 '25 18:03 devtronic

I have similar problem.

App builds fine but analyzer does not see generated files. If i copy the /app/.dart_tool/build folder to the root /.dart_tool/. Analyzer is ok.

this blocks me from switching. Can i fix this with config? Or this must be done in core build_runner? Or is this analyzer problem?

I am happy to help

genesistms avatar Jun 05 '25 05:06 genesistms

I have similar problem.

App builds fine but analyzer does not see generated files. If i copy the /app/.dart_tool/build folder to the root /.dart_tool/. Analyzer is ok.

Which generator(s) are you using? Could you give an example of a generated file that the analyzer does not see to start with then sees when you move it?

davidmorgan avatar Jun 05 '25 07:06 davidmorgan

When I run into issues with the builder—usually after changing dependencies (commonly when switching Git branches)—I typically run:

dart pub upgrade --tighten --unlock-transitive

dart run build_runner clean && dart run build_runner build --delete-conflicting-outputs

gmpassos avatar Jun 05 '25 08:06 gmpassos

Which generator(s) are you using? Could you give an example of a generated file that the analyzer does not see to start with then sees when you move it?

I made simple example project for this. Take a look. @davidmorgan

https://github.com/genesistms/dart-ws-test

genesistms avatar Jun 05 '25 09:06 genesistms

Which generator(s) are you using? Could you give an example of a generated file that the analyzer does not see to start with then sees when you move it?

I made simple example project for this. Take a look. @davidmorgan

https://github.com/genesistms/dart-ws-test

Hmmm I think a/pubspec.yaml should have

resolution: workspace

when it's used in a workspace; and then you want to do pub get in the workspace root to set up the workspace fully. (that will create a .dart_tool folder for the workspace).

davidmorgan avatar Jun 05 '25 09:06 davidmorgan

Hmmm I think a/pubspec.yaml should have

resolution: workspace

when it's used in a workspace; and then you want to do pub get in the workspace root to set up the workspace fully. (that will create a .dart_tool folder for the workspace).

In workspace it should not matter where i run pub get. But i tried, still analyzer has errors...

In the README (in my example repo) there are steps to take. With workspace and without.

genesistms avatar Jun 05 '25 10:06 genesistms

In the README (in my example repo) there are steps to take. With workspace and without.

Okay, I understood now, the example really helped, thank you :)

Your generator is outputting files to the .dart_tool directory.

Most generators don't do this for .dart files ... there is apparently some support for it in the analyzer, which is why your first analyze works. But I still think it doesn't work everywhere: I just tried dart compile exe main.dart and even when the analyzer works, dart compile doesn't, it can't find the files.

Generators usually set build_to: source in build.yaml so the output is next to the checked in source code. Then all the tools work.

Regardless, there are two issues here:

  • The analyzer's support for finding generated files apparently does not work in workspaces, which is why your second analyze fails
  • It's a bit weird that analyze has this support at all given that compile doesn't

I will raise both of these with analyzer+compiler teams. In the meantime, will setting output to source work for your use case?

davidmorgan avatar Jun 05 '25 11:06 davidmorgan

I will raise both of these with analyzer+compiler teams.

Thanks for that. :)

In the meantime, will setting output to source work for your use case?

It would work... yes.

genesistms avatar Jun 05 '25 12:06 genesistms

Just to note...

In order to migrate to workspace we needed to change builder output to source. It has drawback that generated files are only generated in root package. So we need to spawn multiple build_runner watch for each root package and for each dependency package. (now i understand why @devtronic created worskpace_scripts)

I hope this would be resolved. This is not convinient, but it works :)

genesistms avatar Jun 06 '25 05:06 genesistms

@genesistms I'm a bit puzzled how you were using the code before--since for example dart compile won't find the files. Is there some other compile that does work, e.g. dart2js, flutter?

davidmorgan avatar Jun 06 '25 06:06 davidmorgan

@davidmorgan sorry for not mentioning. We are building via build_web_compilers (dart2js/ddc)

We are using angulardart (community) and have root package and couple of dependent packages that needs to be built.

genesistms avatar Jun 06 '25 08:06 genesistms

@genesistms Ah, that makes sense: build_web_compilers uses build_runner so it does see the generated files. Thanks.

davidmorgan avatar Jun 06 '25 09:06 davidmorgan

@davidmorgan sorry for not mentioning. We are building via build_web_compilers (dart2js/ddc)

We are using angulardart (community) and have root package and couple of dependent packages that needs to be built.

Filed https://github.com/dart-lang/sdk/issues/60873 for the workspace compatibility issue.

davidmorgan avatar Jun 09 '25 06:06 davidmorgan