build
build copied to clipboard
how to use `build_runner` in pub workspaces?
- The following code must be added to the root
pubspec.yamlfile
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
- 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
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.
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.
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 :/
Restart the IDE. It's usually just the IDE that's out of sync
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.
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.
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.
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
I have similar problem.
App builds fine but analyzer does not see generated files. If i copy the
/app/.dart_tool/buildfolder 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?
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
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
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).
Hmmm I think
a/pubspec.yamlshould haveresolution: workspacewhen it's used in a workspace; and then you want to do
pub getin the workspace root to set up the workspace fully. (that will create a.dart_toolfolder 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.
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
analyzefails - It's a bit weird that
analyzehas this support at all given thatcompiledoesn't
I will raise both of these with analyzer+compiler teams. In the meantime, will setting output to source work for your use case?
I will raise both of these with analyzer+compiler teams.
Thanks for that. :)
In the meantime, will setting output to
sourcework for your use case?
It would work... yes.
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 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 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 Ah, that makes sense: build_web_compilers uses build_runner so it does see the generated files. Thanks.
@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.