You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Create projectname_shared project when generating projects #1256
I have run into several cases where my client and server need to share some code, e.g. business logic, or data classes / enums that don't need to be passed over the wire but are needed for processing on both the server and the client.
I noticed that the way this is solved by Serverpod itself is by creating a ..._shared directory: packages/serverpod_shared and tests/serverpod_test_shared.
serverpod generate should create a shared project like this automatically (creating ..._shared alongside ..._server, ..._client, and ..._flutter), with instructions for users to put shared code into that project if necessary, automatically adding a dependency on this project from the server and flutter projects.
At a minimum the shared project should contain a configuration file that specifies the host and port of the server. This has to be specified in two places right now:
it is specified in the client when calling the Client constructor;
it has to also be specified in the server, e.g. in config/development.yaml, otherwise file URLs generated by the server will not be valid. (By the way, this is not documented anywhere that I saw, I had to figure out the hard way why uploaded images weren't originally loading from the server...)
The generated starter project should pull from this shared configuration file on both the client and the server.
Reacted by Peter Scully, Frank, Marcelo Glasberg, Gazihan Alankuş and benjamin-otto
Yes! We had a discussion on this point yesterday and it would indeed be quite nice to have even though there is nothing stopping you from manually setting this up.
For documentation purposes:
We could also generate base classes in the shared package of our protocols, this way both the server and client classes could inherit from them and you got a single place where you can add extensions to the generate code (that should be shared).
I'll be manually setting this up, yes, but I couldn't figure out how to do it before I noticed your _shared projects -- I have raised the issue of wanting to share code between client and server once or twice before in Serverpod issues or discussions. Anyway it wasn't intuitive to me to come up with this solution, so that's why I propose generating all projects that way by default. People can delete the _shared project if they don't need it. But at least for host/port config, as I pointed out this is in fact an important usecase for a shared project.
Although that raises another issue -- when a client or server project is built, how does the compiler know to bundle a config file (e.g. a config yaml file) from a dependency with the build? I would assume only the code that is called in dependencies (after tree shaking) makes it into the final build, not every file that was shipped with every dependency.
OK, I tried manually setting up a shared project. The flutter and server projects both depend upon the shared project. However, the shared project needs to also depend on the client project, which is generated from the server, because some of the shared functions depend upon generated types (like enums, generated from protocol files on the server).
Including the shared project on the server now gives errors like:
The argument type 'FileType (where FileType is defined in /home/luke/Work/click/click_server/lib/src/generated/enum_file_type.dart)' can't be assigned to the parameter type 'FileType (where FileType is defined in /home/luke/Work/click/click_client/lib/src/protocol/enum_file_type.dart)'.dart[argument_type_not_assignable](https://dart.dev/diagnostics/argument_type_not_assignable)
enum_file_type.dart(11, 6): FileType is defined in /home/luke/Work/click/click_server/lib/src/generated/enum_file_type.dart
enum_file_type.dart(11, 6): FileType is defined in /home/luke/Work/click/click_client/lib/src/protocol/enum_file_type.dart
So I'm back to the drawing board. The shared project cannot depend upon the client project, therefore it can't use any of the types that should be commonly accessible on both the server and the flutter frontend.
Really what needs to happen is that there should be a way to mark classes on the server for copying into the client.
@lukehutch, I think you can manage this using a symlink. E.g. place your shared extensions in a file (or directory) on the server. Use ln -s to create a symlink for the file or the directory in the client or in your flutter code. You may need to use a relative import to import any supporting files (e.g. the generated protocol and models).
This way you will only need to maintain one file, but it will be present in two places.
@vlidholt I'm willing to contribute time and effort on a redesign and rework for this issue. We are currently designing a build_runner's builder for the topic and I'm realizing because of this that it wouldn't be as good as a 15/30 minutes talk and a roadmap of the changes needed. What do you think about this?
@lukehutch, I think you can manage this using a symlink. E.g. place your shared extensions in a file (or directory) on the server. Use ln -s to create a symlink for the file or the directory in the client or in your flutter code. You may need to use a relative import to import any supporting files (e.g. the generated protocol and models).
This way you will only need to maintain one file, but it will be present in two places.
That would work for pure non-generated shared code, but as the error message in my previous comment indicates, it won't work for any shared code that depends upon generated code (presumably because there are different server and client versions of generated classes).
@vlidholt How about allowing for a common directory in the server project, which, if present, is sync'd into the client project whenever serverpod generate is called? (The client should probably have a README file added that says Do not modify the contents of this directory, it is synchronized from the server. Or maybe the client directory should just be a softlink to the server.)
I think the proper solution is to correctly do the projectname_shared package, with generated abstract classes as a base for the other generated models.
I have tried to create a shared code project in a number of different ways. Nothing I do works. The fundamental problem is that there are separate server and client versions of all model classes, and shared code can import only one of these two. Actually the server versions are not even exported from the server project, so the only option in a separate project is to import the client version. But then if the server project adds a dependency to the shared project, it now transitively imports the client versions of the model classes -- and the server versions are not compatible with them.
One stop-gap might be to make the client and server model classes share a common abstract superclass, although I don't think that will work with enums, since they are not extensible.
The only solution I can see is to allow a user to mark a source file in the server project as something that should be copied across into the generated client project. However, when the file is copied, its imports also need to be updated so that they point to the client project's model classes, rather than the server project's model classes.
This is pretty important -- I keep having to duplicate code between my server and flutter projects, and having to manually keep both versions in sync is a big risk for breakage.
Activity
Isakdl commentedon Aug 4, 2023
Yes! We had a discussion on this point yesterday and it would indeed be quite nice to have even though there is nothing stopping you from manually setting this up.
For documentation purposes:
We could also generate base classes in the shared package of our protocols, this way both the server and client classes could inherit from them and you got a single place where you can add
extensionsto the generate code (that should be shared).lukehutch commentedon Aug 4, 2023
I'll be manually setting this up, yes, but I couldn't figure out how to do it before I noticed your
_sharedprojects -- I have raised the issue of wanting to share code between client and server once or twice before in Serverpod issues or discussions. Anyway it wasn't intuitive to me to come up with this solution, so that's why I propose generating all projects that way by default. People can delete the_sharedproject if they don't need it. But at least for host/port config, as I pointed out this is in fact an important usecase for a shared project.Although that raises another issue -- when a client or server project is built, how does the compiler know to bundle a config file (e.g. a config yaml file) from a dependency with the build? I would assume only the code that is called in dependencies (after tree shaking) makes it into the final build, not every file that was shipped with every dependency.
lukehutch commentedon Sep 3, 2023
OK, I tried manually setting up a shared project. The flutter and server projects both depend upon the shared project. However, the shared project needs to also depend on the client project, which is generated from the server, because some of the shared functions depend upon generated types (like enums, generated from protocol files on the server).
Including the shared project on the server now gives errors like:
So I'm back to the drawing board. The shared project cannot depend upon the client project, therefore it can't use any of the types that should be commonly accessible on both the server and the flutter frontend.
Really what needs to happen is that there should be a way to mark classes on the server for copying into the client.
vlidholt commentedon Sep 13, 2023
@lukehutch, I think you can manage this using a symlink. E.g. place your shared extensions in a file (or directory) on the server. Use
ln -sto create a symlink for the file or the directory in the client or in your flutter code. You may need to use a relative import to import any supporting files (e.g. the generated protocol and models).This way you will only need to maintain one file, but it will be present in two places.
Rodsevich commentedon Sep 13, 2023
@vlidholt I'm willing to contribute time and effort on a redesign and rework for this issue. We are currently designing a build_runner's builder for the topic and I'm realizing because of this that it wouldn't be as good as a 15/30 minutes talk and a roadmap of the changes needed. What do you think about this?
lukehutch commentedon Sep 13, 2023
That would work for pure non-generated shared code, but as the error message in my previous comment indicates, it won't work for any shared code that depends upon generated code (presumably because there are different server and client versions of generated classes).
vlidholt commentedon Sep 13, 2023
@lukehutch I'm pretty sure it would work if you make sure to import using the same relative import.
E.g.
Server side
Client side
Make a
ln -stomy_extension.dartAdd a second
my_exports.dartwhich references the local generated files.lukehutch commentedon Sep 14, 2023
@vlidholt so you're saying the client and server version of generated classes are always exactly the same?
I assumed they wouldn't be, because some fields of generated classes may be api-only, and some may be database-only.
vlidholt commentedon Oct 5, 2023
Sorry I'm a bit slow. They aren't exactly the same, but similar enough that you can extend them with the same code.
lukehutch commentedon Oct 5, 2023
@vlidholt How about allowing for a
commondirectory in the server project, which, if present, is sync'd into the client project wheneverserverpod generateis called? (The client should probably have aREADMEfile added that saysDo not modify the contents of this directory, it is synchronized from the server. Or maybe the client directory should just be a softlink to the server.)vlidholt commentedon Oct 10, 2023
I think the proper solution is to correctly do the
projectname_sharedpackage, with generated abstract classes as a base for the other generated models.lukehutch commentedon Feb 14, 2024
I have tried to create a shared code project in a number of different ways. Nothing I do works. The fundamental problem is that there are separate server and client versions of all model classes, and shared code can import only one of these two. Actually the server versions are not even exported from the server project, so the only option in a separate project is to import the client version. But then if the server project adds a dependency to the shared project, it now transitively imports the client versions of the model classes -- and the server versions are not compatible with them.
One stop-gap might be to make the client and server model classes share a common abstract superclass, although I don't think that will work with enums, since they are not extensible.
The only solution I can see is to allow a user to mark a source file in the server project as something that should be copied across into the generated client project. However, when the file is copied, its imports also need to be updated so that they point to the client project's model classes, rather than the server project's model classes.
This is pretty important -- I keep having to duplicate code between my server and flutter projects, and having to manually keep both versions in sync is a big risk for breakage.
Frank1016 commentedon Jul 24, 2024
Any update on fixing this issue from the Serverpod team?
iampopal commentedon Nov 7, 2024
We are looking for this too...